]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: Add tests for Call with functions taking and returning structs.
authorMichael Hudson-Doyle <michael.hudson@linaro.org>
Tue, 17 Dec 2013 22:49:51 +0000 (14:49 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 17 Dec 2013 22:49:51 +0000 (14:49 -0800)
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

src/pkg/reflect/all_test.go

index df64e3bb7b31346be8ca8fda3534b3ab21f3d0fb..eb84a862d94748c45e05d175b4698c5f77a41b37 100644 (file)
@@ -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 })