forked from NerdGGuy/go-imap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser_test.go
112 lines (98 loc) · 3.44 KB
/
parser_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
package imap
import (
"bytes"
"fmt"
"os"
"reflect"
"testing"
)
func testError(t *testing.T, err os.Error, format string, args ...interface{}) {
if err != nil {
t.Fatalf("%s: %s", fmt.Sprintf(format, args...), err)
}
}
type parseTest struct {
input string
code func(p *parser) (interface{}, os.Error)
expected interface{}
}
func (test parseTest) Run(t *testing.T) {
p := newParser(bytes.NewBufferString(test.input))
ps, err := test.code(p)
if err != nil {
t.Fatalf("parsing %s: %s", test.input, err)
}
_, err = p.ReadByte()
if err != nil {
if err != os.EOF {
t.Fatalf("parsing %s: %s", test.input, err)
}
} else {
t.Fatalf("parsing %s: expected EOF", test.input)
}
if !reflect.DeepEqual(ps, test.expected) {
t.Fatalf("DeepEqual(%#v, %#v)", ps, test.expected)
}
}
func TestParseString(t *testing.T) {
parseTest{
input: "\"foo bar\"",
code: func(p *parser) (interface{}, os.Error) { return p.readQuoted() },
expected: "foo bar",
}.Run(t)
}
func TestParseLiteral(t *testing.T) {
tests := []parseTest{
{
input: "{5}\r\n01234",
code: func(p *parser) (interface{}, os.Error) { return p.readLiteral() },
expected: []byte("01234"),
},
{
input: "({2}\r\nAB abc)",
code: func(p *parser) (interface{}, os.Error) { return p.readSexp() },
expected: []sexp{[]byte("AB"), "abc"},
},
}
for _, test := range tests {
test.Run(t)
}
}
func TestParseSimple(t *testing.T) {
tests := []parseTest{
{
input: "(\\HasNoChildren \\Foo)",
code: func(p *parser) (interface{}, os.Error) {
return p.readParenStringList()
},
expected: []string{"\\HasNoChildren", "\\Foo"},
},
}
for _, test := range tests {
test.Run(t)
}
}
func TestParseComplex(t *testing.T) {
parseTest{
input: `(ENVELOPE ("Fri, 14 Oct 2011 13:51:22 -0700" "Re: [PATCH 1/1] added code to export CAP_LAST_CAP in /proc/sys/kernel modeled after ngroups_max" (("Andrew Morton" NIL "akpm" "linux-foundation.org")) ((NIL NIL "linux-kernel-owner" "vger.kernel.org")) (("Andrew Morton" NIL "akpm" "linux-foundation.org")) (("Dan Ballard" NIL "dan" "mindstab.net")) (("Ingo Molnar" NIL "mingo" "elte.hu") ("Lennart Poettering" NIL "lennart" "poettering.net") ("Kay Sievers" NIL "kay.sievers" "vrfy.org") (NIL NIL "linux-kernel" "vger.kernel.org")) NIL "<1318460194-31983-1-git-send-email-dan@mindstab.net>" "<20111014135122.4bb95565.akpm@linux-foundation.org>") FLAGS () INTERNALDATE "14-Oct-2011 20:51:30 +0000" RFC822.SIZE 4623)`,
code: func(p *parser) (interface{}, os.Error) { return p.readSexp() },
expected: []sexp{"ENVELOPE",
[]sexp{"Fri, 14 Oct 2011 13:51:22 -0700",
"Re: [PATCH 1/1] added code to export CAP_LAST_CAP in /proc/sys/kernel modeled after ngroups_max",
[]sexp{[]sexp{"Andrew Morton", nil, "akpm", "linux-foundation.org"}},
[]sexp{[]sexp{nil, nil, "linux-kernel-owner", "vger.kernel.org"}},
[]sexp{[]sexp{"Andrew Morton", nil, "akpm", "linux-foundation.org"}},
[]sexp{[]sexp{"Dan Ballard", nil, "dan", "mindstab.net"}},
[]sexp{[]sexp{"Ingo Molnar", nil, "mingo", "elte.hu"}, []sexp{"Lennart Poettering", nil, "lennart", "poettering.net"}, []sexp{"Kay Sievers", nil, "kay.sievers", "vrfy.org"}, []sexp{nil, nil, "linux-kernel", "vger.kernel.org"}},
nil,
"<1318460194-31983-1-git-send-email-dan@mindstab.net>",
"<20111014135122.4bb95565.akpm@linux-foundation.org>"},
"FLAGS",
[]sexp{},
"INTERNALDATE",
"14-Oct-2011 20:51:30 +0000",
"RFC822.SIZE",
"4623",
},
}.Run(t)
}