github.com/bobyang007/helper@v1.1.3/reflecth/utils_test.go (about) 1 package reflecth 2 3 import ( 4 "github.com/bobyang007/helper/goh/asth" 5 "github.com/bobyang007/helper/strconvh" 6 "go/ast" 7 "reflect" 8 "testing" 9 ) 10 11 func TestTypeOfPtr(t *testing.T) { 12 type testElement struct { 13 i interface{} 14 r reflect.Type 15 } 16 17 var b bool 18 19 tests := []testElement{ 20 {&b, TypeBool()}, 21 {b, nil}, 22 } 23 24 for i, test := range tests { 25 if r := TypeOfPtr(test.i); r != test.r { 26 t.Errorf("#%v expect %v, got %v", i, test.r, r) 27 } 28 } 29 } 30 31 func TestValueOfPtr(t *testing.T) { 32 var b = true 33 34 if bV := ValueOfPtr(&b); bV.Type() != TypeBool() || bV.Bool() != b { 35 t.Errorf("expect " + strconvh.FormatBool(b) + " (type bool), got " + bV.String()) 36 } 37 38 if bV := ValueOfPtr(b); bV != (reflect.Value{}) { 39 t.Errorf("expect zero value, got " + bV.String()) 40 } 41 } 42 43 func TestChanDir(t *testing.T) { 44 type testElement struct { 45 a ast.ChanDir 46 r reflect.ChanDir 47 } 48 49 tests := []testElement{ 50 {asth.BothDir, reflect.BothDir}, 51 {asth.RecvDir, reflect.RecvDir}, 52 {asth.SendDir, reflect.SendDir}, 53 {0, 0}, 54 } 55 56 for _, test := range tests { 57 if r := ChanDirFromAst(test.a); r != test.r { 58 t.Errorf("%v: expect %v, got %v", test.a, test.r, r) 59 } 60 if a := ChanDirToAst(test.r); a != test.a { 61 t.Errorf("%v: expect %v, got %v", test.r, test.a, a) 62 } 63 } 64 }