-
Notifications
You must be signed in to change notification settings - Fork 23
/
match_test.go
81 lines (72 loc) · 2.69 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
package enmime
import (
"github.com/stretchrcom/testify/assert"
"testing"
)
func TestBreadthMatchFirst(t *testing.T) {
// Setup test MIME tree:
// root
// ├── a1
// │ ├── b1
// │ └── b2
// ├── a2
// └── a3
root := &memMIMEPart{contentType: "multipart/alternative"}
a1 := &memMIMEPart{contentType: "multipart/related", parent: root}
a2 := &memMIMEPart{contentType: "text/plain", parent: root}
a3 := &memMIMEPart{contentType: "text/html", parent: root}
b1 := &memMIMEPart{contentType: "text/plain", parent: a1}
b2 := &memMIMEPart{contentType: "text/html", parent: a1}
root.firstChild = a1
a1.nextSibling = a2
a2.nextSibling = a3
a1.firstChild = b1
b1.nextSibling = b2
p := BreadthMatchFirst(root, func(pt MIMEPart) bool {
return pt.ContentType() == "text/plain"
})
assert.NotNil(t, p, "BreathMatchFirst should have returned a result for text/plain")
assert.True(t, p.(*memMIMEPart) == a2,
"BreadthMatchFirst should have returned the first text/plain object")
p = BreadthMatchFirst(root, func(pt MIMEPart) bool {
return pt.ContentType() == "text/html"
})
assert.True(t, p.(*memMIMEPart) == a3,
"BreadthMatchFirst should have returned the first text/html object")
}
func TestBreadthMatchAll(t *testing.T) {
// Setup test MIME tree:
// root
// ├── a1
// │ ├── b1
// │ └── b2
// ├── a2
// └── a3
root := &memMIMEPart{contentType: "multipart/alternative"}
a1 := &memMIMEPart{contentType: "multipart/related", parent: root}
a2 := &memMIMEPart{contentType: "text/plain", parent: root}
a3 := &memMIMEPart{contentType: "text/html", parent: root}
b1 := &memMIMEPart{contentType: "text/plain", parent: a1}
b2 := &memMIMEPart{contentType: "text/html", parent: a1}
root.firstChild = a1
a1.nextSibling = a2
a2.nextSibling = a3
a1.firstChild = b1
b1.nextSibling = b2
ps := BreadthMatchAll(root, func(pt MIMEPart) bool {
return pt.ContentType() == "text/plain"
})
assert.Equal(t, len(ps), 2, "BreadthMatchAll should have returned two matches")
assert.True(t, ps[0].(*memMIMEPart) == a2,
"BreadthMatchFirst should have returned the first text/plain object")
assert.True(t, ps[1].(*memMIMEPart) == b1,
"BreadthMatchFirst should have returned the second text/plain object")
ps = BreadthMatchAll(root, func(pt MIMEPart) bool {
return pt.ContentType() == "text/html"
})
assert.Equal(t, len(ps), 2, "BreadthMatchAll should have returned two matches")
assert.True(t, ps[0].(*memMIMEPart) == a3,
"BreadthMatchFirst should have returned the first text/html object")
assert.True(t, ps[1].(*memMIMEPart) == b2,
"BreadthMatchFirst should have returned the second text/html object")
}