-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror_test.go
182 lines (173 loc) · 3.94 KB
/
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package cerrors_test
import (
"errors"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/1debit/cerrors"
)
func TestUnwrapMessage(t *testing.T) {
tests := []struct {
name string
err error
result string
}{
{
name: "at the end with a colon",
err: fmt.Errorf("one: %w", fmt.Errorf("two: %w", errors.New("three"))),
result: "one",
},
{
name: "at the end without colon",
err: fmt.Errorf("one %w", fmt.Errorf("two: %w", errors.New("three"))),
result: "one",
},
{
name: "in the middle",
err: fmt.Errorf("foo %w bar", fmt.Errorf("two: %w", errors.New("three"))),
result: "foo * bar",
},
{
name: "chained without wrapping",
err: fmt.Errorf("one: %v", fmt.Errorf("two: %w", errors.New("three"))),
result: "one: two: three",
},
{
name: "not chained",
err: errors.New("foo bar"),
result: "foo bar",
},
{
name: "empty",
err: errors.New(""),
result: "",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.result, cerrors.UnwrapMessage(test.err))
})
}
}
func TestCerrorFormat(t *testing.T) {
var (
location = ` \S*error_test.go:\d+`
locationIndented = ` \S*error_test.go:\d+`
function = ` github.com/1debit/cerrors_test.TestCerrorFormat.*`
)
tests := []struct {
name string
chain []func(error) error
verb string
root error
result []string
}{
{
name: "single error, short",
root: cerrors.New("foo"),
verb: "%v",
result: []string{"foo"},
},
{
name: "multiple levels wrapped, short",
root: cerrors.New("one"),
chain: []func(error) error{
func(e error) error { return cerrors.Wrap(e, "two") },
func(e error) error { return cerrors.Wrap(e, "three") },
},
verb: "%v",
result: []string{"three: two: one"},
},
{
name: "including non-cerror, short",
root: cerrors.New("one"),
chain: []func(error) error{
func(e error) error { return fmt.Errorf("two: %w", e) },
func(e error) error { return cerrors.Wrap(e, "three") },
},
verb: "%v",
result: []string{"three: two: one"},
},
{
name: "single error, detailed",
root: cerrors.New("foo"),
verb: "%+v",
result: []string{
"foo",
location,
},
},
{
name: "multiple levels wrapped, detailed",
root: cerrors.New("one"),
chain: []func(error) error{
func(e error) error { return cerrors.Wrap(e, "two") },
func(e error) error { return cerrors.Wrap(e, "three") },
},
verb: "%+v",
result: []string{
"three",
location,
" - two",
location,
" - one",
location,
},
},
{
name: "including non-cerror, detailed",
root: cerrors.New("one"),
chain: []func(error) error{
func(e error) error { return fmt.Errorf("two: %w", e) },
func(e error) error { return cerrors.Wrap(e, "three") },
},
verb: "%+v",
result: []string{
"three",
location,
" - two",
" - one",
location,
},
},
{
name: "including non-cerror, detailed with functions",
root: cerrors.New("one"),
chain: []func(error) error{
func(e error) error { return fmt.Errorf("two: %w", e) },
func(e error) error { return cerrors.Wrap(e, "three") },
},
verb: "%#v",
result: []string{
"three",
function,
locationIndented,
" - two",
" - one",
function,
locationIndented,
},
},
{
name: "invalid verb",
root: cerrors.New("one"),
verb: "%d",
result: []string{`%!d\(cerror\)`},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.root
for _, f := range test.chain {
err = f(err)
}
result := strings.Split(strings.TrimSpace(fmt.Sprintf(test.verb, err)), "\n")
require.Len(t, result, len(test.result))
for idx, line := range test.result {
assert.Regexp(t, "^"+line+"$", result[idx], "at line %d", idx)
}
})
}
}