-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
172 lines (156 loc) · 4.27 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
package pick
import (
"errors"
"fmt"
"io"
"testing"
"github.com/moukoublen/pick/internal/testingx"
)
func TestMultiError(t *testing.T) {
errOne := errors.New("one")
errTwo := errors.New("two")
errThree := errors.New("three")
m := &multiError{}
testingx.AssertEqual(t, m.Error(), "")
m.Add(errOne)
m.Add(errTwo)
m.Add(errThree)
testingx.AssertEqual(t, m.Error(), "one | two | three")
testingx.AssertEqual(t, errors.Is(m, errOne), true)
testingx.AssertEqual(t, errors.Is(m, errTwo), true)
testingx.AssertEqual(t, errors.Is(m, errThree), true)
}
func ptr[T any](o T) *T {
return &o
}
func TestGather(t *testing.T) {
tests := []struct {
destination *error
newError error
expect func(t *testing.T, dst *error)
}{
{
destination: nil,
newError: nil,
expect: func(t *testing.T, dst *error) {
testingx.AssertEqual(t, dst, (*error)(nil))
},
},
{
destination: nil,
newError: errors.New("error"),
expect: func(t *testing.T, dst *error) {
testingx.AssertEqual(t, dst, (*error)(nil))
},
},
{
destination: new(error),
newError: errors.New("error"),
expect: func(t *testing.T, dst *error) {
m, is := (*dst).(*multiError)
testingx.AssertEqual(t, is, true)
testingx.AssertEqual(t, m.Error(), "error")
},
},
{
destination: new(error),
newError: errors.New("error"),
expect: func(t *testing.T, dst *error) {
m, is := (*dst).(*multiError)
testingx.AssertEqual(t, is, true)
testingx.AssertEqual(t, m.Error(), "error")
},
},
{
destination: ptr[error](&multiError{}),
newError: errors.New("error"),
expect: func(t *testing.T, dst *error) {
m, is := (*dst).(*multiError)
testingx.AssertEqual(t, is, true)
testingx.AssertEqual(t, m.Error(), "error")
},
},
{
destination: ptr[error](errors.New("one")),
newError: errors.New("two"),
expect: func(t *testing.T, dst *error) {
m, is := (*dst).(*multiError)
testingx.AssertEqual(t, is, true)
testingx.AssertEqual(t, len(m.errors), 2)
testingx.AssertEqual(t, m.Error(), "one | two")
},
},
}
for i, tc := range tests {
t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) {
gather(tc.destination, tc.newError)
tc.expect(t, tc.destination)
})
}
}
func TestErrorsSink(t *testing.T) {
tests := map[string]struct {
op func(es *ErrorsSink)
expectedError func(*testing.T, error)
}{
"empty": {
op: func(_ *ErrorsSink) {},
expectedError: nil,
},
"one Gather": {
op: func(es *ErrorsSink) {
es.Gather(errors.New("error"))
},
expectedError: testingx.ExpectedErrorChecks(
testingx.ExpectedErrorStringContains("error"),
),
},
"one GatherSelector": {
op: func(es *ErrorsSink) {
es.GatherSelector("one.two", io.ErrUnexpectedEOF)
},
expectedError: testingx.ExpectedErrorChecks(
testingx.ExpectedErrorStringContains("unexpected EOF"),
testingx.ExpectedErrorOfType[*PickerError](
func(t *testing.T, pe *PickerError) {
testingx.AssertEqual(t, "one.two", pe.Selector())
},
),
testingx.ExpectedErrorIs(io.ErrUnexpectedEOF),
),
},
"mixed": {
op: func(es *ErrorsSink) {
es.GatherSelector("one.two", io.ErrUnexpectedEOF)
es.GatherSelector("one.three", io.ErrClosedPipe)
},
expectedError: testingx.ExpectedErrorChecks(
testingx.ExpectedErrorStringContains("picker error with selector `one.two` error: `unexpected EOF` |"),
testingx.ExpectedErrorStringContains("| picker error with selector `one.three` error: `io: read/write on closed pipe`"),
testingx.ExpectedErrorOfType[*multiError](
func(t *testing.T, pe *multiError) {
testingx.ExpectedErrorOfType[*PickerError](
func(t *testing.T, pe *PickerError) {
testingx.AssertEqual(t, "one.two", pe.Selector())
},
)(t, pe.errors[0])
testingx.ExpectedErrorOfType[*PickerError](
func(t *testing.T, pe *PickerError) {
testingx.AssertEqual(t, "one.three", pe.Selector())
},
)(t, pe.errors[1])
},
),
testingx.ExpectedErrorIs(io.ErrUnexpectedEOF),
testingx.ExpectedErrorIs(io.ErrClosedPipe),
),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
es := &ErrorsSink{}
tc.op(es)
testingx.AssertError(t, tc.expectedError, es.Outcome())
})
}
}