forked from emersion/go-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailbox_test.go
191 lines (170 loc) · 6.17 KB
/
mailbox_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
package imap_test
import (
"fmt"
"reflect"
"sort"
"testing"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/internal"
)
func TestCanonicalMailboxName(t *testing.T) {
if got := imap.CanonicalMailboxName("Inbox"); got != imap.InboxName {
t.Errorf("Invalid canonical mailbox name: expected %q but got %q", imap.InboxName, got)
}
if got := imap.CanonicalMailboxName("Drafts"); got != "Drafts" {
t.Errorf("Invalid canonical mailbox name: expected %q but got %q", "Drafts", got)
}
}
var mailboxInfoTests = []struct {
fields []interface{}
info *imap.MailboxInfo
}{
{
fields: []interface{}{
[]interface{}{"\\Noselect", "\\Recent", "\\Unseen"},
"/",
"INBOX",
},
info: &imap.MailboxInfo{
Attributes: []string{"\\Noselect", "\\Recent", "\\Unseen"},
Delimiter: "/",
Name: "INBOX",
},
},
}
func TestMailboxInfo_Parse(t *testing.T) {
for _, test := range mailboxInfoTests {
info := &imap.MailboxInfo{}
if err := info.Parse(test.fields); err != nil {
t.Fatal(err)
}
if fmt.Sprint(info.Attributes) != fmt.Sprint(test.info.Attributes) {
t.Fatal("Invalid flags:", info.Attributes)
}
if info.Delimiter != test.info.Delimiter {
t.Fatal("Invalid delimiter:", info.Delimiter)
}
if info.Name != test.info.Name {
t.Fatal("Invalid name:", info.Name)
}
}
}
func TestMailboxInfo_Format(t *testing.T) {
for _, test := range mailboxInfoTests {
fields := test.info.Format()
if fmt.Sprint(fields) != fmt.Sprint(test.fields) {
t.Fatal("Invalid fields:", fields)
}
}
}
var mailboxInfoMatchTests = []struct {
name, ref, pattern string
result bool
}{
{name: "INBOX", pattern: "INBOX", result: true},
{name: "INBOX", pattern: "Asuka", result: false},
{name: "INBOX", pattern: "*", result: true},
{name: "INBOX", pattern: "%", result: true},
{name: "Neon Genesis Evangelion/Misato", pattern: "*", result: true},
{name: "Neon Genesis Evangelion/Misato", pattern: "%", result: false},
{name: "Neon Genesis Evangelion/Misato", pattern: "Neon Genesis Evangelion/*", result: true},
{name: "Neon Genesis Evangelion/Misato", pattern: "Neon Genesis Evangelion/%", result: true},
{name: "Neon Genesis Evangelion/Misato", pattern: "Neo* Evangelion/Misato", result: true},
{name: "Neon Genesis Evangelion/Misato", pattern: "Neo% Evangelion/Misato", result: true},
{name: "Neon Genesis Evangelion/Misato", pattern: "*Eva*/Misato", result: true},
{name: "Neon Genesis Evangelion/Misato", pattern: "%Eva%/Misato", result: true},
{name: "Neon Genesis Evangelion/Misato", pattern: "*X*/Misato", result: false},
{name: "Neon Genesis Evangelion/Misato", pattern: "%X%/Misato", result: false},
{name: "Neon Genesis Evangelion/Misato", pattern: "Neon Genesis Evangelion/Mi%o", result: true},
{name: "Neon Genesis Evangelion/Misato", pattern: "Neon Genesis Evangelion/Mi%too", result: false},
{name: "Misato/Misato", pattern: "Mis*to/Misato", result: true},
{name: "Misato/Misato", pattern: "Mis*to", result: true},
{name: "Misato/Misato/Misato", pattern: "Mis*to/Mis%to", result: true},
{name: "Misato/Misato", pattern: "Mis**to/Misato", result: true},
{name: "Misato/Misato", pattern: "Misat%/Misato", result: true},
{name: "Misato/Misato", pattern: "Misat%Misato", result: false},
{name: "Misato/Misato", ref: "Misato", pattern: "Misato", result: true},
{name: "Misato/Misato", ref: "Misato/", pattern: "Misato", result: true},
{name: "Misato/Misato", ref: "Shinji", pattern: "/Misato/*", result: true},
{name: "Misato/Misato", ref: "Misato", pattern: "/Misato", result: false},
{name: "Misato/Misato", ref: "Misato", pattern: "Shinji", result: false},
{name: "Misato/Misato", ref: "Shinji", pattern: "Misato", result: false},
}
func TestMailboxInfo_Match(t *testing.T) {
for _, test := range mailboxInfoMatchTests {
info := &imap.MailboxInfo{Name: test.name, Delimiter: "/"}
result := info.Match(test.ref, test.pattern)
if result != test.result {
t.Errorf("Matching name %q with pattern %q and reference %q returns %v, but expected %v", test.name, test.pattern, test.ref, result, test.result)
}
}
}
func TestNewMailboxStatus(t *testing.T) {
status := imap.NewMailboxStatus("INBOX", []imap.StatusItem{imap.StatusMessages, imap.StatusUnseen})
expected := &imap.MailboxStatus{
Name: "INBOX",
Items: map[imap.StatusItem]interface{}{imap.StatusMessages: nil, imap.StatusUnseen: nil},
}
if !reflect.DeepEqual(expected, status) {
t.Errorf("Invalid mailbox status: expected \n%+v\n but got \n%+v", expected, status)
}
}
var mailboxStatusTests = [...]struct {
fields []interface{}
status *imap.MailboxStatus
}{
{
fields: []interface{}{
"MESSAGES", uint32(42),
"RECENT", uint32(1),
"UNSEEN", uint32(6),
"UIDNEXT", uint32(65536),
"UIDVALIDITY", uint32(4242),
},
status: &imap.MailboxStatus{
Items: map[imap.StatusItem]interface{}{
imap.StatusMessages: nil,
imap.StatusRecent: nil,
imap.StatusUnseen: nil,
imap.StatusUidNext: nil,
imap.StatusUidValidity: nil,
},
Messages: 42,
Recent: 1,
Unseen: 6,
UidNext: 65536,
UidValidity: 4242,
},
},
}
func TestMailboxStatus_Parse(t *testing.T) {
for i, test := range mailboxStatusTests {
status := &imap.MailboxStatus{}
if err := status.Parse(test.fields); err != nil {
t.Errorf("Expected no error while parsing mailbox status #%v, got: %v", i, err)
continue
}
if !reflect.DeepEqual(status, test.status) {
t.Errorf("Invalid parsed mailbox status for #%v: got \n%+v\n but expected \n%+v", i, status, test.status)
}
}
}
func TestMailboxStatus_Format(t *testing.T) {
for i, test := range mailboxStatusTests {
fields := test.status.Format()
// MapListSorter does not know about RawString and will panic.
stringFields := make([]interface{}, 0, len(fields))
for _, field := range fields {
if s, ok := field.(imap.RawString); ok {
stringFields = append(stringFields, string(s))
} else {
stringFields = append(stringFields, field)
}
}
sort.Sort(internal.MapListSorter(stringFields))
sort.Sort(internal.MapListSorter(test.fields))
if !reflect.DeepEqual(stringFields, test.fields) {
t.Errorf("Invalid mailbox status fields for #%v: got \n%+v\n but expected \n%+v", i, fields, test.fields)
}
}
}