-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_test.go
159 lines (129 loc) · 3.78 KB
/
match_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
// 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"
"os"
"reflect"
"testing"
"github.com/go-pogo/errors/internal"
"github.com/stretchr/testify/assert"
)
type errChainHelper []error
//goland:noinspection GoMixedReceiverTypes
func (h *errChainHelper) append(err error) error {
*h = append(*h, err)
return err
}
//goland:noinspection GoMixedReceiverTypes
func (h *errChainHelper) prepend(err error) error {
*h = append([]error{err}, *h...)
return err
}
//goland:noinspection GoMixedReceiverTypes
func (h errChainHelper) last() error { return h[len(h)-1] }
// test if the root cause error matches all wrapping errors in the chain
func TestIs(t *testing.T) {
baseErr := New("root cause")
wrapErr := Wrap(baseErr, "its a wrap")
stdBase := stderrors.New("root cause")
stdWrap := fmt.Errorf("error: %w", stdBase)
chains := map[string]errChainHelper{
"base": {baseErr},
"wrap": {baseErr, wrapErr},
"std error": {stdBase},
"std wrap": {stdBase, stdWrap},
"std with stack": {stdBase, WithStack(stdBase)},
}
for group, wrapFn := range provideEmbedders() {
t.Run(group, func(t *testing.T) {
for name, chain := range chains {
t.Run(name, func(t *testing.T) {
// pass the last error to the function we'd like to test
err := chain.append(wrapFn(chain.last()))
assert.Same(t, chain[0], Cause(err))
for i, target := range chain {
for j := i; j < len(chain); j++ {
err = chain[j]
assert.ErrorIs(t, err, target)
}
}
})
}
})
}
t.Run("manual", func(t *testing.T) {
cause := stderrors.New("root cause")
wrapped := Wrap(cause, Msg("second"))
withFormatter := WithFormatter(wrapped)
// both upgrades should match with the original error
assert.ErrorIs(t, wrapped, cause)
assert.ErrorIs(t, withFormatter, cause)
// both upgrades should match with each other
assert.ErrorIs(t, wrapped, withFormatter)
assert.ErrorIs(t, withFormatter, wrapped)
})
t.Run("multi", func(t *testing.T) {
internal.DisableTraceStack()
defer internal.EnableTraceStack()
err1 := stderrors.New("some err")
err2 := New("whoops")
multi := newMultiErr([]error{err2, err1}, 0)
assert.True(t, Is(multi, err1))
assert.True(t, Is(multi, err2))
assert.False(t, Is(multi, stderrors.New("some err")))
})
}
type customError struct{}
func (ce *customError) Error() string { return "this is a custom error" }
func TestAs(t *testing.T) {
internal.DisableTraceStack()
defer internal.EnableTraceStack()
var customErr *customError
var pathErrPtr *os.PathError
_, pathErr := os.Open("non-existing")
tests := map[string]struct {
error error
target interface{}
wantFn func(err error) interface{}
}{
"traced os.PathError": {
error: WithStack(pathErr),
target: &pathErrPtr,
wantFn: func(err error) interface{} {
return pathErr
},
},
"traced custom error": {
error: WithStack(&customError{}),
target: &customErr,
wantFn: func(err error) interface{} {
return &customError{}
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
val := reflect.ValueOf(tc.target)
val.Elem().Set(reflect.Zero(reflect.TypeOf(tc.target).Elem()))
assert.True(t, As(tc.error, tc.target))
got := val.Elem().Interface()
want := tc.wantFn(tc.error)
assert.Equal(t, want, got)
})
}
t.Run("multi", func(t *testing.T) {
internal.DisableTraceStack()
defer internal.EnableTraceStack()
err1 := stderrors.New("some err")
err2 := New("whoops")
multi := newMultiErr([]error{err2, err1}, 0)
var have commonError
assert.True(t, As(multi, &have))
assert.Equal(t, err2, &have)
var have2 Msg
assert.False(t, As(multi, &have2))
})
}