-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr_wrapper_test.go
181 lines (167 loc) · 5.51 KB
/
err_wrapper_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
package berror_test
import (
"errors"
"fmt"
"github.com/bearaujus/berror"
"testing"
)
const (
testFormat = "error timeout"
testFormatWithArgs = "error timeout: %v %v"
testStackTraceCaputurerMockPath = "./test"
testErrorCode = "t001"
)
var (
testErrDef = berror.NewErrDefinition("xxx %v",
berror.OptionErrDefinitionWithCustomStackTraceCapturer(stackTraceCapturerMock))
testSampleErr = errors.New("test sample error")
testSampleErr2 = errors.New("test sample error 2")
testSampleErr3 = errors.New("test sample error 3")
stackTraceCapturerMock berror.ErrWrapperStackTraceCapturer = func() string {
return testStackTraceCaputurerMockPath
}
)
func Test_err_wrapper(t *testing.T) {
type args struct {
format string
opts []berror.ErrDefinitionOption
args []any
}
tests := []struct {
name string
args args
wantRawErrStr string
wantErrStr string
wantErrCode string
wantErrUnwarp map[string]bool
wantErrIs map[error]bool
wantStackTrace string
}{
{
name: "simple usage",
args: args{
format: testFormat,
opts: []berror.ErrDefinitionOption{berror.OptionErrDefinitionWithCustomStackTraceCapturer(stackTraceCapturerMock)},
args: nil,
},
wantRawErrStr: "error timeout",
wantErrStr: "error timeout (./test)",
wantErrCode: "",
wantErrUnwarp: map[string]bool{},
wantErrIs: map[error]bool{},
wantStackTrace: "./test",
},
{
name: "usage with args",
args: args{
format: testFormatWithArgs,
opts: []berror.ErrDefinitionOption{berror.OptionErrDefinitionWithCustomStackTraceCapturer(stackTraceCapturerMock)},
args: []interface{}{"arg1", "arg2"},
},
wantRawErrStr: "error timeout: arg1 arg2",
wantErrStr: "error timeout: arg1 arg2 (./test)",
wantErrCode: "",
wantErrUnwarp: map[string]bool{},
wantErrIs: map[error]bool{},
wantStackTrace: "./test",
},
{
name: "usage with args and options",
args: args{
format: testFormatWithArgs,
opts: []berror.ErrDefinitionOption{
berror.OptionErrDefinitionWithErrCode(testErrorCode),
berror.OptionErrDefinitionWithCustomFormater(func(err string, code string, stack string) string {
return fmt.Sprintf("%v--%v--%v", err, code, stack)
}),
berror.OptionErrDefinitionWithCustomStackTraceCapturer(stackTraceCapturerMock),
},
args: []interface{}{"arg1", "arg2"},
},
wantRawErrStr: "error timeout: arg1 arg2",
wantErrStr: "error timeout: arg1 arg2--t001--./test",
wantErrCode: "t001",
wantErrUnwarp: map[string]bool{},
wantErrIs: map[error]bool{},
wantStackTrace: "./test",
},
{
name: "unwarp and errors is",
args: args{
format: testFormatWithArgs,
opts: []berror.ErrDefinitionOption{
berror.OptionErrDefinitionWithErrCode(testErrorCode),
berror.OptionErrDefinitionWithCustomStackTraceCapturer(stackTraceCapturerMock),
},
args: []interface{}{testSampleErr, testErrDef.New(testSampleErr2)},
},
wantRawErrStr: "error timeout: test sample error xxx test sample error 2 (./test)",
wantErrStr: "[t001] error timeout: test sample error xxx test sample error 2 (./test) (./test)",
wantErrCode: "t001",
wantErrUnwarp: map[string]bool{
testSampleErr.Error(): false,
testErrDef.New(testSampleErr2).Error(): false,
},
wantErrIs: map[error]bool{
testSampleErr: true,
testSampleErr2: true,
testErrDef.New(): true,
testSampleErr3: false,
nil: false,
},
wantStackTrace: "./test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := berror.NewErrDefinition(tt.args.format, tt.args.opts...)
err := d.New(tt.args.args...)
gotWantRawErrStr := err.RawError()
if tt.wantRawErrStr != gotWantRawErrStr {
t.Fatal(fmt.Sprintf("expected wantRawErrStr: %v, got: %v", tt.wantRawErrStr, gotWantRawErrStr))
}
gotErrStr := err.Error()
if tt.wantErrStr != gotErrStr {
t.Fatal(fmt.Sprintf("expected wantErrStr: %v, got: %v", tt.wantErrStr, gotErrStr))
}
gotErrCode := err.Code()
if tt.wantErrCode != gotErrCode {
t.Fatal(fmt.Sprintf("expected wantErrCode: %v, got: %v", tt.wantErrCode, gotErrCode))
}
for _, e := range err.Unwrap() {
_, ok := tt.wantErrUnwarp[e.Error()]
if ok {
tt.wantErrUnwarp[e.Error()] = true
} else {
t.Fatal(fmt.Sprintf("err: %v is not expected by wantErrUnwarp", e.Error()))
}
}
for k, v := range tt.wantErrUnwarp {
if !v {
t.Fatal(fmt.Sprintf("err: %v is expected by wantErrUnwarp", k))
}
}
for k, v := range tt.wantErrIs {
gotErrIs := errors.Is(err, k)
if gotErrIs != v {
t.Fatal(fmt.Sprintf("err: %v is expected to be %v by wantErrIs, got: %v", k, v, gotErrIs))
}
gotErrIs = err.Is(k)
if gotErrIs != v {
t.Fatal(fmt.Sprintf("parent err: %v is expected to be %v by wantErrIs, got: %v", k, v, gotErrIs))
}
}
if !d.Is(err) || err.ErrorDefinition() != d {
t.Fatal("error definition check fail")
}
gotStackTrace := err.StackTrace()
if tt.wantStackTrace != gotStackTrace {
t.Fatal(fmt.Sprintf("expected stack trace %v, got: %v", tt.wantStackTrace, gotStackTrace))
}
gotString := fmt.Sprintf("%s", err)
if err.String() != gotString {
t.Fatal(fmt.Sprintf("expected err String(): %v, got: %v", err.Error(), gotString))
}
})
}
}