forked from jhillyerd/enmime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_test.go
182 lines (165 loc) · 5.35 KB
/
match_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
package enmime
import (
"testing"
)
func TestBreadthMatchFirst(t *testing.T) {
// Setup test MIME tree:
// root
// ├── a1
// │ ├── b1
// │ └── b2
// ├── a2
// └── a3
root := &Part{ContentType: "multipart/alternative", FileName: "root"}
a1 := &Part{ContentType: "multipart/related", Parent: root, FileName: "a1"}
a2 := &Part{ContentType: "text/plain", Parent: root, FileName: "a2"}
a3 := &Part{ContentType: "text/html", Parent: root, FileName: "a3"}
b1 := &Part{ContentType: "text/plain", Parent: a1, FileName: "b1"}
b2 := &Part{ContentType: "text/html", Parent: a1, FileName: "b2"}
root.FirstChild = a1
a1.NextSibling = a2
a2.NextSibling = a3
a1.FirstChild = b1
b1.NextSibling = b2
p := root.BreadthMatchFirst(func(pt *Part) bool {
return pt.ContentType == "text/plain"
})
if p == nil {
t.Fatal("BreadthMatchFirst should have returned a result for text/plain")
}
if p != a2 {
t.Error("BreadthMatchFirst should have returned a2, got:", p.FileName)
}
p = root.BreadthMatchFirst(func(pt *Part) bool {
return pt.ContentType == "text/html"
})
if p == nil {
t.Fatal("BreadthMatchFirst should have returned a result for text/html")
}
if p != a3 {
t.Error("BreadthMatchFirst should have returned a3, got:", p.FileName)
}
}
func TestBreadthMatchAll(t *testing.T) {
// Setup test MIME tree:
// root
// ├── a1
// │ ├── b1
// │ └── b2
// ├── a2
// └── a3
root := &Part{ContentType: "multipart/alternative", FileName: "root"}
a1 := &Part{ContentType: "multipart/related", Parent: root, FileName: "a1"}
a2 := &Part{ContentType: "text/plain", Parent: root, FileName: "a2"}
a3 := &Part{ContentType: "text/html", Parent: root, FileName: "a3"}
b1 := &Part{ContentType: "text/plain", Parent: a1, FileName: "b1"}
b2 := &Part{ContentType: "text/html", Parent: a1, FileName: "b2"}
root.FirstChild = a1
a1.NextSibling = a2
a2.NextSibling = a3
a1.FirstChild = b1
b1.NextSibling = b2
ps := root.BreadthMatchAll(func(pt *Part) bool {
return pt.ContentType == "text/plain"
})
if len(ps) != 2 {
t.Fatal("BreadthMatchAll should have returned two matches, got:", len(ps))
}
if ps[0] != a2 {
t.Error("BreadthMatchAll should have returned a2, got:", ps[0].FileName)
}
if ps[1] != b1 {
t.Error("BreadthMatchAll should have returned b1, got:", ps[1].FileName)
}
ps = root.BreadthMatchAll(func(pt *Part) bool {
return pt.ContentType == "text/html"
})
if len(ps) != 2 {
t.Fatal("BreadthMatchAll should have returned two matches, got:", len(ps))
}
if ps[0] != a3 {
t.Error("BreadthMatchAll should have returned a3, got:", ps[0].FileName)
}
if ps[1] != b2 {
t.Error("BreadthMatchAll should have returned b2, got:", ps[1].FileName)
}
}
func TestDepthMatchFirst(t *testing.T) {
// Setup test MIME tree:
// root
// ├── a1
// │ ├── b1
// │ └── b2
// ├── a2
// └── a3
root := &Part{ContentType: "multipart/alternative", FileName: "root"}
a1 := &Part{ContentType: "multipart/related", Parent: root, FileName: "a1"}
a2 := &Part{ContentType: "text/plain", Parent: root, FileName: "a2"}
a3 := &Part{ContentType: "text/html", Parent: root, FileName: "a3"}
b1 := &Part{ContentType: "text/plain", Parent: a1, FileName: "b1"}
b2 := &Part{ContentType: "text/html", Parent: a1, FileName: "b2"}
root.FirstChild = a1
a1.NextSibling = a2
a2.NextSibling = a3
a1.FirstChild = b1
b1.NextSibling = b2
p := root.DepthMatchFirst(func(pt *Part) bool {
return pt.ContentType == "text/plain"
})
if p == nil {
t.Fatal("DepthMatchFirst should have returned a result for text/plain")
}
if p != b1 {
t.Error("DepthMatchFirst should have returned b1, got:", p.FileName)
}
p = root.DepthMatchFirst(func(pt *Part) bool {
return pt.ContentType == "text/html"
})
if p != b2 {
t.Error("DepthMatchFirst should have returned b2, got:", p.FileName)
}
}
func TestDepthMatchAll(t *testing.T) {
// Setup test MIME tree:
// root
// ├── a1
// │ ├── b1
// │ └── b2
// ├── a2
// └── a3
root := &Part{ContentType: "multipart/alternative", FileName: "root"}
a1 := &Part{ContentType: "multipart/related", Parent: root, FileName: "a1"}
a2 := &Part{ContentType: "text/plain", Parent: root, FileName: "a2"}
a3 := &Part{ContentType: "text/html", Parent: root, FileName: "a3"}
b1 := &Part{ContentType: "text/plain", Parent: a1, FileName: "b1"}
b2 := &Part{ContentType: "text/html", Parent: a1, FileName: "b2"}
root.FirstChild = a1
a1.NextSibling = a2
a2.NextSibling = a3
a1.FirstChild = b1
b1.NextSibling = b2
ps := root.DepthMatchAll(func(pt *Part) bool {
return pt.ContentType == "text/plain"
})
if len(ps) != 2 {
t.Fatal("DepthMatchAll should have returned two matches, got:", len(ps))
}
if ps[0] != b1 {
t.Error("DepthMatchAll should have returned b1, got:", ps[0].FileName)
}
if ps[1] != a2 {
t.Error("DepthMatchAll should have returned a2, got:", ps[1].FileName)
}
ps = root.DepthMatchAll(func(pt *Part) bool {
return pt.ContentType == "text/html"
})
if len(ps) != 2 {
t.Fatal("DepthMatchAll should have returned two matches, got:", len(ps))
}
if ps[0] != b2 {
t.Error("DepthMatchAll should have returned b2, got:", ps[0].FileName)
}
if ps[1] != a3 {
t.Error("DepthMatchAll should have returned a3, got:", ps[1].FileName)
}
}