github.com/searKing/golang/go@v1.2.117/errors/mark_test.go (about) 1 // Copyright 2023 The searKing Author. 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 errors_test 6 7 import ( 8 "errors" 9 "reflect" 10 "testing" 11 12 errors_ "github.com/searKing/golang/go/errors" 13 ) 14 15 func TestMarkReturnsNil(t *testing.T) { 16 if err := errors_.Mark(nil); err != nil { 17 t.Errorf("errors_.Mark(nil) = %v, want nil", err) 18 } 19 if err := errors_.Mark(nil, nil); err != nil { 20 t.Errorf("errors_.Mark(nil, nil) = %v, want nil", err) 21 } 22 mark := errors.New("mark") 23 if err := errors_.Mark(nil, mark); err != nil { 24 t.Errorf("errors_.Mark(nil, %v) = %v, want nil", mark, err) 25 } 26 } 27 28 func TestMark(t *testing.T) { 29 err := errors.New("err") 30 mark1 := errors.New("mark1") 31 mark2 := errors.New("mark2") 32 for _, test := range []struct { 33 err error 34 errs []error 35 want error 36 }{{ 37 err: err, 38 errs: []error{mark1}, 39 want: err, 40 }, { 41 err: err, 42 errs: []error{mark1, mark2}, 43 want: err, 44 }, { 45 err: err, 46 errs: []error{mark1, nil, mark2}, 47 want: err, 48 }} { 49 got := errors_.Mark(test.err, test.errs...).(interface{ Unwrap() error }).Unwrap() 50 if !reflect.DeepEqual(got, test.want) { 51 t.Errorf("Mark(%v) = %v; want %v", test.errs, got, test.want) 52 } 53 } 54 } 55 56 func TestMarkErrorMethod(t *testing.T) { 57 err := errors.New("err") 58 mark1 := errors.New("mark1") 59 mark2 := errors.New("mark2") 60 for _, test := range []struct { 61 err error 62 errs []error 63 want string 64 }{{ 65 err: err, 66 errs: []error{mark1}, 67 want: "err", 68 }, { 69 err: err, 70 errs: []error{mark1, mark2}, 71 want: "err", 72 }, { 73 err: err, 74 errs: []error{mark1, nil, mark2}, 75 want: "err", 76 }} { 77 got := errors_.Mark(test.err, test.errs...).Error() 78 if got != test.want { 79 t.Errorf("Mark(%v).Error() = %q; want %q", test.errs, got, test.want) 80 } 81 } 82 } 83 84 func TestMarkErrorIs(t *testing.T) { 85 err := errors.New("err") 86 mark1 := errors.New("mark1") 87 mark2 := errors.New("mark2") 88 for _, test := range []struct { 89 err error 90 errs []error 91 want []error 92 notWant []error 93 }{{ 94 err: nil, 95 errs: []error{mark1}, 96 want: []error{nil}, 97 notWant: []error{err, mark1, mark2}, 98 }, { 99 err: err, 100 errs: []error{mark1}, 101 want: []error{err, mark1}, 102 notWant: []error{nil, mark2}, 103 }, { 104 err: err, 105 errs: []error{mark1, mark2}, 106 want: []error{err, mark1, mark2}, 107 notWant: []error{nil}, 108 }, { 109 err: err, 110 errs: []error{mark1, nil, mark2}, 111 want: []error{err, mark1, mark2}, 112 notWant: []error{nil}, 113 }} { 114 got := errors_.Mark(test.err, test.errs...) 115 for _, want := range test.want { 116 if !errors.Is(got, want) { 117 t.Errorf("errors.Is(Mark(%v), %v) = %t; want %t", test.errs, want, got, true) 118 } 119 } 120 } 121 }