github.com/FISCO-BCOS/crypto@v0.0.0-20200202032121-bd8ab0b5d4f1/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 . "internal/reflectlite" 12 "io" 13 "testing" 14 ) 15 16 func TestImplicitSetConversion(t *testing.T) { 17 // Assume TestImplicitMapConversion covered the basics. 18 // Just make sure conversions are being applied at all. 19 var r io.Reader 20 b := new(bytes.Buffer) 21 rv := ValueOf(&r).Elem() 22 rv.Set(ValueOf(b)) 23 if r != b { 24 t.Errorf("after Set: r=%T(%v)", r, r) 25 } 26 } 27 28 var implementsTests = []struct { 29 x interface{} 30 t interface{} 31 b bool 32 }{ 33 {new(*bytes.Buffer), new(io.Reader), true}, 34 {new(bytes.Buffer), new(io.Reader), false}, 35 {new(*bytes.Buffer), new(io.ReaderAt), false}, 36 {new(*ast.Ident), new(ast.Expr), true}, 37 {new(*notAnExpr), new(ast.Expr), false}, 38 {new(*ast.Ident), new(notASTExpr), false}, 39 {new(notASTExpr), new(ast.Expr), false}, 40 {new(ast.Expr), new(notASTExpr), false}, 41 {new(*notAnExpr), new(notASTExpr), true}, 42 } 43 44 type notAnExpr struct{} 45 46 func (notAnExpr) Pos() token.Pos { return token.NoPos } 47 func (notAnExpr) End() token.Pos { return token.NoPos } 48 func (notAnExpr) exprNode() {} 49 50 type notASTExpr interface { 51 Pos() token.Pos 52 End() token.Pos 53 exprNode() 54 } 55 56 func TestImplements(t *testing.T) { 57 for _, tt := range implementsTests { 58 xv := TypeOf(tt.x).Elem() 59 xt := TypeOf(tt.t).Elem() 60 if b := xv.Implements(xt); b != tt.b { 61 t.Errorf("(%s).Implements(%s) = %v, want %v", TypeString(xv), TypeString(xt), b, tt.b) 62 } 63 } 64 } 65 66 var assignableTests = []struct { 67 x interface{} 68 t interface{} 69 b bool 70 }{ 71 {new(chan int), new(<-chan int), true}, 72 {new(<-chan int), new(chan int), false}, 73 {new(*int), new(IntPtr), true}, 74 {new(IntPtr), new(*int), true}, 75 {new(IntPtr), new(IntPtr1), false}, 76 {new(Ch), new(<-chan interface{}), true}, 77 // test runs implementsTests too 78 } 79 80 type IntPtr *int 81 type IntPtr1 *int 82 type Ch <-chan interface{} 83 84 func TestAssignableTo(t *testing.T) { 85 for i, tt := range append(assignableTests, implementsTests...) { 86 xv := TypeOf(tt.x).Elem() 87 xt := TypeOf(tt.t).Elem() 88 if b := xv.AssignableTo(xt); b != tt.b { 89 t.Errorf("%d:AssignableTo: got %v, want %v", i, b, tt.b) 90 } 91 } 92 }