-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap_test.go
219 lines (196 loc) · 5.06 KB
/
wrap_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
// Copyright (c) 2020, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package errors
import (
stderrors "errors"
"fmt"
"testing"
"github.com/go-pogo/errors/internal"
"github.com/stretchr/testify/assert"
)
func TestWrapWrapf(t *testing.T) {
internal.DisableTraceStack()
defer internal.EnableTraceStack()
tests := map[string]struct {
cause error
message, format string
args []interface{}
}{
"with std error": {
cause: stderrors.New("some err"),
message: "foobar",
format: "%s",
args: []interface{}{"foobar"},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
wrap := Wrap(tc.cause, tc.message)
assert.Exactly(t, tc.cause, Unwrap(wrap))
wrapf := Wrapf(tc.cause, tc.format, tc.args...)
assert.Exactly(t, wrap.Error(), wrapf.Error())
})
}
t.Run("with nil cause", func(t *testing.T) {
assert.Nil(t, Wrap(nil, "foobar"))
})
}
func TestWrap(t *testing.T) {
internal.DisableTraceStack()
defer internal.EnableTraceStack()
t.Run("with nil cause", func(t *testing.T) {
assert.Nil(t, Wrap(nil, "foobar"))
})
cause := stderrors.New("the cause")
str := "my error message"
msg := Msg(str)
tests := map[string]interface{}{
"with Msg": msg,
"with *Msg": &msg,
"with string": str,
"with *string": &str,
}
for name, input := range tests {
t.Run(name, func(t *testing.T) {
//goland:noinspection GoTypeAssertionOnErrors
have := Wrap(cause, input).(*commonError)
assert.Equal(t, msg, have.error)
assert.Same(t, cause, Unwrap(have))
assert.Same(t, have, Unembed(have))
})
}
tests = map[string]interface{}{
"int": 10,
"bool": false,
"*errors.errorString": stderrors.New("not supported"),
}
t.Run("unsupported type", func(t *testing.T) {
for typ, input := range tests {
t.Run(typ, func(t *testing.T) {
assert.PanicsWithValue(t,
unsupportedType("errors.Wrap", typ),
func() { _ = Wrap(cause, input) },
)
})
}
})
}
func TestWrapf(t *testing.T) {
t.Run("with nil cause", func(t *testing.T) {
assert.Nil(t, Wrapf(nil, "%s", "foobar"))
})
}
func TestUnwrapAll(t *testing.T) {
tests := map[string]func(chain *errChainHelper) error{
"std error": func(chain *errChainHelper) error {
return chain.prepend(stderrors.New("foo bar"))
},
"traced std error": func(chain *errChainHelper) error {
err := chain.prepend(stderrors.New("bar: baz"))
return chain.prepend(WithStack(err))
},
"std wrap": func(chain *errChainHelper) error {
err := chain.prepend(stderrors.New("foo bar"))
return chain.prepend(fmt.Errorf("cause: %w", err))
},
"traced std wrap": func(chain *errChainHelper) error {
err := chain.prepend(stderrors.New("foo bar"))
wrap := chain.prepend(fmt.Errorf("cause: %w", err))
return chain.prepend(WithStack(wrap))
},
"error": func(chain *errChainHelper) error {
return chain.prepend(New("err msg"))
},
"traced error": func(chain *errChainHelper) error {
err := chain.prepend(New("err msg"))
return WithStack(err)
},
"wrapped error": func(chain *errChainHelper) error {
err := chain.prepend(New("qux"))
return chain.prepend(Wrap(err, "bar msg"))
},
}
for name, setupFn := range tests {
t.Run(name, func(t *testing.T) {
var chain errChainHelper
err := setupFn(&chain)
have := UnwrapAll(err)
assert.Equal(t, len(chain), len(have))
assert.Exactly(t, []error(chain), have)
})
}
t.Run("nil", func(t *testing.T) {
assert.Exactly(t, []error{}, UnwrapAll(nil))
})
}
func TestCause(t *testing.T) {
tests := map[string]struct {
want error
setup func(e error) error
}{
"std error": {
want: stderrors.New("foo bar"),
setup: func(e error) error { return e },
},
"traced std error": {
want: stderrors.New("foo bar"),
setup: func(e error) error { return WithStack(e) },
},
"std wrap": {
want: stderrors.New("foo bar"),
setup: func(e error) error {
return fmt.Errorf("%w", e)
},
},
"traced std wrap": {
want: stderrors.New("baz"),
setup: func(e error) error {
return WithStack(fmt.Errorf("cause: %w", e))
},
},
"error": {
want: New("xoo"),
setup: func(e error) error { return e },
},
"embedded error": {
want: New("xoo"),
setup: func(e error) error { return WithExitCode(e, 1) },
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
have := Cause(tc.setup(tc.want))
assert.Same(t, tc.want, have)
})
}
}
func TestIsCause(t *testing.T) {
tests := map[string]struct {
err error
want bool
}{
"std error": {
err: stderrors.New("foo bar"),
want: true,
},
"wrapped std error": {
err: Wrap(stderrors.New("foo bar"), "wrap"),
},
"wrap fmt error": {
err: fmt.Errorf("foo bar: %w", stderrors.New("baz")),
},
"error": {
err: New("foo bar"),
want: true,
},
"wrapped error": {
err: Wrap(stderrors.New("foo bar"), "wrap"),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.want, IsCause(tc.err))
})
}
}