-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathaddress_test.go
137 lines (120 loc) · 3.49 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
package xco
import (
"encoding/xml"
"testing"
"github.com/pkg/errors"
)
var nilAddress = Address{}
var withDomain = Address{DomainPart: "example.com"}
var parseAddressTests = []struct {
Input string
Output Address
Error string
}{
{"", nilAddress, ""},
{" ", nilAddress, ""},
{"@example.com", withDomain, "Localpart is empty"},
{"example.com/", withDomain, "Resourcepart is empty"},
{"@example.com/", withDomain, "Multiple errors: [Localpart is empty Resourcepart is empty]"},
{"@/", nilAddress, "Multiple errors: [Localpart is empty Resourcepart is empty]"},
{"example.com", Address{"", "example.com", ""}, ""},
{"[email protected]", Address{"hello", "example.com", ""}, ""},
{"example.com/home", Address{"", "example.com", "home"}, ""},
{"[email protected]/home", Address{"hello", "example.com", "home"}, ""},
{"[email protected]/home", Address{"goodbye", "example.com", "home"}, ""},
}
func TestParseAddress(t *testing.T) {
for _, pat := range parseAddressTests {
addr, err := ParseAddress(pat.Input)
matches := addr.Equals(&pat.Output) && (err == nil && pat.Error == "" || err.Error() == pat.Error)
if !matches {
t.Errorf("ParseAddress(%s) => {%s,%v}, expected {%s,%s}",
pat.Input, addr, err, pat.Output, pat.Error)
}
}
}
var stringAddressTests = []struct {
Input *Address
Output string
}{
{&Address{"", "example.com", ""}, "example.com"},
{&Address{"hello", "example.com", ""}, "[email protected]"},
{&Address{"", "example.com", "home"}, "example.com/home"},
{&Address{"hello", "example.com", "home"}, "[email protected]/home"},
{&Address{"goodbye", "example.com", "home"}, "[email protected]/home"},
}
func TestStringAddress(t *testing.T) {
for _, sat := range stringAddressTests {
out := sat.Input.String()
matches := out == sat.Output
if !matches {
t.Errorf("%v.String() => {%s}, expected {%s}",
sat.Input, out, sat.Output)
}
}
}
func attrEquals(a *xml.Attr, b *xml.Attr) bool {
if a == b {
return true
}
if a == nil && b != nil {
return false
}
if a != nil && b == nil {
return false
}
if a.Name.Space != b.Name.Space {
return false
}
if a.Name.Local != b.Name.Local {
return false
}
if a.Value != b.Value {
return false
}
return true
}
var marshallXMLAddressTests = []struct {
Input *Address
Output xml.Attr
Error string
}{
{nil, xml.Attr{}, ""},
{&Address{"", "example.com", ""},
xml.Attr{Name: xml.Name{}, Value: "example.com"}, ""},
{&Address{"asdf", "", ""},
xml.Attr{}, "[Domain is empty]"},
}
func TestMarshallXMLAddress(t *testing.T) {
for _, mat := range marshallXMLAddressTests {
out, err := mat.Input.MarshalXMLAttr(xml.Name{})
matches := attrEquals(&out, &mat.Output) && (err == nil && mat.Error == "" || errors.Cause(err).Error() == mat.Error)
if !matches {
t.Errorf("{%s}.MarshalXMLAttr({}) => {%s,%v}, expected {%v,%s}",
mat.Input, out, errors.Cause(err), mat.Output, mat.Error)
}
}
}
var bareAddressTests = []struct {
Input string
Output string
}{
{"example.com", "example.com"},
{"[email protected]", "[email protected]"},
{"example.com/home", "example.com"},
{"[email protected]/home", "[email protected]"},
{"[email protected]/home", "[email protected]"},
}
func TestBareAddress(t *testing.T) {
for _, test := range bareAddressTests {
a, err := ParseAddress(test.Input)
if err != nil {
t.Errorf("Invalid address: %s", test.Input)
continue
}
if a.Bare().String() != test.Output {
t.Errorf("%v.Bare() => {%s}, expected {%s}",
test.Input, a.Bare(), test.Output)
}
}
}