Debian Codegraph @

match_test.go

raw
1 package enmime
2
3 import (
4 "testing"
5 )
6
7 func TestBreadthMatchFirst(t *testing.T) {
8 // Setup test MIME tree:
9 // root
10 // ├── a1
11 // │   ├── b1
12 // │   └── b2
13 // ├── a2
14 // └── a3
15
16 root := &Part{ContentType: "multipart/alternative", FileName: "root"}
17 a1 := &Part{ContentType: "multipart/related", Parent: root, FileName: "a1"}
18 a2 := &Part{ContentType: "text/plain", Parent: root, FileName: "a2"}
19 a3 := &Part{ContentType: "text/html", Parent: root, FileName: "a3"}
20 b1 := &Part{ContentType: "text/plain", Parent: a1, FileName: "b1"}
21 b2 := &Part{ContentType: "text/html", Parent: a1, FileName: "b2"}
22 root.FirstChild = a1
23 a1.NextSibling = a2
24 a2.NextSibling = a3
25 a1.FirstChild = b1
26 b1.NextSibling = b2
27
28 p := root.BreadthMatchFirst(func(pt *Part) bool {
29 return pt.ContentType == "text/plain"
30 })
31 if p == nil {
32 t.Fatal("BreadthMatchFirst should have returned a result for text/plain")
33 }
34 if p != a2 {
35 t.Error("BreadthMatchFirst should have returned a2, got:", p.FileName)
36 }
37
38 p = root.BreadthMatchFirst(func(pt *Part) bool {
39 return pt.ContentType == "text/html"
40 })
41 if p == nil {
42 t.Fatal("BreadthMatchFirst should have returned a result for text/html")
43 }
44 if p != a3 {
45 t.Error("BreadthMatchFirst should have returned a3, got:", p.FileName)
46 }
47 }
48
49 func TestBreadthMatchAll(t *testing.T) {
50 // Setup test MIME tree:
51 // root
52 // ├── a1
53 // │   ├── b1
54 // │   └── b2
55 // ├── a2
56 // └── a3
57
58 root := &Part{ContentType: "multipart/alternative", FileName: "root"}
59 a1 := &Part{ContentType: "multipart/related", Parent: root, FileName: "a1"}
60 a2 := &Part{ContentType: "text/plain", Parent: root, FileName: "a2"}
61 a3 := &Part{ContentType: "text/html", Parent: root, FileName: "a3"}
62 b1 := &Part{ContentType: "text/plain", Parent: a1, FileName: "b1"}
63 b2 := &Part{ContentType: "text/html", Parent: a1, FileName: "b2"}
64 root.FirstChild = a1
65 a1.NextSibling = a2
66 a2.NextSibling = a3
67 a1.FirstChild = b1
68 b1.NextSibling = b2
69
70 ps := root.BreadthMatchAll(func(pt *Part) bool {
71 return pt.ContentType == "text/plain"
72 })
73 if len(ps) != 2 {
74 t.Fatal("BreadthMatchAll should have returned two matches, got:", len(ps))
75 }
76 if ps[0] != a2 {
77 t.Error("BreadthMatchAll should have returned a2, got:", ps[0].FileName)
78 }
79 if ps[1] != b1 {
80 t.Error("BreadthMatchAll should have returned b1, got:", ps[1].FileName)
81 }
82
83 ps = root.BreadthMatchAll(func(pt *Part) bool {
84 return pt.ContentType == "text/html"
85 })
86 if len(ps) != 2 {
87 t.Fatal("BreadthMatchAll should have returned two matches, got:", len(ps))
88 }
89 if ps[0] != a3 {
90 t.Error("BreadthMatchAll should have returned a3, got:", ps[0].FileName)
91 }
92 if ps[1] != b2 {
93 t.Error("BreadthMatchAll should have returned b2, got:", ps[1].FileName)
94 }
95 }
96
97 func TestDepthMatchFirst(t *testing.T) {
98 // Setup test MIME tree:
99 // root
100 // ├── a1
101 // │   ├── b1
102 // │   └── b2
103 // ├── a2
104 // └── a3
105
106 root := &Part{ContentType: "multipart/alternative", FileName: "root"}
107 a1 := &Part{ContentType: "multipart/related", Parent: root, FileName: "a1"}
108 a2 := &Part{ContentType: "text/plain", Parent: root, FileName: "a2"}
109 a3 := &Part{ContentType: "text/html", Parent: root, FileName: "a3"}
110 b1 := &Part{ContentType: "text/plain", Parent: a1, FileName: "b1"}
111 b2 := &Part{ContentType: "text/html", Parent: a1, FileName: "b2"}
112 root.FirstChild = a1
113 a1.NextSibling = a2
114 a2.NextSibling = a3
115 a1.FirstChild = b1
116 b1.NextSibling = b2
117
118 p := root.DepthMatchFirst(func(pt *Part) bool {
119 return pt.ContentType == "text/plain"
120 })
121 if p == nil {
122 t.Fatal("DepthMatchFirst should have returned a result for text/plain")
123 }
124 if p != b1 {
125 t.Error("DepthMatchFirst should have returned b1, got:", p.FileName)
126 }
127
128 p = root.DepthMatchFirst(func(pt *Part) bool {
129 return pt.ContentType == "text/html"
130 })
131 if p != b2 {
132 t.Error("DepthMatchFirst should have returned b2, got:", p.FileName)
133 }
134 }
135
136 func TestDepthMatchAll(t *testing.T) {
137 // Setup test MIME tree:
138 // root
139 // ├── a1
140 // │   ├── b1
141 // │   └── b2
142 // ├── a2
143 // └── a3
144
145 root := &Part{ContentType: "multipart/alternative", FileName: "root"}
146 a1 := &Part{ContentType: "multipart/related", Parent: root, FileName: "a1"}
147 a2 := &Part{ContentType: "text/plain", Parent: root, FileName: "a2"}
148 a3 := &Part{ContentType: "text/html", Parent: root, FileName: "a3"}
149 b1 := &Part{ContentType: "text/plain", Parent: a1, FileName: "b1"}
150 b2 := &Part{ContentType: "text/html", Parent: a1, FileName: "b2"}
151 root.FirstChild = a1
152 a1.NextSibling = a2
153 a2.NextSibling = a3
154 a1.FirstChild = b1
155 b1.NextSibling = b2
156
157 ps := root.DepthMatchAll(func(pt *Part) bool {
158 return pt.ContentType == "text/plain"
159 })
160 if len(ps) != 2 {
161 t.Fatal("DepthMatchAll should have returned two matches, got:", len(ps))
162 }
163 if ps[0] != b1 {
164 t.Error("DepthMatchAll should have returned b1, got:", ps[0].FileName)
165 }
166 if ps[1] != a2 {
167 t.Error("DepthMatchAll should have returned a2, got:", ps[1].FileName)
168 }
169
170 ps = root.DepthMatchAll(func(pt *Part) bool {
171 return pt.ContentType == "text/html"
172 })
173 if len(ps) != 2 {
174 t.Fatal("DepthMatchAll should have returned two matches, got:", len(ps))
175 }
176 if ps[0] != b2 {
177 t.Error("DepthMatchAll should have returned b2, got:", ps[0].FileName)
178 }
179 if ps[1] != a3 {
180 t.Error("DepthMatchAll should have returned a3, got:", ps[1].FileName)
181 }
182 }