-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_test.go
202 lines (181 loc) · 3.68 KB
/
list_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
package errutil
import (
"errors"
"reflect"
"testing"
)
func TestInvalidNil(t *testing.T) {
got := (&errorList{}).First()
if got != nil {
t.Errorf("expected nil, got: %v\n", got)
}
}
func TestInvalidLenOne(t *testing.T) {
want := "want"
list := &errorList{a: []error{errors.New(want)}}
got := list.Error()
if want != got {
t.Errorf("wanted: %v, got: %v\n", want, got)
}
}
func TestErrorTwo(t *testing.T) {
err := Append(
errors.New("a"),
errors.New("b"),
)
want := "a\nb"
got := err.Error()
if want != got {
t.Errorf("wanted: %v, got: %v\n", want, got)
}
}
func TestNested(t *testing.T) {
chick := errors.New("in nest")
nest := &errorList{a: []error{
&errorList{a: []error{chick}},
}}
var got error
nest.Walk(func(err error) {
got = err
})
if got != chick {
t.Errorf("expected: %v, got: %v\n", chick, got)
}
}
func TestWalkNil(t *testing.T) {
called := false
Walk(nil, func(err error) {
called = true
})
if called {
t.Error("func was called but should not have been")
}
}
func TestWalkNNil(t *testing.T) {
called := false
WalkN(nil, 1, func(err error) {
called = true
})
if called {
t.Error("func was called but should not have been")
}
}
func TestWalkSingle(t *testing.T) {
want := errors.New("want")
var got error
Walk(want, func(err error) {
got = err
})
if want != got {
t.Errorf("wanted: %v, got: %v\n", want, got)
}
}
func TestWalkNPanic(t *testing.T) {
want := errors.New("want")
defer func() {
got := recover()
if got != want {
t.Errorf("wanted: %v, got: %v\n", want, got)
}
}()
WalkN(errors.New("dummy"), 1, func(_ error) {
panic(want)
})
}
func TestWalkNLimits(t *testing.T) {
err := Append(
errors.New("one"),
errors.New("two"),
)
var got error
WalkN(err, 1, func(err error) {
if got != nil {
t.Error("WalkN walked too far")
}
got = err
})
}
func TestFirstOne(t *testing.T) {
correct := errors.New("a")
out := First(correct)
if out != correct {
t.Errorf("\n%#v\n!=\n%#v\n", out, correct)
}
}
func TestFirstThree(t *testing.T) {
correct := errors.New("a")
e2 := errors.New("b")
e3 := errors.New("c")
out := First(Append(correct, e2, e3))
if !reflect.DeepEqual(out, correct) {
t.Errorf("\n%#v\n!=\n%#v\n", out, correct)
}
}
func TestMulti_Merr(t *testing.T) {
e1 := errors.New("a")
e2 := errors.New("b")
e3 := errors.New("c")
e23 := Append(e2, e3)
out := Append(e1, e23)
correct := &errorList{[]error{e1, e2, e3}}
if !reflect.DeepEqual(out, correct) {
t.Errorf("\n%#v\n!=\n%#v\n", out, correct)
}
}
func TestMulti_ErrErrX(t *testing.T) {
e1 := errors.New("a")
e2 := errors.New("b")
out := Append(e2, e1)
correct := &errorList{[]error{e2, e1}}
if !reflect.DeepEqual(out, correct) {
t.Errorf("\n%#v\n!=\n%#v\n", out, correct)
}
}
func TestMulti_ErrErr(t *testing.T) {
e1 := errors.New("a")
e2 := errors.New("b")
out := Append(e1, e2)
correct := &errorList{[]error{e1, e2}}
if !reflect.DeepEqual(out, correct) {
t.Errorf("\n%#v\n!=\n%#v\n", out, correct)
}
}
func TestMulti_ErrNil(t *testing.T) {
e := errors.New("a")
out := Append(e, nil)
if out != e {
t.Errorf("%#v != %#v\n", out, e)
}
}
func TestMulti_NilErr(t *testing.T) {
e := errors.New("a")
out := Append(nil, e)
if out != e {
t.Errorf("%#v != %#v\n", out, e)
}
}
func TestMulti_Err(t *testing.T) {
e := errors.New("a")
out := Append(e)
if out != e {
t.Errorf("%#v != %#v\n", out, e)
}
}
func TestMulti_NilNilNil(t *testing.T) {
out := Append(nil, nil, nil)
if out != nil {
t.Errorf("%#v\n", out)
}
}
func TestMulti_NilNil(t *testing.T) {
out := Append(nil, nil)
if out != nil {
t.Errorf("%#v\n", out)
}
}
func TestMulti_Nil(t *testing.T) {
out := Append(nil)
if out != nil {
t.Errorf("%#v\n", out)
}
}