Example #1
0
func TestUnionInArray(t *testing.T) {
	var ss, out test_unions.SmallStruct
	ss.PodUnionArray = &[]test_unions.PodUnion{
		&test_unions.PodUnionFInt8{8},
		&test_unions.PodUnionFInt16{16},
		&test_unions.PodUnionFUint64{64},
		&test_unions.PodUnionFBool{true},
		&test_unions.PodUnionFEnum{test_unions.AnEnum_Second},
	}
	check(t, &ss, &out)
}
Example #2
0
func TestUnionInArrayNonNullableNull(t *testing.T) {
	// Encoding should fail
	var ss test_unions.SmallStruct
	ss.PodUnionArray = &[]test_unions.PodUnion{
		nil,
		&test_unions.PodUnionFInt8{8},
		&test_unions.PodUnionFInt16{16},
		&test_unions.PodUnionFUint64{64},
		&test_unions.PodUnionFBool{true},
		&test_unions.PodUnionFEnum{test_unions.AnEnum_Second},
	}

	_, _, err := encode(t, &ss)
	if typedErr := err.(*bindings.ValidationError); typedErr.ErrorCode != bindings.UnexpectedNullUnion {
		t.Fatalf("Non-nullable null should have failed to encode.")
	}

	// Decoding should also fail
	ss.PodUnionArray = &[]test_unions.PodUnion{
		&test_unions.PodUnionFInt8{8},
		&test_unions.PodUnionFInt16{16},
		&test_unions.PodUnionFUint64{64},
		&test_unions.PodUnionFBool{true},
		&test_unions.PodUnionFEnum{test_unions.AnEnum_Second},
	}
	bytes, handles, _ := encode(t, &ss)

	// Set first union to null.
	bytes[8*10] = 0
	var decoded test_unions.SmallStruct
	decoder := bindings.NewDecoder(bytes, handles)
	err = decoded.Decode(decoder)
	if typedErr := err.(*bindings.ValidationError); typedErr.ErrorCode != bindings.UnexpectedNullUnion {
		t.Fatalf("Null non-nullable should have failed to decode.")
	}
}