Commit 0a6ad46b authored by Michael Hudson-Doyle's avatar Michael Hudson-Doyle Committed by Brad Fitzpatrick

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
parent 8d2757cb
......@@ -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 })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment