-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmicroerror_test.go
227 lines (219 loc) · 5.22 KB
/
microerror_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package microerror
import (
"errors"
"fmt"
"strconv"
"testing"
)
func Test_Cause(t *testing.T) {
var testCauseMicroError = &Error{
Kind: "testCauseErrorB",
}
var testCauseErrorsNewError = errors.New("test cause error A")
var testCauseErrorsNewWrappedError = fmt.Errorf("test cause error B: %w", errors.New("test cause error A"))
testCases := []struct {
name string
inputErrorFunc func() error
expectedCauseError error
}{
{
name: "case 0: nil",
inputErrorFunc: func() error {
return nil
},
expectedCauseError: nil,
},
{
name: "case 1: no masking error=microerror.Error",
inputErrorFunc: func() error {
err := testCauseMicroError
return err
},
expectedCauseError: testCauseMicroError,
},
{
name: "case 2: no masking error=errors.New",
inputErrorFunc: func() error {
err := testCauseErrorsNewError
return err
},
expectedCauseError: testCauseErrorsNewError,
},
{
name: "case 3: Maskf depth=1 error=microerror.Error",
inputErrorFunc: func() error {
err := Maskf(testCauseMicroError, "test annotation")
return err
},
expectedCauseError: testCauseMicroError,
},
{
name: "case 4: Maskf depth=3 error=microerror.Error",
inputErrorFunc: func() error {
err := Maskf(testCauseMicroError, "test annotation")
err = Mask(err)
err = Mask(err)
err = Mask(err)
return err
},
expectedCauseError: testCauseMicroError,
},
{
name: "case 5: Mask depth=1 error=microerror.Error",
inputErrorFunc: func() error {
err := Mask(testCauseErrorsNewError)
return err
},
expectedCauseError: testCauseErrorsNewError,
},
{
name: "case 6: Mask depth=3 error=microerror.Error",
inputErrorFunc: func() error {
err := Mask(testCauseErrorsNewError)
err = Mask(err)
err = Mask(err)
return err
},
expectedCauseError: testCauseErrorsNewError,
},
{
name: "case 7: Mask depth=1 error=microerror.Error",
inputErrorFunc: func() error {
err := Mask(testCauseErrorsNewError)
return err
},
expectedCauseError: testCauseErrorsNewError,
},
{
name: "case 8: Mask depth=3 error=errors.New",
inputErrorFunc: func() error {
err := Mask(testCauseErrorsNewError)
err = Mask(err)
err = Mask(err)
return err
},
expectedCauseError: testCauseErrorsNewError,
},
{
name: "case 9: Mask depth=3 error=fmt.Printf",
inputErrorFunc: func() error {
err := Mask(testCauseErrorsNewWrappedError)
err = Mask(err)
err = Mask(err)
return err
},
expectedCauseError: testCauseErrorsNewWrappedError,
},
}
for i, tc := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Log(tc.name)
err := tc.inputErrorFunc()
cause := Cause(err)
if cause != tc.expectedCauseError {
t.Errorf("err = %#v, want %#v", cause, tc.expectedCauseError)
}
})
}
}
func Test_Mask_Nil(t *testing.T) {
err := Mask(nil)
if err != nil {
t.Fatalf("expected nil, got %v", err)
}
}
var testMicroErr = &Error{
Desc: "test-desc",
Docs: "test-docs",
Kind: "testKind",
}
func Test_Mask_Error(t *testing.T) {
testCases := []struct {
name string
inputErrorFunc func() error
expectedError string
}{
{
name: "case 0: Maskf depth=1 error=microerror.Error",
inputErrorFunc: func() error {
err := Maskf(testMicroErr, "test annotation")
return err
},
expectedError: "test kind: test annotation",
},
{
name: "case 1: Maskf depth=3 error=microerror.Error",
inputErrorFunc: func() error {
err := Maskf(testMicroErr, "test annotation")
err = Mask(err)
err = Mask(err)
return err
},
expectedError: "test kind: test annotation",
},
{
name: "case 2: Maskf depth=3 error=microerror.Error & empty annotation",
inputErrorFunc: func() error {
err := Maskf(testMicroErr, "")
err = Mask(err)
err = Mask(err)
return err
},
expectedError: "test kind",
},
{
name: "case 3: Mask depth=1 error=microerror.Error",
inputErrorFunc: func() error {
err := Mask(testMicroErr)
return err
},
expectedError: "test kind",
},
{
name: "case 4: Mask depth=3 error=microerror.Error",
inputErrorFunc: func() error {
err := Mask(testMicroErr)
err = Mask(err)
err = Mask(err)
return err
},
expectedError: "test kind",
},
{
name: "case 5: Mask depth=1 error=errors.New",
inputErrorFunc: func() error {
err := Mask(errors.New("test error"))
return err
},
expectedError: "test error",
},
{
name: "case 6: Mask depth=3 error=errors.New",
inputErrorFunc: func() error {
err := Mask(errors.New("test error"))
err = Mask(err)
err = Mask(err)
return err
},
expectedError: "test error",
},
{
name: "case 9: Mask depth=3 error=fmt.Printf",
inputErrorFunc: func() error {
err := Mask(fmt.Errorf("test cause error B: %w", errors.New("test cause error A")))
err = Mask(err)
err = Mask(err)
return err
},
expectedError: "test cause error B: test cause error A",
},
}
for i, tc := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
err := tc.inputErrorFunc()
if err.Error() != tc.expectedError {
t.Errorf("err.Error() = %#q, want %#q", err.Error(), tc.expectedError)
}
})
}
}