-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathxerrors_test.go
More file actions
236 lines (222 loc) · 6.19 KB
/
xerrors_test.go
File metadata and controls
236 lines (222 loc) · 6.19 KB
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
228
229
230
231
232
233
234
235
236
package xerrors
import (
"errors"
"fmt"
"strings"
"testing"
)
type stringer struct{ s string }
func (s stringer) String() string {
return s.s
}
func TestMessage(t *testing.T) {
tests := []struct {
val string
want string
}{
{val: "", want: ""},
{val: "foo", want: "foo"},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
got1 := Message(tt.val)
got2 := Message(tt.val)
if msg := got1.Error(); msg != tt.want {
t.Errorf("Message(%#v): got: %q, want %q", tt.val, msg, tt.want)
}
if len(StackTrace(got1)) != 0 {
t.Errorf("Message(%#v): returned error must not contain a stack trace", tt.val)
}
if got1 == got2 {
t.Errorf("Message(%#v): returned error must not be the same instance", tt.val)
}
})
}
}
func TestMessagef(t *testing.T) {
tests := []struct {
format string
args []any
want string
}{
{format: "", args: nil, want: ""},
{format: "foo", args: nil, want: "foo"},
{format: "foo %d", args: []any{42}, want: "foo 42"},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
got1 := Messagef(tt.format, tt.args...)
got2 := Messagef(tt.format, tt.args...)
if msg := got1.Error(); msg != tt.want {
t.Errorf("Messagef(%q, %#v): got: %q, want %q", tt.format, tt.args, msg, tt.want)
}
if len(StackTrace(got1)) != 0 {
t.Errorf("Messagef(%q, %#v): returned error must not contain a stack trace", tt.format, tt.args)
}
if got1 == got2 {
t.Errorf("Messagef(%q, %#v): returned error must not be the same instance", tt.format, tt.args)
}
})
}
}
func TestNew(t *testing.T) {
// Since New is mostly a wrapper around Join, we only test
// the error message and stack trace.
tests := []struct {
vals []any
want string
wantNil bool
}{
{vals: []any{"foo", "bar"}, want: "foo: bar"},
{vals: []any{nil}, wantNil: true},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
got := New(tt.vals...)
switch {
case tt.wantNil:
if got != nil {
t.Errorf("New(%#v): expected nil", tt.vals)
}
default:
if got.Error() != tt.want {
t.Errorf("New(%#v): got: %q, want %q", tt.vals, got, tt.want)
}
st := StackTrace(got)
if len(st) == 0 {
t.Errorf("New(%#v): returned error must contain a stack trace", tt.vals)
return
}
if !strings.Contains(st.Frames()[0].Function, "TestNew") {
t.Errorf("New(%#v): first frame must point to TestNew", tt.vals)
}
}
})
}
}
func TestNewf(t *testing.T) {
// Since Newf is mostly a wrapper around Joinf, we only test
// the error message and stack trace.
tests := []struct {
format string
args []any
want string
}{
{format: "foo", args: nil, want: "foo"},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
got := Newf(tt.format, tt.args...)
if got.Error() != tt.want {
t.Errorf("Newf(%q, %#v): got: %q, want %q", tt.format, tt.args, got, tt.want)
}
st := StackTrace(got)
if len(st) == 0 {
t.Errorf("Newf(%q, %#v): returned error must contain a stack trace", tt.format, tt.args)
return
}
if !strings.Contains(st.Frames()[0].Function, "TestNewf") {
t.Errorf("Newf(%q, %#v): first frame must point to TestNewf", tt.format, tt.args)
}
})
}
}
func TestJoin(t *testing.T) {
tests := []struct {
vals []any
want string
wantNil bool
}{
// String
{vals: []any{""}, want: ""},
{vals: []any{"foo", "bar"}, want: "foo: bar"},
// Error
{vals: []any{Message("foo"), Message("bar")}, want: "foo: bar"},
// Stringer
{vals: []any{stringer{s: "foo"}, stringer{s: "bar"}}, want: "foo: bar"},
// Sprintf
{vals: []any{42, 314}, want: "42: 314"},
// Nil cases
{vals: []any{}, wantNil: true},
{vals: []any{nil}, wantNil: true},
{vals: []any{nil, nil}, wantNil: true},
{vals: []any{nil, "foo", "bar"}, want: "foo: bar"},
{vals: []any{"foo", nil, "bar"}, want: "foo: bar"},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
got := Join(tt.vals...)
switch {
case tt.wantNil:
if got != nil {
t.Errorf("Join(%#v): expected nil", tt.vals)
}
default:
if got.Error() != tt.want {
t.Errorf("Join(%#v): got: %q, want %q", tt.vals, got, tt.want)
}
if len(StackTrace(got)) != 0 {
t.Errorf("Join(%#v): returned error must not contain a stack trace", tt.vals)
}
for _, v := range tt.vals {
if err, ok := v.(error); ok {
if !errors.Is(got, err) {
t.Errorf("errors.Is(Join(errs...), err): must return true")
}
}
}
}
})
}
}
func TestJoinf(t *testing.T) {
err := Message("first error")
tests := []struct {
format string
args []any
want string
}{
{format: "simple error", args: nil, want: "simple error"},
{format: "error with value %d", args: []any{42}, want: "error with value 42"},
{format: "wrapped error: %w", args: []any{err}, want: "wrapped error: first error"},
{format: "wrapped nil error: %w", args: []any{nil}, want: "wrapped nil error: %!w(<nil>)"},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
got := Joinf(tt.format, tt.args...)
if got == nil {
t.Errorf("Joinf(%q, %#v): expected non-nil error", tt.format, tt.args)
return
}
if got.Error() != tt.want {
t.Errorf("Joinf(%q, %#v): got: %q, want %q", tt.format, tt.args, got, tt.want)
}
if len(StackTrace(got)) != 0 {
t.Errorf("Joinf(%q, %#v): returned error must not contain a stack trace", tt.format, tt.args)
}
for _, v := range tt.args {
if err, ok := v.(error); ok {
if !errors.Is(got, err) {
t.Errorf("errors.Is(Joinf(errs...), err): must return true")
}
}
}
})
}
}
func TestJoin_Unwrap(t *testing.T) {
err1 := Message("first error")
err2 := Message("second error")
got := Join(err1, err2)
unwrapper, ok := got.(interface{ Unwrap() error })
if !ok {
t.Fatalf("Join(err1, err2) must implement Unwrap()")
}
unwrapped := unwrapper.Unwrap()
if unwrapped == nil {
t.Fatalf("Join(err1, err2).Unwrap() must not return nil")
}
if errors.Is(unwrapped, err1) || !errors.Is(unwrapped, err2) {
t.Fatalf("Join(err1, err2).Unwrap() must return the second error")
}
}