示例#1
0
func TestUnionInStruct(t *testing.T) {
	var ss, out test_unions.SmallStruct
	ss.PodUnion = &test_unions.PodUnionFInt8{10}
	check(t, &ss, &out)

	bytes, _, _ := encode(t, &ss)
	if int(bytes[8*2]) != 16 {
		t.Fatalf("Union does not start at the correct location in struct.")
	}
}
示例#2
0
func TestUnionInMap(t *testing.T) {
	var ss, out test_unions.SmallStruct
	ss.PodUnionMap = &map[string]test_unions.PodUnion{
		"eight":      &test_unions.PodUnionFInt8{8},
		"sixteen":    &test_unions.PodUnionFInt16{16},
		"sixty-four": &test_unions.PodUnionFUint64{64},
		"bool":       &test_unions.PodUnionFBool{true},
		"enum":       &test_unions.PodUnionFEnum{test_unions.AnEnum_Second},
	}
	check(t, &ss, &out)
}
示例#3
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)
}
示例#4
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.")
	}
}