-
Notifications
You must be signed in to change notification settings - Fork 2
/
mailaddress_test.go
170 lines (148 loc) · 4.53 KB
/
mailaddress_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
package mailaddress
import (
"errors"
"fmt"
"reflect"
"testing"
"github.com/teamwork/test"
"github.com/teamwork/test/diff"
)
func TestParseList(t *testing.T) {
// This just tests the expected return values and the like. Testing the full
// parsing logic is in parse_test.go.
cases := []struct {
str string
expected List
expectedValid bool
}{
{``, List{}, false},
{`asd`, List{Address{Raw: "asd"}}, true},
{"Martin <[email protected]>", List{Address{Name: "Martin", Address: "[email protected]"}}, false},
{"\"Martin, What\" <[email protected]>, [email protected]", List{
Address{Raw: "\"Martin, What\" <[email protected]>", Name: "Martin, What", Address: "[email protected]"},
Address{Raw: "[email protected]", Address: "[email protected]"},
}, false},
}
for _, tc := range cases {
t.Run(tc.str, func(t *testing.T) {
got, gotValid := ParseList(tc.str)
if !cmplist(tc.expected, got) {
t.Errorf(diff.Cmp(tc.expected, got))
}
if gotValid != tc.expectedValid {
t.Errorf("gotValid: %v, expectedValid: %v",
gotValid, tc.expectedValid)
}
})
}
}
func TestParse(t *testing.T) {
// This just tests the expected return values and the like. Testing the
// parsing logic is in parse_test.go.
cases := []struct {
str string
expected Address
expectedErr string
}{
{"Martin <[email protected]>", Address{Name: "Martin", Address: "[email protected]"}, ""},
{"Amy <[email protected],>", Address{Name: "Amy", Address: "[email protected]"}, ""},
{"Martin <[email protected]>, [email protected]", Address{}, ErrTooManyEmails.Error()},
}
for _, tc := range cases {
t.Run(tc.str, func(t *testing.T) {
got, gotErr := Parse(tc.str)
if !test.ErrorContains(gotErr, tc.expectedErr) {
t.Errorf("wrong error\nexpected: %#v\ngot : %v\n",
tc.expectedErr, gotErr)
}
if !cmpaddr(got, tc.expected) {
t.Errorf(diff.Cmp(tc.expected, got))
}
})
}
}
func TestNewHelpers(t *testing.T) {
cases := []struct {
name, address string
expected Address
}{
{"", "", Address{Name: "", Address: "", err: errors.New("")}},
{"Martin", "", Address{Name: "Martin", Address: "", err: errors.New("")}},
{"", "[email protected]", Address{Name: "", Address: "[email protected]"}},
{"Martin", "[email protected]", Address{Name: "Martin", Address: "[email protected]"}},
// Invalid addresses should result in an invalid Address{} (that is, one
// without the Address field set).
{"Martin", "invalid", Address{Name: "Martin", Address: "", err: errors.New("")}},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%v <%v>", tc.name, tc.address), func(t *testing.T) {
got := New(tc.name, tc.address)
if tc.expected.Error() != nil && got.Error() == nil {
t.Fatal("expected error but got nil")
}
if tc.expected.Error() == nil && got.Error() != nil {
t.Fatal("expected no error but got error")
}
if got.Name != tc.expected.Name || got.Address != tc.expected.Address {
t.Errorf(diff.Cmp(tc.expected, got))
}
gotList := NewList(tc.name, tc.address)
if len(gotList) != 1 || gotList[0].Name != tc.expected.Name || gotList[0].Address != tc.expected.Address {
t.Errorf(diff.Cmp(tc.expected, gotList[0]))
}
})
}
}
func TestFromMap(t *testing.T) {
cases := []struct {
in map[string]string
expected List
}{
{map[string]string{}, *new(List)},
{nil, *new(List)},
{
map[string]string{"Martin": "[email protected]"},
List{Address{Name: "Martin", Address: "[email protected]"}},
},
//{
// map[string]string{"Martin": "[email protected]", "foo": "[email protected]"},
// List{
// Address{Name: "Martin", Address: "[email protected]"},
// Address{Name: "foo", Address: "[email protected]"},
// },
//},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%#v", tc.in), func(t *testing.T) {
got := FromMap(tc.in)
if !reflect.DeepEqual(tc.expected, got) {
t.Errorf(diff.Cmp(tc.expected, got))
}
})
}
}
func TestFromSlice(t *testing.T) {
cases := []struct {
in []string
expected List
}{
{[]string{}, *new(List)},
{nil, *new(List)},
{
[]string{"[email protected]"},
List{Address{Name: "", Address: "[email protected]"}},
},
{
[]string{"[email protected]", "[email protected]"},
List{Address{Address: "[email protected]"}, Address{Address: "[email protected]"}},
},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%#v", tc.in), func(t *testing.T) {
got := FromSlice(tc.in)
if diff.Diff(tc.expected, got) != "" {
t.Errorf(diff.Cmp(tc.expected, got))
}
})
}
}