-
Notifications
You must be signed in to change notification settings - Fork 2
/
address_test.go
191 lines (166 loc) · 4.67 KB
/
address_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
package mailaddress
import (
"fmt"
"mime"
"testing"
"github.com/teamwork/test/diff"
)
func TestNameEncoded(t *testing.T) {
cases := []struct {
in, expected string
}{
{"", ""},
{"martin", "martin"},
{"m€rtin", "=?utf-8?q?m=E2=82=ACrtin?="},
{"martin, tournoij", `"martin, tournoij"`},
{"m@rtin", `"m@rtin"`},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%v", tc.in), func(t *testing.T) {
out := Address{Name: tc.in}.NameEncoded()
if out != tc.expected {
t.Errorf("\nout: %v\nexpected: %v\n", out, tc.expected)
}
})
}
}
func TestAddressEncoded(t *testing.T) {
cases := []struct {
in, expected string
}{
{"", ""},
{"martin", "martin"},
{"m€rtin", "=?utf-8?q?m=E2=82=ACrtin?="},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%v", tc.in), func(t *testing.T) {
out := Address{Address: tc.in}.AddressEncoded()
if out != tc.expected {
t.Errorf("\nout: %v\nexpected: %v\n", out, tc.expected)
}
})
}
}
func TestStringEncoded(t *testing.T) {
cases := []struct {
in, expected string
count int
}{
{`[email protected]`, `[email protected]`, 1},
{`Martin Tournoij <[email protected]>`, `Martin Tournoij <[email protected]>`, 1},
{`"Martin Tournoij" <[email protected]>`, `Martin Tournoij <[email protected]>`, 1},
{`Martin Tour<noij> <[email protected]>`, `Martin Tour noij <[email protected]>`, 1},
{
`Martin, Nichole <[email protected] <mailto:[email protected]>>r`,
"Nichole <[email protected]>",
1,
},
{
`a العَرَبِي b <[email protected]>`,
`=?utf-8?q?a_=D8=A7=D9=84=D8=B9=D9=8E=D8=B1=D9=8E=D8=A8=D9=90=D9=8A_b?= <[email protected]>`,
1,
},
}
for _, tc := range cases {
t.Run(tc.in, func(t *testing.T) {
out, outErr := ParseList(tc.in)
out = out.ValidAddresses()
if outErr && len(out) != tc.count {
t.Error(out.Errors(), " count:", len(out))
}
if out.StringEncoded() != tc.expected {
t.Errorf("\nout: %#v\nexpected: %#v\n",
out.StringEncoded(), tc.expected)
}
dec := new(mime.WordDecoder)
_, err := dec.DecodeHeader(out.StringEncoded())
if err != nil {
t.Errorf("can't parse %s: %s", out.StringEncoded(), err.Error())
}
})
}
}
func TestToList(t *testing.T) {
cases := []struct {
in Address
expected List
}{
{Address{}, List{Address{}}},
{Address{Name: "Martin", Address: ""}, List{Address{Name: "Martin", Address: ""}}},
{Address{Name: "Martin", Address: "martin@"}, List{Address{Name: "Martin", Address: "martin@"}}},
{
Address{Name: "Martin", Address: "[email protected]"},
List{Address{Name: "Martin", Address: "[email protected]"}},
},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%v", tc.in), func(t *testing.T) {
out := tc.in.ToList()
if diff.Diff(tc.expected, out) != "" {
t.Errorf(diff.Cmp(tc.expected, out))
}
})
}
}
func TestLocal(t *testing.T) {
cases := []struct {
in Address
expected string
}{
{Address{}, ""},
{Address{Address: "martin"}, "martin"},
{Address{Address: "[email protected]"}, "martin"},
{Address{Address: "[email protected]"}, "martin+tag"},
{Address{Address: "[email protected]"}, "martin"},
{Address{Address: "@example.com"}, ""},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%v", tc.in), func(t *testing.T) {
out := tc.in.Local()
if out != tc.expected {
t.Errorf("\nout: %#v\nexpected: %#v\n", out, tc.expected)
}
})
}
}
func TestDomain(t *testing.T) {
cases := []struct {
in Address
expected string
}{
{Address{}, ""},
{Address{Address: "martin"}, "martin"},
{Address{Address: "[email protected]"}, "example.com"},
{Address{Address: "[email protected]"}, "example.co.com.many.domains"},
{Address{Address: "@example.com"}, "example.com"},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%v", tc.in), func(t *testing.T) {
out := tc.in.Domain()
if out != tc.expected {
t.Errorf("\nout: %#v\nexpected: %#v\n", out, tc.expected)
}
})
}
}
func TestWithoutTag(t *testing.T) {
cases := []struct {
in Address
expected string
}{
{Address{}, ""},
{Address{Address: "[email protected]"}, "[email protected]"},
{Address{Address: "[email protected]"}, "[email protected]"},
{Address{Address: "[email protected]"}, "[email protected]"},
// Don't support - separated tags
{Address{Address: "[email protected]"}, "[email protected]"},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%v", tc.in), func(t *testing.T) {
out := tc.in.WithoutTag()
if out != tc.expected {
t.Errorf("\nout: %#v\nexpected: %#v\n", out, tc.expected)
}
})
}
}