-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
238 lines (221 loc) · 5.96 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package errors
import (
"encoding/json"
"fmt"
"strings"
"testing"
)
var err = fmt.Errorf("test error")
func TestFormat(t *testing.T) {
IncludeBacktrace = false
// %+s check for unwrapped error
e1 := Newf("test")
str := fmt.Sprintf("%+s", e1)
if str != e1.m {
t.Errorf("Error message invalid %s, expected %s", str, e1.m)
}
// %+s check for wrapped error
e2 := Annotatef(e1, "another")
str = fmt.Sprintf("%+s", e2)
val := e2.m + ": " + e2.c.Error()
if str != val {
t.Errorf("Error message invalid %s, expected %s", str, val)
}
// %v check for unwrapped error with trace
IncludeBacktrace = true
e3 := Newf("test1")
str = fmt.Sprintf("%+v", e3)
if !strings.Contains(str, e3.m) {
t.Errorf("Error message %s\n should contain %s", str, e3.m)
}
if !strings.Contains(str, fmt.Sprintf("%+v", e3.t.StackTrace())) {
t.Errorf("Error message %s\n should contain %+v", str, e3.t.StackTrace())
}
}
func TestMarshalJSON(t *testing.T) {
IncludeBacktrace = true
e := Newf("test")
b, err := e.t.StackTrace().MarshalJSON()
if err != nil {
t.Errorf("MarshalJSON failed with error: %+s", err)
}
stack := make([]json.RawMessage, 0)
err = json.Unmarshal(b, &stack)
if err != nil {
t.Errorf("JSON message could not be unmarshaled: %+s", err)
}
if len(stack) != len(e.t) {
t.Errorf("Count of stack frames different after marshaling, expected %d got %d", len(e.t), len(stack))
}
}
func TestAnnotatef(t *testing.T) {
testStr := "Annotatef string"
te := Annotatef(err, testStr)
if te.c != err {
t.Errorf("Invalid parent error %T:%s, expected %T:%s", te.c, te.c, err, err)
}
if te.m != testStr {
t.Errorf("Invalid error message %s, expected %s", te.m, testStr)
}
}
var homeVal = "$HOME"
func TestNewf(t *testing.T) {
testStr := "Newf string"
te := Newf(testStr)
if te.c != nil {
t.Errorf("Invalid parent error %T:%s, expected nil", te.c, te.c)
}
if te.m != testStr {
t.Errorf("Invalid error message %s, expected %s", te.m, testStr)
}
}
func TestErrorf(t *testing.T) {
testStr := "Errorf string"
err := Errorf(testStr)
if te, ok := err.(*Err); ok {
if te.c != nil {
t.Errorf("Invalid parent error %T:%s, expected nil", te.c, te.c)
}
if te.m != testStr {
t.Errorf("Invalid error message %s, expected %s", te.m, testStr)
}
} else {
t.Errorf("Invalid error type returned %T, expected type %T", err, &Err{})
}
}
/*
func TestErr_As(t *testing.T) {
e := Err{m: "test", l: 11, f: "random", t: []uintptr{0x6, 0x6, 0x6}, c: fmt.Errorf("ttt")}
if e.As(&err) {
t.Errorf("%T should not be assertable as %T", err, e)
}
type clone = Err
e1 := clone{}
if !e.As(&e1) {
t.Errorf("%T should be assertable as %T", e, e1)
}
if e1.m != e.m {
t.Errorf("%T message should equal %T's, received %s, expected %s", e1, e, e1.m, e.m)
}
if e1.l != e.l {
t.Errorf("%T line should equal %T's, received %d, expected %d", e1, e, e1.l, e.l)
}
if e1.f != e.f {
t.Errorf("%T file should equal %T's, received %s, expected %s", e1, e, e1.f, e.f)
}
if !bytes.Equal(e1.t, e.t) {
t.Errorf("%T trace should equal %T's, received %2x, expected %2x", e1, e, e1.t, e.t)
}
if e1.c != e.c {
t.Errorf("%T parent error should equal %T's, received %T[%s], expected %T[%s]", e1, e, e1.c, e1.c, e.c, e.c)
}
e2 := &e
if !e.As(&e2) {
t.Errorf("%T should be assertable as %T", e, e2)
}
if e2.m != e.m {
t.Errorf("%T message should equal %T's, received %s, expected %s", e2, e, e2.m, e.m)
}
if e2.l != e.l {
t.Errorf("%T line should equal %T's, received %d, expected %d", e2, e, e2.l, e.l)
}
if e2.f != e.f {
t.Errorf("%T file should equal %T's, received %s, expected %s", e2, e, e2.f, e.f)
}
if !bytes.Equal(e2.t, e.t) {
t.Errorf("%T trace should equal %T's, received %2x, expected %2x", e2, e, e2.t, e.t)
}
if e2.c != e.c {
t.Errorf("%T parent error should equal %T's, received %T[%s], expected %T[%s]", e2, e, e2.c, e2.c, e.c, e.c)
}
}
func TestErr_Error(t *testing.T) {
e := Err{m: "test"}
if e.Error() != e.m {
t.Errorf("Error() returned %s, expected %s", e.Error(), e.m)
}
}
func TestErr_Location(t *testing.T) {
e := Err{l: 11, f: "random"}
if f, l := e.Location(); l != e.l || f != e.f {
t.Errorf("Location() returned: %s:%d, expected: %s:%d", f, l, e.f, e.l)
}
}
func TestErr_StackTrace(t *testing.T) {
e := Err{t: []byte{0x6, 0x6, 0x6}}
if !bytes.Equal(e.StackTrace(), e.t) {
t.Errorf("StackTrace() returned: %2x, expected: %2x", e.StackTrace(), e.t)
}
}
func TestErr_Unwrap(t *testing.T) {
e := Err{c: fmt.Errorf("ttt")}
w := e.Unwrap()
if w != e.c {
t.Errorf("Unwrap() returned: %T[%s], expected: %T[%s]", w, w, e.c, e.c)
}
}
*/
func TestErr_Error(t *testing.T) {
type fields struct {
m string
c error
t stack
}
tests := []struct {
quiet bool
name string
fields fields
want string
}{
{
name: "empty",
fields: fields{},
want: "",
},
{
name: "just text",
fields: fields{m: "test"},
want: "test",
},
{
name: "text with single wrapped error",
fields: fields{m: "test", c: fmt.Errorf("error")},
want: "test: error",
},
{
name: "text with two wrapped errors",
fields: fields{m: "test", c: fmt.Errorf("error: %w", Newf("some error"))},
want: "test: error: some error",
},
{
name: "text with two wrapped errors, but no unwrapping",
quiet: true,
fields: fields{m: "test", c: fmt.Errorf("error: %w", Newf("some error"))},
want: "test",
},
{
name: "text with two wrapped Err errors",
fields: fields{m: "test", c: &Err{m: "error", c: &Err{m: "another error"}}},
want: "test: error: another error",
},
{
name: "text with two wrapped Err errors, without unwrapping",
quiet: true,
fields: fields{m: "test", c: &Err{m: "error", c: &Err{m: "another error"}}},
want: "test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
IncludeBacktrace = tt.quiet
e := Err{
m: tt.fields.m,
c: tt.fields.c,
t: tt.fields.t,
}
if got := e.Error(); got != tt.want {
t.Errorf("Error() = %v, want %v", got, tt.want)
}
})
}
}