-
Notifications
You must be signed in to change notification settings - Fork 3
/
execute_test.go
121 lines (107 loc) · 2.71 KB
/
execute_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
package sieve
import (
"bufio"
"context"
"net/textproto"
"reflect"
"strings"
"testing"
"github.com/foxcpp/go-sieve/interp"
)
var eml string = `Date: Tue, 1 Apr 1997 09:06:31 -0800 (PST)
From: coyote@desert.example.org
To: roadrunner@acme.example.com
Subject: I have a present for you
Look, I'm sorry about the whole anvil thing, and I really
didn't mean to try and drop it on you from the top of the
cliff. I want to try to make it up to you. I've got some
great birdseed over here at my place--top of the line
stuff--and if you come by, I'll have it all wrapped up
for you. I'm really sorry for all the problems I've caused
for you over the years, but I know we can work this out.
--
Wile E. Coyote "Super Genius" coyote@desert.example.org
`
type result struct {
redirect []string
fileinto []string
implicitKeep bool
keep bool
flags []string
}
func testExecute(t *testing.T, in string, eml string, intendedResult result) {
t.Run("case", func(t *testing.T) {
msgHdr, err := textproto.NewReader(bufio.NewReader(strings.NewReader(eml))).ReadMIMEHeader()
if err != nil {
t.Fatal(err)
}
script := bufio.NewReader(strings.NewReader(in))
loadedScript, err := Load(script, DefaultOptions())
if err != nil {
t.Fatal(err)
}
env := interp.EnvelopeStatic{
From: "from@test.com",
To: "to@test.com",
}
msg := interp.MessageStatic{
Size: len(eml),
Header: msgHdr,
}
data := interp.NewRuntimeData(loadedScript, interp.DummyPolicy{},
env, msg)
ctx := context.Background()
if err := loadedScript.Execute(ctx, data); err != nil {
t.Fatal(err)
}
r := result{
redirect: data.RedirectAddr,
fileinto: data.Mailboxes,
keep: data.Keep,
implicitKeep: data.ImplicitKeep,
flags: data.Flags,
}
if !reflect.DeepEqual(r, intendedResult) {
t.Log("Wrong Execute output")
t.Log("Actual: ", r)
t.Log("Expected:", intendedResult)
t.Fail()
}
})
}
func TestFileinto(t *testing.T) {
testExecute(t, `require ["fileinto"];
fileinto "test";
`, eml,
result{
fileinto: []string{"test"},
})
testExecute(t, `require ["fileinto"];
fileinto "test";
fileinto "test2";
`, eml,
result{
fileinto: []string{"test", "test2"},
})
}
func TestFlags(t *testing.T) {
testExecute(t, `require ["fileinto", "imap4flags"];
setflag ["flag1", "flag2"];
addflag ["flag2", "flag3"];
removeflag ["flag1"];
fileinto "test";
`, eml,
result{
fileinto: []string{"test"},
flags: []string{"flag2", "flag3"},
})
testExecute(t, `require ["fileinto", "imap4flags"];
addflag ["flag2", "flag3"];
removeflag ["flag3", "flag4"];
fileinto "test";
`, eml,
result{
fileinto: []string{"test"},
flags: []string{"flag2"},
})
}