-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherrors_test.go
238 lines (182 loc) · 5.38 KB
/
errors_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
228
229
230
231
232
233
234
235
236
237
238
package errors
import (
"errors"
"fmt"
"io"
"testing"
cockroachdb "github.com/cockroachdb/errors"
"github.com/stretchr/testify/assert"
)
func TestError_Error(t *testing.T) {
t.Run("should return the error in string form", func(t *testing.T) {
err := New(Kind("testing"), "oops")
assert.Equal(t, "[go-errors.TestError_Error.func1]: oops (testing)", err.Error())
})
}
func TestError_Format(t *testing.T) {
t.Run("should return the error in string form if formatted with %v", func(t *testing.T) {
err := New(Kind("testing"), "oops")
assert.Equal(t, "[go-errors.TestError_Format.func1]: oops (testing)", fmt.Sprintf("%v", err))
})
t.Run("should return the error in string form if formatted with %+v", func(t *testing.T) {
err := Wrap(io.EOF, Kind("testing"), "oops").WithFields("foo", "bar")
assert.Contains(t, fmt.Sprintf("%+v", err), "[go-errors.TestError_Format.func2]: oops (testing)")
assert.Contains(t, fmt.Sprintf("%+v", err), "\n")
assert.Contains(t, fmt.Sprintf("%+v", err), "File: ")
assert.Contains(t, fmt.Sprintf("%+v", err), ", line ")
assert.Contains(t, fmt.Sprintf("%+v", err), "foo")
assert.Contains(t, fmt.Sprintf("%+v", err), "bar")
})
}
func TestError_WithFields(t *testing.T) {
t.Run("should attach the given fields to the error", func(t *testing.T) {
err := New("oops").WithFields("foo", "bar", "baz", "qux")
assert.Len(t, err.Fields, 2)
_, fooOK := err.Fields["foo"]
_, bazOK := err.Fields["foo"]
assert.True(t, fooOK)
assert.True(t, bazOK)
})
t.Run("should panic if an odd number of arguments is given", func(t *testing.T) {
assert.Panics(t, func() {
_ = New("oops").WithFields("hello")
})
})
t.Run("should panic if a non-string value is given as a key", func(t *testing.T) {
assert.Panics(t, func() {
_ = New("oops").WithFields(1234, 1234)
})
})
}
func TestError_WithField(t *testing.T) {
t.Run("should attach the given field to the error", func(t *testing.T) {
err := New("oops").WithField("foo", "bar")
assert.Len(t, err.Fields, 1)
_, fooOK := err.Fields["foo"]
assert.True(t, fooOK)
})
}
func TestNew(t *testing.T) {
t.Run("should not return nil", func(t *testing.T) {
assert.NotNil(t, New("oops"))
})
t.Run("should panic if no arguments are passed", func(t *testing.T) {
assert.Panics(t, func() {
_ = New()
})
})
t.Run("should assign the given kind", func(t *testing.T) {
kind := Kind("testing 1")
err := New(kind)
assert.Equal(t, kind, err.Kind)
})
t.Run("should assign the given message", func(t *testing.T) {
message := "testing"
err := New(message)
assert.Equal(t, message, err.Message)
})
t.Run("should assign the given standard error cause", func(t *testing.T) {
cause := errors.New("oops")
err := New(cause)
assert.Equal(t, cause, err.Cause)
})
t.Run("should assign the given go-errors error cause ", func(t *testing.T) {
cause := New("oops")
err := New(cause)
assert.Equal(t, cause, err.Cause)
})
t.Run("should assign the given fields", func(t *testing.T) {
err := New(map[string]interface{}{
"foo": "bar",
"baz": "qux",
})
assert.Len(t, err.Fields, 2)
})
t.Run("should panic if the given cause is a nil *Error", func(t *testing.T) {
var cause *Error
assert.Panics(t, func() {
_ = New(cause)
})
})
t.Run("should panic if the given argument is of an unexpected type", func(t *testing.T) {
assert.Panics(t, func() {
_ = New(123)
})
})
}
func TestWrap(t *testing.T) {
cause := New("oops")
t.Run("should return nil if the given cause is nil", func(t *testing.T) {
assert.Nil(t, Wrap(nil))
})
t.Run("should not return nil", func(t *testing.T) {
assert.NotNil(t, Wrap(cause, "oops"))
})
t.Run("should assign the given kind", func(t *testing.T) {
kind := Kind("testing 1")
err := Wrap(cause, kind)
assert.Equal(t, kind, err.Kind)
})
t.Run("should assign the given message", func(t *testing.T) {
message := "testing"
err := Wrap(cause, message)
assert.Equal(t, message, err.Message)
})
t.Run("should assign the given standard error cause", func(t *testing.T) {
cause := errors.New("oops")
err := Wrap(cause)
assert.Equal(t, cause, err.Cause)
})
t.Run("should assign the given go-errors error cause ", func(t *testing.T) {
cause := New("oops")
err := Wrap(cause)
assert.Equal(t, cause, err.Cause)
})
t.Run("should assign the given fields", func(t *testing.T) {
err := Wrap(cause, map[string]interface{}{
"foo": "bar",
"baz": "qux",
})
assert.Len(t, err.Fields, 2)
})
t.Run("should panic if the given cause is a nil *Error", func(t *testing.T) {
var cause2 *Error
assert.Panics(t, func() {
_ = Wrap(cause, cause2)
})
})
t.Run("should panic if the given argument is of an unexpected type", func(t *testing.T) {
assert.Panics(t, func() {
_ = Wrap(cause, 123)
})
})
}
func BenchmarkNew(b *testing.B) {
var err error
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = New("benchmarking")
}
_ = err
}
func BenchmarkNewCockroachDB(b *testing.B) {
var err error
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// We automatically add a stack trace when creating or wrapping an error, rather than
// creating another layer of error.
err = cockroachdb.WithStack(cockroachdb.New("benchmarking"))
}
_ = err
}
func BenchmarkNewStd(b *testing.B) {
var err error
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = errors.New("benchmarking")
}
_ = err
}