This repository has been archived by the owner on May 27, 2021. It is now read-only.
forked from kvannotten/mailstrip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailstrip_test.go
330 lines (300 loc) · 8.87 KB
/
mailstrip_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package mailstrip
import (
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"runtime"
"testing"
)
var tests = []struct {
name string // name of the test, from email_reply_parser
fixture string // fixture file to parse
checks []checker // checks to perform
}{
{
"test_reads_simple_body",
"email_1_1",
[]checker{
&attributeChecker{"Quoted", []bool{false, false, false}},
&attributeChecker{"Signature", []bool{false, true, true}},
&attributeChecker{"Hidden", []bool{false, true, true}},
&fragmentStringChecker{
0, equalsString(`Hi folks
What is the best way to clear a Riak bucket of all key, values after
running a test?
I am currently using the Java HTTP API.
`),
},
},
},
{
"test_reads_top_post",
"email_1_3",
[]checker{
&attributeChecker{"Quoted", []bool{false, false, true, false, false}},
&attributeChecker{"Hidden", []bool{false, true, true, true, true}},
&attributeChecker{"Signature", []bool{false, true, false, false, true}},
&fragmentStringChecker{0, regexp.MustCompile("(?m)^Oh thanks.\n\nHaving")},
&fragmentStringChecker{1, regexp.MustCompile("(?m)^-A")},
&fragmentStringChecker{2, regexp.MustCompile("(?m)^On [^\\:]+\\:|(?m)^Op [^\\:]+\\:")},
&fragmentStringChecker{4, regexp.MustCompile("^_")},
},
},
{
"test_reads_bottom_post",
"email_1_2",
[]checker{
&attributeChecker{"Quoted", []bool{false, true, false, true, false, false}},
&attributeChecker{"Signature", []bool{false, false, false, false, false, true}},
&attributeChecker{"Hidden", []bool{false, false, false, true, true, true}},
&fragmentStringChecker{0, equalsString("Hi,")},
&fragmentStringChecker{1, regexp.MustCompile("(?m)^On [^\\:]+\\:|(?m)^Op [^\\:]+\\:")},
&fragmentStringChecker{2, regexp.MustCompile("(?m)^You can list")},
&fragmentStringChecker{3, regexp.MustCompile("(?m)^> ")},
&fragmentStringChecker{5, regexp.MustCompile("(?m)^_")},
},
},
{
"test_recognizes_date_string_above_quote",
"email_1_4",
[]checker{
&fragmentStringChecker{
0, equalsString(`Awesome! I haven't had another problem with it.`),
},
},
},
{
"test_a_complex_body_with_only_one_fragment",
"email_1_5",
[]checker{fragmentCountChecker(1)},
},
{
"test_reads_email_with_correct_signature",
"correct_sig",
[]checker{
&attributeChecker{"Quoted", []bool{false, false}},
&attributeChecker{"Signature", []bool{false, true}},
&attributeChecker{"Hidden", []bool{false, true}},
&fragmentStringChecker{1, regexp.MustCompile("(?m)^-- \nrick")},
},
},
{
"test_deals_with_multiline_reply_headers",
"email_1_6",
[]checker{
&fragmentStringChecker{0, regexp.MustCompile("(?m)^I get")},
&fragmentStringChecker{1, regexp.MustCompile("(?m)^On|(?m)^Op")},
&fragmentStringChecker{1, regexp.MustCompile("Was this")},
},
},
{
"test_deals_with_windows_line_endings",
"email_1_7",
[]checker{
&fragmentStringChecker{0, regexp.MustCompile(":\\+1:")},
},
},
{
"test_returns_only_the_visible_fragments_as_a_string",
"email_2_1",
// The original test re-implements the visible_text function which seems
// less useful than asserting on the result as i'm doing here. However, it
// means that this test is a duplicate of
// test_parse_out_just_top_for_outlook_reply.
[]checker{&emailStringChecker{equalsString("Outlook with a reply")}},
},
{
"test_parse_out_just_top_for_outlook_reply",
"email_2_1",
[]checker{&emailStringChecker{equalsString("Outlook with a reply")}},
},
{
"test_parse_out_just_top_for_inbox_reply_swe",
"email_2_2",
[]checker{&emailStringChecker{equalsString("shysst! Vad är du beredd att betala då ?")}},
},
{
"test_outlook_win10_reply",
"email_2_3",
[]checker{&emailStringChecker{equalsString("Outlook message reply test")}},
},
{
"test_parse_out_sent_from_iPhone",
"email_iPhone",
[]checker{&emailStringChecker{equalsString("Here is another email")}},
},
{
"test_parse_out_sent_from_BlackBerry",
"email_BlackBerry",
[]checker{&emailStringChecker{equalsString("Here is another email")}},
},
{
"test_parse_out_send_from_multiword_mobile_device",
"email_multi_word_sent_from_my_mobile_device",
[]checker{&emailStringChecker{equalsString("Here is another email")}},
},
{
"test_do_not_parse_out_send_from_in_regular_sentence",
"email_sent_from_my_not_signature",
[]checker{&emailStringChecker{equalsString("Here is another email\n\nSent from my desk, is much easier then my mobile phone.")}},
},
{
"test_retains_bullets",
"email_bullets",
[]checker{
&emailStringChecker{
equalsString("test 2 this should list second\n\nand have spaces\n\nand retain this formatting\n\n\n - how about bullets\n - and another"),
},
},
},
// test_parse_reply is not ported, as it's specific to the email_reply_parser
// API.
{
"test_one_is_not_on",
"email_one_is_not_on",
[]checker{
&fragmentStringChecker{0, regexp.MustCompile("One outstanding question")},
},
},
// the tests below are mailstrip specific
{
"forward text should be non-Hidden()",
"forward",
[]checker{
&emailStringChecker{regexp.MustCompile("(?m).*check out the joke below.*")},
&emailStringChecker{regexp.MustCompile("(?m).*You must work in management.*")},
&attributeChecker{"Quoted", []bool{false, false}},
&attributeChecker{"Hidden", []bool{false, false}},
&attributeChecker{"Signature", []bool{false, false}},
&attributeChecker{"Forwarded", []bool{false, true}},
},
},
{
"yahoo reply quotes should be handled",
"yahoo",
[]checker{
&emailStringChecker{equalsString("who is using yahoo?")},
&attributeChecker{"Quoted", []bool{false, true}},
&attributeChecker{"Hidden", []bool{false, true}},
&attributeChecker{"Signature", []bool{false, false}},
&attributeChecker{"Forwarded", []bool{false, false}},
},
},
{
"gmail alternative quote header should be handled",
"gmail_alt_quoteheader",
[]checker{
&emailStringChecker{equalsString("Fine, and you?")},
},
},
}
var languages = []string{"en", "nl"}
func TestParse(t *testing.T) {
for _, test := range tests {
for _, language := range languages {
// t.Logf("===== %s.%s - %s =====", test.fixture, test.name, language)
text, err := loadFixture(test.fixture, language)
if err != nil {
t.Errorf("could not load fixture: %s", err)
continue
}
parsed := Parse(text)
var hadError bool
for _, check := range test.checks {
if err := check.Check(parsed); err != nil {
hadError = true
// t.Error(err)
}
}
if hadError {
//for i, fragment := range parsed {
// t.Logf("ERROR FRAGMENT #%d: %#v", i, fragment)
//}
t.Errorf("ERROR: %v %v %v", language, test.fixture, test.name)
t.Error(parsed.String())
}
}
}
}
type checker interface {
Check(email Email) error
}
type attributeChecker struct {
attribute string
values []bool
}
func (c *attributeChecker) Check(email Email) error {
expectedCount := len(c.values)
gotCount := len(email)
if gotCount != expectedCount {
return fmt.Errorf("wrong fragment count: %d != %d", gotCount, expectedCount)
}
for i, fragment := range email {
var val bool
// could also use reflect, but seems overkill for this
switch c.attribute {
case "Hidden":
val = fragment.Hidden()
case "Quoted":
val = fragment.Quoted()
case "Signature":
val = fragment.Signature()
case "Forwarded":
val = fragment.Forwarded()
default:
return fmt.Errorf("Unknown attribute: %s", c.attribute)
}
if val != c.values[i] {
return fmt.Errorf("Invalid %s() value in fragment #%d: %t != %t", c.attribute, i, val, c.values[i])
}
}
return nil
}
type emailStringChecker struct {
content stringMatcher
}
func (c *emailStringChecker) Check(email Email) error {
content := email.String()
if !c.content.MatchString(content) {
return fmt.Errorf("email String(): %q did not match %T(%s)", content, c.content, c.content)
}
return nil
}
type fragmentStringChecker struct {
fragmentId int
content stringMatcher
}
func (c *fragmentStringChecker) Check(email Email) error {
fragment := email[c.fragmentId]
content := fragment.String()
if !c.content.MatchString(content) {
return fmt.Errorf("fragment %d String(): %q did not match %s", c.fragmentId, content, c.content)
}
return nil
}
type fragmentCountChecker int
func (c fragmentCountChecker) Check(email Email) error {
expectedCount := int(c)
gotCount := len(email)
if gotCount != expectedCount {
return fmt.Errorf("wrong fragment count: %d != %d", gotCount, expectedCount)
}
return nil
}
type stringMatcher interface {
MatchString(string) bool
}
type equalsString string
func (s equalsString) MatchString(str string) bool {
return str == string(s)
}
var (
_, srcPath, _, _ = runtime.Caller(0)
fixturesDir = filepath.Join(filepath.Dir(srcPath), "fixtures")
)
func loadFixture(name string, language string) (string, error) {
fixturePath := filepath.Join(fixturesDir, language, name+".txt")
data, err := ioutil.ReadFile(fixturePath)
return string(data), err
}