-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_test.go
185 lines (141 loc) · 4.19 KB
/
format_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
package multierr
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_ListFormatterFunc_noError(t *testing.T) {
DefaultFormatter = ListFormatterFunc
assert.Equal(t, "no errors occurred", (&Error{}).Error())
// Calling on a nil-error panics
var err *Error
assert.Panics(t, func() {
_ = err.Error()
})
}
func Test_ListFormatterFunc_singleError(t *testing.T) {
DefaultFormatter = ListFormatterFunc
err := Append(nil, errors.New("some error"))
expected := "1 error occurred:\n - some error"
assert.Equal(t, expected, err.Error())
// multiline-error:
err = Append(nil, errors.New("some error\nsecond line"))
expected = "1 error occurred:\n" +
" - some error\n" +
" second line"
assert.Equal(t, expected, err.Error())
}
func Test_ListFormatterFunc_multipleErrors(t *testing.T) {
DefaultFormatter = ListFormatterFunc
err := Append(nil,
errors.New("error 1"),
errors.New("error 2"))
expected := "2 errors occurred:\n" +
" - error 1\n" +
" - error 2"
assert.Equal(t, expected, err.Error())
// multiline-errors:
err = Append(nil,
errors.New("error 1\nsecond line 1"),
errors.New("error 2\nsecond line 2"))
expected = "2 errors occurred:\n" +
" - error 1\n" +
" second line 1\n" +
" - error 2\n" +
" second line 2"
assert.Equal(t, expected, err.Error())
}
func Test_TitledListFormatterFunc_noError(t *testing.T) {
DefaultFormatter = TitledListFormatter("the title")
assert.Equal(t, "no errors occurred", (&Error{}).Error())
// Calling on a nil-error panics
var err *Error
assert.Panics(t, func() {
_ = err.Error()
})
}
func Test_TitledListFormatterFunc_singleError(t *testing.T) {
DefaultFormatter = TitledListFormatter("the title")
err := Append(nil, errors.New("some error"))
expected := "the title\n - some error"
assert.Equal(t, expected, err.Error())
// multiline-error:
err = Append(nil, errors.New("some error\nsecond line"))
expected = "the title\n" +
" - some error\n" +
" second line"
assert.Equal(t, expected, err.Error())
}
func Test_TitledListFormatterFunc_multipleErrors(t *testing.T) {
DefaultFormatter = TitledListFormatter("the title")
err := Append(nil,
errors.New("error 1"),
errors.New("error 2"))
expected := "the title\n" +
" - error 1\n" +
" - error 2"
assert.Equal(t, expected, err.Error())
// multiline-errors:
err = Append(nil,
errors.New("error 1\nsecond line 1"),
errors.New("error 2\nsecond line 2"))
expected = "the title\n" +
" - error 1\n" +
" second line 1\n" +
" - error 2\n" +
" second line 2"
assert.Equal(t, expected, err.Error())
}
func Test_PrefixedListFormatterFunc_noError(t *testing.T) {
DefaultFormatter = PrefixedListFormatter("prefix: ")
assert.Equal(t, "no errors occurred", (&Error{}).Error())
// Calling on a nil-error panics
var err *Error
assert.Panics(t, func() {
_ = err.Error()
})
}
func Test_PrefixedListFormatterFunc_singleError(t *testing.T) {
DefaultFormatter = PrefixedListFormatter("prefix: ")
err := Append(nil, errors.New("some error"))
expected := "prefix: some error"
assert.Equal(t, expected, err.Error())
// multiline-error:
err = Append(nil, errors.New("some error\nsecond line"))
expected = "" +
"prefix: some error\n" +
" second line"
assert.Equal(t, expected, err.Error())
}
func Test_PrefixedListFormatterFunc_multipleErrors(t *testing.T) {
DefaultFormatter = PrefixedListFormatter("prefix: ")
err := Append(nil,
errors.New("error 1"),
errors.New("error 2"))
expected := "" +
"prefix: error 1\n" +
"prefix: error 2"
assert.Equal(t, expected, err.Error())
// multiline-errors:
err = Append(nil,
errors.New("error 1\nsecond line 1"),
errors.New("error 2\nsecond line 2"))
expected = "" +
"prefix: error 1\n" +
" second line 1\n" +
"prefix: error 2\n" +
" second line 2"
assert.Equal(t, expected, err.Error())
}
func Test_CustomFormatterFunc(t *testing.T) {
err := Append(nil,
errors.New("error 1"),
errors.New("error 2")).(*Error)
err.Formatter = func(errs []error) string {
assert.Len(t, errs, 2)
assert.EqualError(t, errs[0], "error 1")
assert.EqualError(t, errs[1], "error 2")
return "custom format"
}
assert.Equal(t, "custom format", err.Error())
}