mirror of
https://github.com/golang/go
synced 2024-10-04 10:21: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:
parent
8d2757cba4
commit
0a6ad46b4f
@ -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 })
|
||||
|
Loading…
Reference in New Issue
Block a user