-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscanner_test.go
64 lines (61 loc) · 1.83 KB
/
scanner_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
package iptables_parser
import (
"strings"
"testing"
)
func TestScanner_Scan(t *testing.T) {
for i, tc := range []struct {
s string
tok Token
lit string
}{
{s: "", tok: EOF, lit: ""},
{s: `# `, tok: COMMENTLINE, lit: ` `},
{s: `#`, tok: COMMENTLINE, lit: ""},
{s: "-src", tok: FLAG, lit: "-src"},
{s: "--src", tok: FLAG, lit: "--src"},
{s: "-src=", tok: FLAG, lit: "-src"},
{s: " -src=", tok: WS, lit: " "},
{s: "--destination-port", tok: FLAG, lit: "--destination-port"},
{s: "---destination-port", tok: FLAG, lit: "---destination-port"},
{s: " ", tok: WS, lit: " "},
{s: "\"hello my friend\" ", tok: COMMENT, lit: `hello my friend`},
{s: `"hello 'my' friend" `, tok: COMMENT, lit: `hello 'my' friend`},
{s: `"hello \"my\" friend" bla`, tok: COMMENT, lit: "hello \\\"my\\\" friend"},
{s: `"hello \"my\" friend bla\`, tok: COMMENT, lit: "hello \\\"my\\\" friend bla\\"},
{s: "hello friend", tok: IDENT, lit: "hello"},
{s: "192.168.178.2/24", tok: IDENT, lit: "192.168.178.2/24"},
{s: "2001:db8::/64", tok: IDENT, lit: "2001:db8::/64"},
{s: "# 192.168.178.2/24", tok: COMMENTLINE, lit: " 192.168.178.2/24"},
{s: "I_test_rule-something", tok: IDENT, lit: "I_test_rule-something"},
} {
s := newScanner(strings.NewReader(tc.s))
tok, lit := s.scan()
if tc.tok != tok {
t.Errorf("%d. %q token mismatch: exp=%q got=%q <%q>", i, tc.s, tc.tok, tok, lit)
} else if tc.lit != lit {
t.Errorf("%d. %q literal mismatch: exp=%q got=%q", i, tc.s, tc.lit, lit)
}
}
}
func TestScanner_readLine(t *testing.T) {
for i, tc := range []struct {
s string
r string
}{
{
s: "hello you\nasdf",
r: "hello you",
},
{
s: "hello",
r: "hello",
},
} {
s := newScanner(strings.NewReader(tc.s))
str := s.scanLine()
if tc.r != str {
t.Errorf("%d. mismatch: exp=%q got=%q", i, tc.r, str)
}
}
}