-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr_wrapper_options_test.go
117 lines (113 loc) · 3.67 KB
/
err_wrapper_options_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
package berror_test
import (
"fmt"
"github.com/bearaujus/berror"
"testing"
)
func Test_err_wrapper_options(t *testing.T) {
type args struct {
opts []berror.ErrDefinitionOption
}
tests := []struct {
name string
args args
wantRawErrStr string
wantErrStr string
wantErrCode string
ignoreErrStrAssert bool
}{
{
name: "test OptionErrDefinitionWithErrCode",
args: args{[]berror.ErrDefinitionOption{
berror.OptionErrDefinitionWithErrCode(testErrorCode),
berror.OptionErrDefinitionWithCustomStackTraceCapturer(stackTraceCapturerMock),
}},
wantRawErrStr: "test",
wantErrStr: "[t001] test (./test)",
wantErrCode: "t001",
ignoreErrStrAssert: false,
},
{
name: "test empty args OptionErrDefinitionWithErrCode",
args: args{[]berror.ErrDefinitionOption{
berror.OptionErrDefinitionWithErrCode(""),
berror.OptionErrDefinitionWithCustomStackTraceCapturer(stackTraceCapturerMock),
}},
wantRawErrStr: "test",
wantErrStr: "test (./test)",
wantErrCode: "",
ignoreErrStrAssert: false,
},
{
name: "test OptionErrDefinitionWithCustomFormater",
args: args{[]berror.ErrDefinitionOption{
berror.OptionErrDefinitionWithErrCode(testErrorCode),
berror.OptionErrDefinitionWithCustomFormater(berror.ErrWrapperFormatterJSON),
berror.OptionErrDefinitionWithCustomStackTraceCapturer(stackTraceCapturerMock),
}},
wantRawErrStr: "test",
wantErrStr: `{"code":"t001","err":"test","stack":"./test"}`,
wantErrCode: "t001",
ignoreErrStrAssert: false,
},
{
name: "test empty args OptionErrDefinitionWithCustomFormater",
args: args{[]berror.ErrDefinitionOption{
berror.OptionErrDefinitionWithCustomFormater(nil),
berror.OptionErrDefinitionWithCustomStackTraceCapturer(stackTraceCapturerMock),
}},
wantRawErrStr: "test",
wantErrStr: "test (./test)",
wantErrCode: "",
ignoreErrStrAssert: false,
},
{
name: "test OptionErrDefinitionWithDisabledStackTrace",
args: args{[]berror.ErrDefinitionOption{
berror.OptionErrDefinitionWithDisabledStackTrace(),
berror.OptionErrDefinitionWithCustomStackTraceCapturer(stackTraceCapturerMock),
}},
wantRawErrStr: "test",
wantErrStr: "test",
wantErrCode: "",
ignoreErrStrAssert: false,
},
{
name: "test OptionErrDefinitionWithCustomStackTraceCapturer",
args: args{[]berror.ErrDefinitionOption{
berror.OptionErrDefinitionWithCustomStackTraceCapturer(stackTraceCapturerMock),
}},
wantRawErrStr: "test",
wantErrStr: "test (./test)",
wantErrCode: "",
ignoreErrStrAssert: false,
},
{
name: "test empty args OptionErrDefinitionWithCustomStackTraceCapturer",
args: args{[]berror.ErrDefinitionOption{
berror.OptionErrDefinitionWithCustomStackTraceCapturer(nil),
}},
wantRawErrStr: "test",
wantErrStr: "test (./test)",
wantErrCode: "",
ignoreErrStrAssert: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := berror.NewErrDefinition("test", tt.args.opts...).New()
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 && !tt.ignoreErrStrAssert {
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))
}
})
}
}