-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy patherror_test.go
210 lines (161 loc) · 4.28 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
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
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package base
import (
"bytes"
"errors"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestParseError_Error(t *testing.T) {
errorList := ErrorList{}
errorList.Add(errors.New("testing"))
pse := ParseError{
Err: errorList,
Line: 5,
Record: "ABC",
}
if !strings.Contains(pse.Error(), "testing") {
t.Errorf("got %s", errorList.Error())
}
if pse.Record != "ABC" {
t.Errorf("got %s", pse.Record)
}
if pse.Line != 5 {
t.Errorf("got %v", pse.Line)
}
}
func TestParseErrorRecordNull_Error(t *testing.T) {
errorList := ErrorList{}
errorList.Add(errors.New("testing"))
pse := ParseError{
Err: errorList,
Line: 5,
Record: "",
}
e1 := pse.Error()
if e1 != "line:5 base.ErrorList testing" {
t.Errorf("got %s", e1)
}
}
func TestErrorList_Add(t *testing.T) {
errorList := ErrorList{}
errorList.Add(errors.New("testing"))
es := errorList.Error()
if es != "testing" {
t.Errorf("got %s", errorList.Error())
}
if errorList.Empty() {
t.Errorf("ErrorList is empty: %v", errorList)
}
errorList.Add(errors.New("continued testing"))
if errorList.Empty() {
t.Errorf("ErrorList is empty: %v", errorList)
}
}
func TestErrorList_Err(t *testing.T) {
errorList := ErrorList{}
errorList.Add(errors.New("testing"))
e1 := errorList.Err()
if e1.Error() != "testing" {
t.Errorf("got %q", e1)
}
}
func TestErrorList_Print(t *testing.T) {
errorList := ErrorList{}
errorList.Add(errors.New("testing"))
errorList.Add(errors.New("continued testing"))
var buf bytes.Buffer
errorList.Print(&buf)
if v := errorList.Error(); v == "<nil>" {
t.Errorf("got %q", v)
}
buf.Reset()
}
func TestErrorList_Empty(t *testing.T) {
errorList := ErrorList{}
e1 := errorList.Err()
if e1 != nil {
t.Errorf("got %q", e1)
}
if errorList.Error() != "<nil>" {
t.Errorf("got %s", errorList.Error())
}
var buf bytes.Buffer
errorList.Print(&buf)
buf.Reset()
}
func TestErrorList__EmptyThenNot(t *testing.T) {
var el ErrorList
require.NoError(t, el.Err())
require.Equal(t, "<nil>", el.Error())
require.True(t, el.Empty())
el.Add(errors.New("bad thing"))
require.Error(t, el.Err())
require.Equal(t, "bad thing", el.Error())
require.False(t, el.Empty())
}
func TestErrorList_MarshalJSON(t *testing.T) {
errorList := ErrorList{}
errorList.Add(errors.New("testing"))
errorList.Add(errors.New("continued testing"))
errorList.Add(errors.New("testing again"))
errorList.Add(errors.New("continued testing again"))
b, err := errorList.MarshalJSON()
if len(b) == 0 {
t.Errorf("got %s", errorList.Error())
}
if err != nil {
t.Errorf("got %s", errorList.Error())
}
}
// testMatch validates the Match error function
func TestMatch(t *testing.T) {
testError := errors.New("Test error")
if !Match(nil, nil) {
t.Error("Match should be reflexive on nil")
}
if !Match(testError, testError) {
t.Error("Match should be reflexive")
}
p := ParseError{Err: testError}
if !Match(p, testError) {
t.Error("Match should match wrapped errors implementing the UnwrappableError interface")
}
differentError := errors.New("Different error")
if Match(testError, differentError) {
t.Error("Match should return false for different simple errors")
}
q := ParseError{Err: differentError}
if !Match(p, q) {
t.Error("Match should match two different ParseErrors to each other since they have the same type")
}
errorList := ErrorList{}
if Match(errorList, p) {
t.Error("Match should return false for errors with different types")
}
}
// testHas validates the Has error function
func TestHas(t *testing.T) {
err := errors.New("Non list error")
if Has(err, err) {
t.Error("Has should return false when given a non-list error as the first arg")
}
if Has(nil, err) {
t.Error("Has should not return true if there are no errors")
}
if Has(ErrorList([]error{}), err) {
t.Error("Has should not return true if there are no errors")
}
if !Has(ErrorList([]error{err}), err) {
t.Error("Has should return true if the error list has the test error")
}
}
func TestErrorList_Panic(t *testing.T) {
var el ErrorList
require.Equal(t, "<nil>", fmt.Sprintf("%v", el))
require.Equal(t, "<nil>", fmt.Errorf("%w", el).Error())
}