forked from emersion/go-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_test.go
137 lines (125 loc) · 3.84 KB
/
search_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 imap
import (
"bytes"
"io"
"net/textproto"
"reflect"
"strings"
"testing"
"time"
)
// Note to myself: writing these boring tests actually fixed 2 bugs :P
var searchSeqSet1, _ = ParseSeqSet("1:42")
var searchSeqSet2, _ = ParseSeqSet("743:938")
var searchDate1 = time.Date(1997, 11, 21, 0, 0, 0, 0, time.UTC)
var searchDate2 = time.Date(1984, 11, 5, 0, 0, 0, 0, time.UTC)
var searchCriteriaTests = []struct {
expected string
criteria *SearchCriteria
}{
{
expected: `(1:42 UID 743:938 ` +
`SINCE "5-Nov-1984" BEFORE "21-Nov-1997" SENTSINCE "5-Nov-1984" SENTBEFORE "21-Nov-1997" ` +
`FROM "[email protected]" BODY "hey there" TEXT "DILLE" ` +
`ANSWERED DELETED KEYWORD "cc" UNKEYWORD "microsoft" ` +
`LARGER 4242 SMALLER 4342 ` +
`NOT (SENTON "21-Nov-1997" HEADER "Content-Type" "text/csv") ` +
`OR (ON "5-Nov-1984" DRAFT FLAGGED UNANSWERED UNDELETED OLD) (UNDRAFT UNFLAGGED UNSEEN))`,
criteria: &SearchCriteria{
SeqNum: searchSeqSet1,
Uid: searchSeqSet2,
Since: searchDate2,
Before: searchDate1,
SentSince: searchDate2,
SentBefore: searchDate1,
Header: textproto.MIMEHeader{
"From": {"[email protected]"},
},
Body: []string{"hey there"},
Text: []string{"DILLE"},
WithFlags: []string{AnsweredFlag, DeletedFlag, "cc"},
WithoutFlags: []string{"microsoft"},
Larger: 4242,
Smaller: 4342,
Not: []*SearchCriteria{{
SentSince: searchDate1,
SentBefore: searchDate1.Add(24 * time.Hour),
Header: textproto.MIMEHeader{
"Content-Type": {"text/csv"},
},
}},
Or: [][2]*SearchCriteria{{
{
Since: searchDate2,
Before: searchDate2.Add(24 * time.Hour),
WithFlags: []string{DraftFlag, FlaggedFlag},
WithoutFlags: []string{AnsweredFlag, DeletedFlag, RecentFlag},
},
{
WithoutFlags: []string{DraftFlag, FlaggedFlag, SeenFlag},
},
}},
},
},
}
func TestSearchCriteria_Format(t *testing.T) {
for i, test := range searchCriteriaTests {
fields := test.criteria.Format()
got, err := formatFields(fields)
if err != nil {
t.Fatal("Unexpected no error while formatting fields, got:", err)
}
if got != test.expected {
t.Errorf("Invalid search criteria fields for #%v: got \n%v\n instead of \n%v", i+1, got, test.expected)
}
}
}
func TestSearchCriteria_Parse(t *testing.T) {
for i, test := range searchCriteriaTests {
criteria := new(SearchCriteria)
b := bytes.NewBuffer([]byte(test.expected))
r := NewReader(b)
fields, _ := r.ReadFields()
if err := criteria.ParseWithCharset(fields[0].([]interface{}), nil); err != nil {
t.Errorf("Cannot parse search criteria for #%v: %v", i+1, err)
} else if !reflect.DeepEqual(criteria, test.criteria) {
t.Errorf("Invalid search criteria for #%v: got \n%+v\n instead of \n%+v", i+1, criteria, test.criteria)
}
}
}
var searchCriteriaParseTests = []struct {
fields []interface{}
criteria *SearchCriteria
charset func(io.Reader) io.Reader
}{
{
fields: []interface{}{"ALL"},
criteria: &SearchCriteria{},
},
{
fields: []interface{}{"NEW"},
criteria: &SearchCriteria{
WithFlags: []string{RecentFlag},
WithoutFlags: []string{SeenFlag},
},
},
{
fields: []interface{}{"SUBJECT", strings.NewReader("café")},
criteria: &SearchCriteria{
Header: textproto.MIMEHeader{"Subject": {"café"}},
},
charset: func(r io.Reader) io.Reader {
return r
},
},
}
func TestSearchCriteria_Parse_others(t *testing.T) {
for i, test := range searchCriteriaParseTests {
criteria := new(SearchCriteria)
if err := criteria.ParseWithCharset(test.fields, test.charset); err != nil {
t.Errorf("Cannot parse search criteria for #%v: %v", i+1, err)
} else if !reflect.DeepEqual(criteria, test.criteria) {
t.Errorf("Invalid search criteria for #%v: got \n%+v\n instead of \n%+v", i+1, criteria, test.criteria)
}
}
}