From: Michael Hudson-Doyle Date: Tue, 17 Dec 2013 22:49:51 +0000 (-0800) Subject: reflect: Add tests for Call with functions taking and returning structs. X-Git-Tag: go1.3beta1~1196 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=0a6ad46b4f59f38fd1b55d0f46134133d74bd376;p=gostls13.git reflect: Add tests for Call with functions taking and returning structs. gccgo has problems using reflect.Call with functions that take and return structs with no members. Prior to fixing that problem there, I thought it sensible to add some tests of this situation. Update #6761 First contribution to Go, apologies in advance if I'm doing it wrong. R=golang-dev, dave, minux.ma, iant, khr, bradfitz CC=golang-dev https://golang.org/cl/26570046 --- diff --git a/src/pkg/reflect/all_test.go b/src/pkg/reflect/all_test.go index df64e3bb7b..eb84a862d9 100644 --- a/src/pkg/reflect/all_test.go +++ b/src/pkg/reflect/all_test.go @@ -1434,6 +1434,46 @@ func TestFunc(t *testing.T) { } } +type emptyStruct struct{} + +type nonEmptyStruct struct { + member int +} + +func returnEmpty() emptyStruct { + return emptyStruct{} +} + +func takesEmpty(e emptyStruct) { +} + +func returnNonEmpty(i int) nonEmptyStruct { + return nonEmptyStruct{member: i} +} + +func takesNonEmpty(n nonEmptyStruct) int { + return n.member +} + +func TestCallWithStruct(t *testing.T) { + r := ValueOf(returnEmpty).Call(nil) + if len(r) != 1 || r[0].Type() != TypeOf(emptyStruct{}) { + t.Errorf("returning empty struct returned %#v instead", r) + } + r = ValueOf(takesEmpty).Call([]Value{ValueOf(emptyStruct{})}) + if len(r) != 0 { + t.Errorf("takesEmpty returned values: %#v", r) + } + r = ValueOf(returnNonEmpty).Call([]Value{ValueOf(42)}) + if len(r) != 1 || r[0].Type() != TypeOf(nonEmptyStruct{}) || r[0].Field(0).Int() != 42 { + t.Errorf("returnNonEmpty returned %#v", r) + } + r = ValueOf(takesNonEmpty).Call([]Value{ValueOf(nonEmptyStruct{member: 42})}) + if len(r) != 1 || r[0].Type() != TypeOf(1) || r[0].Int() != 42 { + t.Errorf("takesNonEmpty returned %#v", r) + } +} + func TestMakeFunc(t *testing.T) { f := dummy fv := MakeFunc(TypeOf(f), func(in []Value) []Value { return in })