-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsentinel_error_test.go
158 lines (139 loc) · 3.46 KB
/
sentinel_error_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
package serrors
import (
"errors"
"fmt"
"testing"
)
const (
err = String("err")
errFmt = String("err %s")
)
func TestTypedError_Is(t *testing.T) {
type args struct {
err error
}
var (
cause = errors.New("cause")
wrapped = err.New().Wrap(cause)
nested = errFmt.New("test").Wrap(wrapped)
)
tests := []struct {
name string
e error
args args
want bool
}{
{"same error", err.New(), args{err}, true},
{"same error with args", errFmt.New("formattedError"), args{errFmt}, true},
{"error has different type but same text", err.New(), args{errors.New("err")}, false},
{"wrapped error", wrapped, args{err}, true},
{"wrapped error check cause", wrapped, args{cause}, true},
{"nested wrapped error", nested, args{cause}, true},
{"nested wrapped error", nested, args{err}, true},
{"nested wrapped error", nested, args{errors.New("cause")}, false},
{"different errors", err.New(), args{String("other")}, false},
{"error was cause", wrapped, args{fmt.Errorf("caused by %w", err)}, false},
{"error is nil", err.New(), args{nil}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := errors.Is(tt.e, tt.args.err); got != tt.want {
t.Errorf("Is() = %v, want %v", got, tt.want)
}
})
}
}
func Test_Wrap(t *testing.T) {
type args struct {
err Error
cause error
}
var (
cause = errors.New("root")
)
tests := []struct {
name string
args args
want string
}{
{"error", args{err.New(), cause}, "err: root"},
{"error with args", args{errFmt.New("formattedError"), cause}, "err formattedError: root"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.args.err.Wrap(tt.args.cause); got.Error() != tt.want {
t.Errorf("Wrap() = %v, want %v", got.Error(), tt.want)
}
})
}
}
func TestTypedError_Unwrap(t *testing.T) {
var cause = errors.New("cause")
tests := []struct {
name string
e wrapped
want error
}{
{"without cause", err.New().Wrap(nil), nil},
{"with cause", err.New().Wrap(cause), cause},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.e.Unwrap(); got != tt.want {
t.Errorf("Unwrap() = %v, want %v", got, tt.want)
}
})
}
}
func TestTypedError_As(t *testing.T) {
var (
chain = fmt.Errorf("second: %w", fmt.Errorf("first: %w", err.New()))
chainFmt = fmt.Errorf("second: %w", fmt.Errorf("first: %w", errFmt.New()))
)
tests := []struct {
name string
e error
want error
}{
{"error chain", chain, err},
{"error chain fmt", chainFmt, errFmt},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var err Comparer
if got := errors.As(tt.e, &err); !got || err.Error() != tt.want.Error() {
t.Errorf("errors.As() = %v; want %v", got, tt.want)
}
})
}
}
func BenchmarkWrap(b *testing.B) {
const strerr = String("error %f")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var err = errors.New("cause")
err = strerr.New().Wrap(err)
_ = err.Error()
_ = errors.Is(err, strerr)
}
}
func BenchmarkWrapWithStack(b *testing.B) {
const strerr = String("error")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var err = errors.New("cause")
err = strerr.New().WithStack()
_ = err.Error()
_ = errors.Is(err, strerr)
}
}
func BenchmarkErrorfWrap(b *testing.B) {
var strerr = errors.New("error")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var err = errors.New("cause")
err = fmt.Errorf("%s: %w", strerr, err)
_ = err.Error()
_ = errors.Is(err, strerr)
}
}