-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors_test.go
146 lines (126 loc) · 4.01 KB
/
errors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package testament_test
import (
"fmt"
"testing"
"github.com/blokur/testament"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestAssertInError(t *testing.T) {
t.Parallel()
msg1 := testament.RandomString(50)
msg2 := testament.RandomString(60)
msg3 := testament.RandomString(70)
err1 := errors.New(msg1)
err2 := errors.New(msg2)
err3 := errors.New(msg3)
err1STL := fmt.Errorf("%s %w %s", testament.RandomString(80), err1, testament.RandomString(90))
mutiWrap := func(errs ...error) error {
var err *multierror.Error
for _, e := range errs {
err = multierror.Append(err, e)
}
return err
}
tcs := map[string]struct {
needle error
haystack error
want assert.BoolAssertionFunc
}{
"same": {err1, err1, assert.True},
"different": {err1, err2, assert.False},
"nil needle": {nil, err2, assert.False},
"nil haystack": {err1, nil, assert.False},
"wrapped true": {err1, errors.Wrap(err1, msg3), assert.True},
"wrapped false": {err1, errors.Wrap(err2, msg3), assert.False},
"wrapped twice": {err1, errors.Wrap(errors.Wrap(err1, msg3), msg2), assert.True},
"in wrapped message": {err1, errors.Wrap(err2, err1.Error()), assert.True},
"stl true": {err1, err1STL, assert.True},
"stl false": {
err1,
fmt.Errorf("%s %w %s", testament.RandomString(40), err2, testament.RandomString(40)),
assert.False,
},
"in stl message": {
err1,
fmt.Errorf("%s %w %s", testament.RandomString(40), err2, err1.Error()),
assert.True,
},
"stl(stl)": {
err1,
fmt.Errorf("%s %w %s", testament.RandomString(40), err1STL, testament.RandomString(40)),
assert.True,
},
"wrap(stl)": {
err1,
errors.Wrap(err1STL, testament.RandomString(40)),
assert.True,
},
"stl(wrap)": {
err1,
fmt.Errorf("%s %w %s", testament.RandomString(40), errors.Wrap(err1, testament.RandomString(40)), testament.RandomString(40)),
assert.True,
},
"multi false": {err1, mutiWrap(err2), assert.False},
"multi true": {err1, mutiWrap(err1), assert.True},
"multi mixed": {err1, mutiWrap(err2, err1, err3), assert.True},
"wrap(multi)": {err1, errors.Wrap(mutiWrap(err2, err1, err3), msg3), assert.True},
"stl(multi)": {err1, fmt.Errorf("%s %w %s", msg2, mutiWrap(err2, err1, err3), msg3), assert.True},
"stl(multi(wrap))": {
err1,
fmt.Errorf("%s %w %s", msg2, mutiWrap(err2, errors.Wrap(err1, testament.RandomString(100)), err3), msg3),
assert.True,
},
"stl(wrap(multi))": {
err1,
fmt.Errorf("%s %w %s", msg2, errors.Wrap(mutiWrap(err2, err1, err3), testament.RandomString(100)), msg3),
assert.True,
},
"multi(stl(wrap))": {
err1,
mutiWrap(err2, fmt.Errorf("%s %w %s", msg2, errors.Wrap(err1, testament.RandomString(100)), err3)),
assert.True,
},
"multi(wrap(stl))": {
err1,
mutiWrap(err2, errors.Wrap(fmt.Errorf("%s %w %s", msg2, err1, err3), testament.RandomString(100))),
assert.True,
},
"wrap(multi(stl))": {
err1,
errors.Wrap(mutiWrap(err2, fmt.Errorf("%s %w %s", msg2, err1, err3)), testament.RandomString(100)),
assert.True,
},
"wrap(stl(multi))": {
err1,
errors.Wrap(fmt.Errorf("%s %w %s", msg2, mutiWrap(err2, err1, err3), testament.RandomString(100)), testament.RandomString(100)),
assert.True,
},
}
for name, tc := range tcs {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
noT := &testing.T{}
got := testament.AssertInError(noT, tc.haystack, tc.needle)
tc.want(t, got)
})
}
}
func TestAssertIsCode(t *testing.T) {
for i := 1; i < 17; i++ {
tc := codes.Code(i)
name := tc.String()
t.Run(name, func(t *testing.T) {
err := status.New(tc, assert.AnError.Error())
got := testament.AssertIsCode(t, err.Err(), tc)
assert.True(t, got)
wrappedErr := errors.Wrap(status.Error(tc, assert.AnError.Error()), testament.RandomString(10))
got = testament.AssertIsCode(t, wrappedErr, tc)
assert.True(t, got)
})
}
}