1
0
mirror of https://github.com/golang/go synced 2024-10-04 12:31:21 -06:00

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
This commit is contained in:
Michael Hudson-Doyle 2013-12-17 14:49:51 -08:00 committed by Brad Fitzpatrick
parent 8d2757cba4
commit 0a6ad46b4f

View 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) { func TestMakeFunc(t *testing.T) {
f := dummy f := dummy
fv := MakeFunc(TypeOf(f), func(in []Value) []Value { return in }) fv := MakeFunc(TypeOf(f), func(in []Value) []Value { return in })