-
Notifications
You must be signed in to change notification settings - Fork 2
/
list_test.go
324 lines (294 loc) · 7.48 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package mailaddress
import (
"encoding/json"
"errors"
"fmt"
"testing"
multierror "github.com/hashicorp/go-multierror"
"github.com/teamwork/test"
"github.com/teamwork/test/diff"
)
func TestAppend(t *testing.T) {
cases := []struct {
name, address string
in, expected List
}{
{
"Kees", "[email protected]",
List{Address{Name: "Martin", Address: "[email protected]"}},
List{
Address{Name: "Martin", Address: "[email protected]"},
Address{Name: "Kees", Address: "[email protected]"},
},
},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%v-%v", tc.name, tc.address), func(t *testing.T) {
tc.in.Append(tc.name, tc.address)
if diff.Diff(tc.expected, tc.in) != "" {
t.Errorf(diff.Cmp(tc.expected, tc.in))
}
})
}
}
func TestJSON(t *testing.T) {
cases := []struct {
in string
expected []string
expectedErr string
}{
{"", []string{}, "unexpected end of JSON input"},
{"[", []string{}, "unexpected end of JSON input"},
{`["invalid"]`, []string{}, ""},
{
`["[email protected]", "[email protected]"]`,
[]string{"[email protected]", "[email protected]"},
"",
},
{
`["[email protected]", "martin.com"]`,
[]string{"[email protected]"},
"",
},
{
[]string{"[email protected]", "[email protected]"},
"",
},
{
`"[email protected], beanwork"`,
[]string{"[email protected]"},
"",
},
{
`[{"name": "Robert O'Leary", "address": "[email protected]"}]`,
[]string{"[email protected]"},
"",
},
{
`[
{"name": "Robert O'Leary", "address": "[email protected]"},
{"name": "Brandon Hansen", "address": "[email protected]"}
]`,
[]string{"[email protected]", "[email protected]"},
"",
},
{
`[
{"name": "Robert O'Leary", "address": "[email protected]"},
{"name": "Brandon Hansen", "address": "[email protected]"},
{"name": "bad email", "address": "bad"}
]`,
[]string{"[email protected]", "[email protected]"},
"",
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
var list List
gotErr := json.Unmarshal([]byte(tc.in), &list)
if !test.ErrorContains(gotErr, tc.expectedErr) {
t.Errorf("got error %#v – expected %#v", gotErr, tc.expectedErr)
}
out := list.Slice()
if diff.Diff(tc.expected, out) != "" {
t.Errorf(diff.Cmp(tc.expected, out))
}
})
}
}
func TestToSlice(t *testing.T) {
cases := []struct {
in List
expected []string
}{
{List{}, []string{}},
{List{Address{}}, []string{}},
{
List{Address{Name: "Martin", Address: ""}},
[]string{},
},
{
List{Address{Name: "Martin", Address: "[email protected]"}},
[]string{"[email protected]"},
},
{
List{Address{Name: "Martin", Address: "[email protected]"}, Address{Name: "", Address: "[email protected]"}},
[]string{"[email protected]", "[email protected]"},
},
{
List{Address{Name: "Martin", Address: "[email protected]"}, Address{}, Address{Name: "", Address: "[email protected]"}},
[]string{"[email protected]", "[email protected]"},
},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%v", tc.in), func(t *testing.T) {
out := tc.in.Slice()
if diff.Diff(tc.expected, out) != "" {
t.Errorf(diff.Cmp(tc.expected, out))
}
})
}
}
func TestSort(t *testing.T) {
cases := []struct {
in List
sortKey int8
expected List
}{
{List{}, ByName, List{}},
{List{Address{}}, ByName, List{Address{}}},
{
List{Address{Name: "zz"}, Address{Name: "aa"}},
ByName,
List{Address{Name: "aa"}, Address{Name: "zz"}},
},
{
List{Address{Address: "[email protected]"}, Address{Address: "[email protected]"}},
ByAddress,
List{Address{Address: "[email protected]"}, Address{Address: "[email protected]"}},
},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%v", tc.in), func(t *testing.T) {
tc.in.Sort(tc.sortKey)
if diff.Diff(tc.expected, tc.in) != "" {
t.Errorf(diff.Cmp(tc.expected, tc.in))
}
})
}
}
func TestErrors(t *testing.T) {
cases := []struct {
in func() (List, bool)
expectedHaveErr bool
expectedError string
expectedMultiError *multierror.Error
}{
{
func() (List, bool) { return ParseList("[email protected]") },
false,
"",
nil,
},
{
func() (List, bool) { return ParseList("[email protected], invalid") },
true,
"1 error occurred:",
&multierror.Error{
Errors: []error{errors.New("unable to find an email address")},
},
},
{
func() (List, bool) { return ParseList("@example.com") },
true,
"1 error occurred:",
&multierror.Error{
Errors: []error{errors.New("unable to find an email address")},
},
},
{
func() (List, bool) { return ParseList("@example.com, invalid") },
true,
"2 errors occurred:",
&multierror.Error{
Errors: []error{
errors.New("unable to find an email address"),
errors.New("unable to find an email address"),
},
},
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
gotList, gotHaveErr := tc.in()
if gotHaveErr != tc.expectedHaveErr {
t.Errorf("expected haveErr %v, got %v",
tc.expectedHaveErr, gotHaveErr)
}
if !test.ErrorContains(gotList.Errors(), tc.expectedError) {
t.Errorf("wrong errors\nexpected: %#v\ngot : %v\n",
tc.expectedError, gotList.Errors())
}
if gotList.Errors() != nil {
merr, ok := gotList.Errors().(*multierror.Error)
if !ok {
t.Fatalf("cannot convert %#v to multierror", gotList.Errors())
}
if diff.Diff(merr, tc.expectedMultiError) != "" {
t.Fatalf("multierror didn't match:\ngot : %+v\nexpected: %+v\n",
merr, tc.expectedMultiError)
}
}
})
}
}
func TestValidAddresses(t *testing.T) {
cases := []struct {
in List
expected List
}{
{List{}, List{}},
{
List{Address{Name: "foo", Address: "[email protected]"}},
List{Address{Name: "foo", Address: "[email protected]"}},
},
{
List{
Address{Name: "foo", Address: "[email protected]"},
Address{Name: "foo", Address: "not valid"},
},
List{
Address{Name: "foo", Address: "[email protected]"},
},
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
out := tc.in.ValidAddresses()
if out.String() != tc.expected.String() {
t.Errorf("\nout: %#v\nexpected: %#v\n", out, tc.expected)
}
})
}
}
func TestContainsAddress(t *testing.T) {
cases := []struct {
in List
test string
expected bool
}{
{List{}, "", false},
{List{Address{Address: "[email protected]"}}, "[email protected]", true},
{List{Address{Address: "f€@Ü.русские"}}, "f€@ü.русские", true},
{List{Address{Address: "f€@Ü.русские"}}, "f€@ü.рсские", false},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
out := tc.in.ContainsAddress(tc.test)
if out != tc.expected {
t.Errorf("\nout: %#v\nexpected: %#v\n", out, tc.expected)
}
})
}
}
func TestContainsDomain(t *testing.T) {
cases := []struct {
in List
test string
expected bool
}{
{List{}, "", false},
{List{Address{Address: "[email protected]"}}, "example.com", true},
{List{Address{Address: "f€@Ü.русские"}}, "ü.русские", true},
{List{Address{Address: "f€@Ü.русские"}}, "ü.рсские", false},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
out := tc.in.ContainsDomain(tc.test)
if out != tc.expected {
t.Errorf("\nout: %#v\nexpected: %#v\n", out, tc.expected)
}
})
}
}