-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
190 lines (164 loc) · 3.8 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
package errors_test
import (
"fmt"
"io/fs"
"runtime"
"testing"
"github.com/pierrre/assert"
. "github.com/pierrre/errors"
"github.com/pierrre/errors/errbase"
"github.com/pierrre/errors/errstack"
)
func ExampleNew() {
err := New("error")
fmt.Println(err)
// Output: error
}
func ExampleWrap() {
err := errbase.New("error")
err = Wrap(err, "wrap")
fmt.Println(err)
// Output: wrap: error
}
func TestNew(t *testing.T) {
err := New("error")
assert.ErrorEqual(t, err, "error")
sfs := errstack.Frames(err)
assert.SliceLen(t, sfs, 1)
}
func TestNewf(t *testing.T) {
err := Newf("error %d", 1)
assert.ErrorEqual(t, err, "error 1")
sfs := errstack.Frames(err)
assert.SliceLen(t, sfs, 1)
}
func TestReportGlobalInitPanics(t *testing.T) {
assert.Panics(t, func() {
ReportGlobalInit(New("error"))
})
}
var errGlobal = errstack.Wrap(errbase.New("global error"))
func TestCheckGlobalInit(t *testing.T) {
called := false
CheckGlobalInit(errGlobal, func(err error) {
called = true
assert.ErrorEqual(t, err, "global error initialization detected, use errbase.New() instead, see https://pkg.go.dev/github.com/pierrre/errors#ReportGlobalInit : global error")
})
assert.True(t, called)
}
func TestCheckGlobalInitNoStack(t *testing.T) {
err := errbase.New("error")
CheckGlobalInit(err, nil)
}
func TestCheckGlobalInitEmptyPCs(t *testing.T) {
err := errstack.WrapSkip(errbase.New("error"), 1000)
CheckGlobalInit(err, nil)
}
func TestCheckGlobalInitNotInit(t *testing.T) {
err := New("error")
CheckGlobalInit(err, nil)
}
func TestWrap(t *testing.T) {
err := errbase.New("error")
err = Wrap(err, "test")
assert.ErrorEqual(t, err, "test: error")
sfs := errstack.Frames(err)
assert.SliceLen(t, sfs, 1)
}
func TestWrapf(t *testing.T) {
err := errbase.New("error")
err = Wrapf(err, "test %d", 1)
assert.ErrorEqual(t, err, "test 1: error")
sfs := errstack.Frames(err)
assert.SliceLen(t, sfs, 1)
}
func TestAs(t *testing.T) {
err := errbase.New("error")
err = &fs.PathError{Err: err}
err = Wrap(err, "test")
var pathError *fs.PathError
ok := As(err, &pathError)
assert.True(t, ok)
}
func TestIs(t *testing.T) {
errBase := errbase.New("error")
err := Wrap(errBase, "test")
ok := Is(err, errBase)
assert.True(t, ok)
}
func TestJoin(t *testing.T) {
err := Join(New("error 1"), New("error 2"))
err = Unwrap(err) // Remove the stack.
errUnwrap, _ := assert.Type[interface {
Unwrap() []error
}](t, err)
errs := errUnwrap.Unwrap()
assert.SliceLen(t, errs, 2)
}
func TestUnwrap(t *testing.T) {
errBase := errbase.New("error")
err := Wrap(errBase, "test")
err = Unwrap(err)
err = Unwrap(err)
assert.Equal(t, err, errBase)
}
func TestNewAllocs(t *testing.T) {
var res error
assert.AllocsPerRun(t, 100, func() {
res = New("error")
}, 3)
runtime.KeepAlive(res)
}
func TestNewfAllocs(t *testing.T) {
var res error
assert.AllocsPerRun(t, 100, func() {
res = Newf("error %d", 1)
}, 4)
runtime.KeepAlive(res)
}
func TestWrapAllocs(t *testing.T) {
err := errbase.New("error")
var res error
assert.AllocsPerRun(t, 100, func() {
res = Wrap(err, "test")
}, 4)
runtime.KeepAlive(res)
}
func TestWrapfAllocs(t *testing.T) {
err := errbase.New("error")
var res error
assert.AllocsPerRun(t, 100, func() {
res = Wrapf(err, "test %d", 1)
}, 5)
runtime.KeepAlive(res)
}
func BenchmarkNew(b *testing.B) {
var res error
for range b.N {
res = New("error")
}
runtime.KeepAlive(res)
}
func BenchmarkNewf(b *testing.B) {
var res error
for range b.N {
res = Newf("error %d", 1)
}
runtime.KeepAlive(res)
}
func BenchmarkWrap(b *testing.B) {
err := errbase.New("error")
var res error
for range b.N {
res = Wrap(err, "test")
}
runtime.KeepAlive(res)
}
func BenchmarkWrapf(b *testing.B) {
err := errbase.New("error")
var res error
for range b.N {
res = Wrapf(err, "test %d", 1)
}
runtime.KeepAlive(res)
}