-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
99 lines (92 loc) · 1.87 KB
/
config_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
package main
import (
"strings"
"testing"
)
type configTestCase struct {
text string
expected configActions
}
type configTest struct {
text string
err string
checks []configTestCase
}
var configTests = []configTest{
{
text: `
# comment
google.com https proxy
*.google.com https direct
# another comment
ads.whatever.com block
`,
checks: []configTestCase{
{"google.com", actionProxy | actionForceHTTPS},
{"www.google.com", actionDirect | actionForceHTTPS},
{"www.more.google.com", actionDirect | actionForceHTTPS},
{"1google.com", actionNone},
},
},
{
text: `
# these are good
google.com https
ads.whatever.com block
# this line is bad
unexpected
`,
err: "cannot parse: unexpected",
},
{
text: `
# these are good
google.com https
ads.whatever.com block
# this line is bad
unexpected action
`,
err: "unknown action: \"action\"",
},
{
text: ``,
checks: []configTestCase{
{"google.com", actionNone},
},
},
}
func runChecks(t *testing.T, index int, config *config, checks []configTestCase) {
for _, tc := range checks {
var actions configActions
for _, c := range config.cases {
if c.mask.MatchString(tc.text) {
actions = c.actions
break
}
}
if actions != tc.expected {
t.Fatalf("config #%d %s: %v (expected %v)", index, tc.text, actions, tc.expected)
}
}
}
func TestConfig(t *testing.T) {
for index, ct := range configTests {
c, err := loadConfigReader(strings.NewReader(ct.text))
if ct.err != "" {
if err == nil {
t.Fatalf("config #%d: expected error, got nil", index)
}
if ct.err != err.Error() {
t.Fatalf("config #%d: expected %q, got %q", index, ct.err, err.Error())
}
if c != nil {
t.Fatalf("config #%d: expected nil config, got %#v", index, c)
}
continue
}
if err != nil {
t.Fatalf("config #%d: unexpected error: %s", index, err)
}
runChecks(t, index, c, ct.checks)
}
}