gopkg.in/hedzr/errors.v3@v3.3.1/coded_test.go (about) 1 package errors 2 3 import ( 4 "fmt" 5 "io" 6 "testing" 7 ) 8 9 type bizErr struct { //nolint:unused //usable 10 num int 11 } 12 13 func (e *bizErr) Error() string { //nolint:unused //usable 14 return fmt.Sprintf("%v", e.num) 15 } 16 17 func TestCode_WithCode(t *testing.T) { 18 c := Internal 19 c1 := (&c).WithCode(NotFound) 20 21 t.Logf("failed: %+v", c1) 22 23 c = Code(111) 24 t.Logf("failed: %+v", c) 25 } 26 27 func TestCode_Register(t *testing.T) { 28 c := Code(111) 29 t.Logf("failed: %+v", c) 30 31 _ = c.Register("Code111") 32 t.Logf("failed: %+v", c) 33 } 34 35 // func TestCodeEqual(t *testing.T) { 36 // be := &bizErr{1} 37 // err := InvalidArgument.New("wrong").Attach(be) 38 // 39 // //var e *bizErr 40 // e1 := err.Unwrap().(*WithCodeInfo) 41 // 42 // if !e1.Equal(InvalidArgument) { 43 // t.Fatal("expecting e1 is equal to InvalidArgument") 44 // } 45 // if !Equal(e1, InvalidArgument) { 46 // t.Fatal("expecting e1 is equal to InvalidArgument") 47 // } 48 // } 49 // 50 // func TestCodeAsIsAndSoOn(t *testing.T) { 51 // be := &bizErr{1} 52 // err := InvalidArgument.New("wrong").Attach(be) 53 // 54 // var e *bizErr 55 // e1 := err.Unwrap().(*WithCodeInfo) 56 // if !e1.As(&e) { 57 // t.Fatal("WithCodeInfo.As() failed.") 58 // } 59 // 60 // if !err.Is(be) { 61 // t.Fatal("WithCodeInfo.Is() failed.") 62 // } 63 // } 64 65 // func TestCodes(t *testing.T) { 66 // be := &bizErr{1} 67 // err := InvalidArgument.New("wrong").Attach(be) 68 // t.Log(err) 69 // t.Logf("%+v", err) 70 // 71 // exm := Internal.New("msg") 72 // ex := exm.Unwrap() 73 // if x, ok := ex.(interface{ Code() Code }); ok { 74 // t.Log(x.Code()) 75 // t.Logf("Internal: %q | cause = %v", x, ex.(*WithCodeInfo).Cause()) 76 // } else { 77 // t.Fatalf("Internal: %v", ex) 78 // } 79 // 80 // if !old.Is(err, be) { 81 // t.Fatal("wrong Is(): expecting be") 82 // } 83 // if old.Is(err, io.EOF) { 84 // t.Fatal("wrong Is(): shouldn't be like to io.EOF") 85 // } 86 // } 87 88 func TestCodesEqual(t *testing.T) { 89 err := InvalidArgument.New("wrong").WithErrors(io.ErrShortWrite) 90 91 ok := Is(err, InvalidArgument) 92 if !ok { 93 t.Fatal("want Equal() return true but got false") 94 } 95 } 96 97 func TestCodesRegister(t *testing.T) { 98 const illegalStateEx Code = MinErrorCode - 1 99 _ = RegisterCode(int(illegalStateEx), "I'm in an illegal state (ext for testing).") 100 t.Log(illegalStateEx) 101 t.Logf("%+v", illegalStateEx) 102 }