-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_test.go
71 lines (59 loc) · 1.52 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
package imap_test
import (
"bytes"
"io"
"testing"
"time"
"github.com/paulrosania/go-imap"
"github.com/stretchr/testify/assert"
)
type Buffer struct {
io.ReadWriter
}
func (b *Buffer) Close() error {
return nil
}
func NewBuffer(input string) *Buffer {
// Parser always reads lines, so make sure to give it one
return &Buffer{bytes.NewBufferString(input + "\r\n")}
}
func NewParser(input string) *imap.Parser {
rwc := NewBuffer(input)
conn := imap.NewConn(rwc)
parser := imap.NewParser(conn)
return parser
}
func TestReadSearch(t *testing.T) {
cs, actual := NewParser(` FLAGGED SINCE 1-Feb-1994 NOT FROM "Smith"`).ReadSearch()
expected := &imap.BooleanTerm{
Op: imap.OpAnd,
Terms: []imap.Term{
&imap.FlagTerm{Flag: imap.FlagFlagged, Present: true},
&imap.DateTerm{
Op: imap.OpGTE,
Field: imap.InternalDateField,
Date: time.Date(1994, time.February, 1, 0, 0, 0, 0, time.UTC),
},
&imap.UnaryTerm{
Op: imap.OpNot,
Term: &imap.StringTerm{
Op: imap.OpContains,
Field: imap.FromField,
String: "Smith",
},
},
},
}
assert.Equal(t, "us-ascii", cs, "charset should default to US-ASCII")
assert.Equal(t, expected, actual, "parsed search query incorrectly")
}
func TestSearchParsesCharset(t *testing.T) {
cs, actual := NewParser(` CHARSET UTF-8 TEXT XXXXXX`).ReadSearch()
expected := &imap.StringTerm{
Op: imap.OpContains,
Field: imap.TextField,
String: "XXXXXX",
}
assert.Equal(t, "UTF-8", cs, "charset should be UTF-8")
assert.Equal(t, expected, actual)
}