-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmatch_test.go
More file actions
184 lines (138 loc) · 3.5 KB
/
match_test.go
File metadata and controls
184 lines (138 loc) · 3.5 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
package match
import (
"errors"
"testing"
)
func TestAny(t *testing.T) {
t.Run("empty", func(t *testing.T) {
matcher := Any{}
if matcher.MatchError(errors.New("error")) {
t.Error("empty any matcher is not supposed to match an error")
}
})
t.Run("not_empty", func(t *testing.T) {
matcher := Any{
ErrorMatcherFunc(func(err error) bool { return false }),
ErrorMatcherFunc(func(err error) bool { return true }),
}
if !matcher.MatchError(errors.New("error")) {
t.Error("not-empty any matcher is supposed to match an error if any of the matchers match it")
}
})
}
func TestAll(t *testing.T) {
t.Run("empty", func(t *testing.T) {
matcher := All{}
if !matcher.MatchError(errors.New("error")) {
t.Error("empty all matcher is not supposed to match an error")
}
})
t.Run("not_empty", func(t *testing.T) {
matcher := All{
ErrorMatcherFunc(func(err error) bool { return false }),
ErrorMatcherFunc(func(err error) bool { return true }),
}
if matcher.MatchError(errors.New("error")) {
t.Error("not-empty all matcher is not supposed to match an error if not all of the matchers match it")
}
})
}
func TestIs(t *testing.T) {
err := errors.New("error")
matcher := Is(err)
if !matcher.MatchError(err) {
t.Error("is matcher is supposed to match an error if errors.Is returns true")
}
}
type asErrorStub struct{}
func (asErrorStub) Error() string {
return "error"
}
func (asErrorStub) IsError() bool {
return true
}
func TestAs(t *testing.T) {
var matchErr interface {
IsError() bool
}
matcher := As(&matchErr)
if !matcher.MatchError(asErrorStub{}) {
t.Error("is matcher is supposed to match an error if errors.Is returns true")
}
}
type errorData struct {
data string
}
func (e errorData) Error() string {
return e.data
}
type wrappingError struct {
error
}
func (w wrappingError) Unwrap() error {
return w.error
}
func TestAs_SetMatchedError(t *testing.T) {
var matchErr errorData
matcher := As(&matchErr)
matchingErr := wrappingError{error: errorData{"target data"}}
if !matcher.MatchError(matchingErr) {
t.Error("As matcher is not supposed to match an error that cannot be assigned from the error chain")
}
if matchErr.data != "target data" {
t.Error("As matcher is supposed to set the matched error to it's value in the chain")
}
}
func TestAs_IncompatibleErrors(t *testing.T) {
var matchErr errorData
matcher := As(&matchErr)
if matcher.MatchError(errors.New("error")) {
t.Error("As matcher is not supposed to match an error that cannot be assigned from the error chain")
}
}
func TestAs_Race(t *testing.T) {
var matchErr interface {
IsError() bool
}
matcher := As(&matchErr)
go func() {
if !matcher.MatchError(asErrorStub{}) {
t.Error("is matcher is supposed to match an error if errors.Is returns true")
}
}()
go func() {
if !matcher.MatchError(asErrorStub{}) {
t.Error("is matcher is supposed to match an error if errors.Is returns true")
}
}()
go func() {
if !matcher.MatchError(asErrorStub{}) {
t.Error("is matcher is supposed to match an error if errors.Is returns true")
}
}()
}
func TestAs_Validation(t *testing.T) {
t.Run("nil", func(t *testing.T) {
defer func() {
_ = recover()
}()
As(nil)
t.Error("did not panic")
})
t.Run("non-pointer", func(t *testing.T) {
defer func() {
_ = recover()
}()
var s struct{}
As(s)
t.Error("did not panic")
})
t.Run("non-error", func(t *testing.T) {
defer func() {
_ = recover()
}()
var s struct{}
As(&s)
t.Error("did not panic")
})
}