gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/internal/reflectlite/set_test.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package reflectlite_test 6 7 import ( 8 "bytes" 9 "go/ast" 10 "go/token" 11 "io" 12 "testing" 13 14 . "gitee.com/ks-custle/core-gm/internal/reflectlite" 15 ) 16 17 func TestImplicitSetConversion(t *testing.T) { 18 // Assume TestImplicitMapConversion covered the basics. 19 // Just make sure conversions are being applied at all. 20 var r io.Reader 21 b := new(bytes.Buffer) 22 rv := ValueOf(&r).Elem() 23 rv.Set(ValueOf(b)) 24 if r != b { 25 t.Errorf("after Set: r=%T(%v)", r, r) 26 } 27 } 28 29 var implementsTests = []struct { 30 x interface{} 31 t interface{} 32 b bool 33 }{ 34 {new(*bytes.Buffer), new(io.Reader), true}, 35 {new(bytes.Buffer), new(io.Reader), false}, 36 {new(*bytes.Buffer), new(io.ReaderAt), false}, 37 {new(*ast.Ident), new(ast.Expr), true}, 38 {new(*notAnExpr), new(ast.Expr), false}, 39 {new(*ast.Ident), new(notASTExpr), false}, 40 {new(notASTExpr), new(ast.Expr), false}, 41 {new(ast.Expr), new(notASTExpr), false}, 42 {new(*notAnExpr), new(notASTExpr), true}, 43 {new(mapError), new(error), true}, 44 {new(*mapError), new(error), true}, 45 } 46 47 type notAnExpr struct{} 48 49 func (notAnExpr) Pos() token.Pos { return token.NoPos } 50 func (notAnExpr) End() token.Pos { return token.NoPos } 51 func (notAnExpr) exprNode() {} 52 53 type notASTExpr interface { 54 Pos() token.Pos 55 End() token.Pos 56 exprNode() 57 } 58 59 type mapError map[string]string 60 61 func (mapError) Error() string { return "mapError" } 62 63 var _ error = mapError{} 64 var _ error = new(mapError) 65 66 func TestImplements(t *testing.T) { 67 for _, tt := range implementsTests { 68 xv := TypeOf(tt.x).Elem() 69 xt := TypeOf(tt.t).Elem() 70 if b := xv.Implements(xt); b != tt.b { 71 t.Errorf("(%s).Implements(%s) = %v, want %v", TypeString(xv), TypeString(xt), b, tt.b) 72 } 73 } 74 } 75 76 var assignableTests = []struct { 77 x interface{} 78 t interface{} 79 b bool 80 }{ 81 {new(chan int), new(<-chan int), true}, 82 {new(<-chan int), new(chan int), false}, 83 {new(*int), new(IntPtr), true}, 84 {new(IntPtr), new(*int), true}, 85 {new(IntPtr), new(IntPtr1), false}, 86 {new(Ch), new(<-chan interface{}), true}, 87 // test runs implementsTests too 88 } 89 90 type IntPtr *int 91 type IntPtr1 *int 92 type Ch <-chan interface{} 93 94 func TestAssignableTo(t *testing.T) { 95 for i, tt := range append(assignableTests, implementsTests...) { 96 xv := TypeOf(tt.x).Elem() 97 xt := TypeOf(tt.t).Elem() 98 if b := xv.AssignableTo(xt); b != tt.b { 99 t.Errorf("%d:AssignableTo: got %v, want %v", i, b, tt.b) 100 } 101 } 102 }