reflect: add {methodIter.Seq,methodIter2.Seq2} test cases for {CanSeq,CanSeq2}

For #71874.

Change-Id: Idc834fdd9ade41ba4976ded32e5861a2dcef44bf
This commit is contained in:
Jes Cok 2025-02-22 10:58:41 +08:00
parent d31c805535
commit 574c1edb7a
2 changed files with 12 additions and 2 deletions

View File

@ -176,7 +176,7 @@ func TestValueSeq(t *testing.T) {
t.Fatalf("should loop four times")
}
}},
{"method", ValueOf(methodIter{}).Method(0), func(t *testing.T, s iter.Seq[Value]) {
{"method", ValueOf(methodIter{}).MethodByName("Seq"), func(t *testing.T, s iter.Seq[Value]) {
i := int64(0)
for v := range s {
if v.Int() != i {
@ -323,7 +323,7 @@ func TestValueSeq2(t *testing.T) {
t.Fatalf("should loop four times")
}
}},
{"method", ValueOf(methodIter2{}).Method(0), func(t *testing.T, s iter.Seq2[Value, Value]) {
{"method", ValueOf(methodIter2{}).MethodByName("Seq2"), func(t *testing.T, s iter.Seq2[Value, Value]) {
i := int64(0)
for v1, v2 := range s {
if v1.Int() != i {
@ -393,6 +393,9 @@ func (methodIter) Seq(yield func(int) bool) {
}
}
// For Type.CanSeq test.
func (methodIter) NonSeq(yield func(int)) {}
// methodIter2 is a type from which we can derive a method
// value that is an iter.Seq2.
type methodIter2 struct{}
@ -404,3 +407,6 @@ func (methodIter2) Seq2(yield func(int, int) bool) {
}
}
}
// For Type.CanSeq2 test.
func (methodIter2) NonSeq2(yield func(int, int)) {}

View File

@ -126,6 +126,8 @@ func TestType_CanSeq(t *testing.T) {
}{
{"func(func(int) bool)", reflect.TypeOf(func(func(int) bool) {}), true},
{"func(func(int))", reflect.TypeOf(func(func(int)) {}), false},
{"methodIter.Seq", reflect.ValueOf(methodIter{}).MethodByName("Seq").Type(), true},
{"methodIter.NonSeq", reflect.ValueOf(methodIter{}).MethodByName("NonSeq").Type(), false},
{"int64", reflect.TypeOf(int64(1)), true},
{"uint64", reflect.TypeOf(uint64(1)), true},
{"*[4]int", reflect.TypeOf(&[4]int{}), true},
@ -151,6 +153,8 @@ func TestType_CanSeq2(t *testing.T) {
}{
{"func(func(int, int) bool)", reflect.TypeOf(func(func(int, int) bool) {}), true},
{"func(func(int, int))", reflect.TypeOf(func(func(int, int)) {}), false},
{"methodIter2.Seq2", reflect.ValueOf(methodIter2{}).MethodByName("Seq2").Type(), true},
{"methodIter2.NonSeq2", reflect.ValueOf(methodIter2{}).MethodByName("NonSeq2").Type(), false},
{"int64", reflect.TypeOf(int64(1)), false},
{"uint64", reflect.TypeOf(uint64(1)), false},
{"*[4]int", reflect.TypeOf(&[4]int{}), true},