-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex_test.go
97 lines (94 loc) · 1.88 KB
/
lex_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
package vera
import "testing"
func TestLex(t *testing.T) {
type testCase struct {
input string
expected []lexeme
}
for _, c := range []testCase{
{"a", []lexeme{{ltStatement, 'a'}}},
{"(a)", []lexeme{
{ltOpenParen, '('},
{ltStatement, 'a'},
{ltCloseParen, ')'},
}},
{"a > b", []lexeme{
{ltStatement, 'a'},
{ltOperator, '>'},
{ltStatement, 'b'},
}},
{" ( a >b) & 1 ", []lexeme{
{ltOpenParen, '('},
{ltStatement, 'a'},
{ltOperator, '>'},
{ltStatement, 'b'},
{ltCloseParen, ')'},
{ltOperator, '&'},
{ltTrue, '1'},
}},
{"!(!(a = b) | !0) > (c ^ d)", []lexeme{
{ltNegate, '!'},
{ltOpenParen, '('},
{ltNegate, '!'},
{ltOpenParen, '('},
{ltStatement, 'a'},
{ltOperator, '='},
{ltStatement, 'b'},
{ltCloseParen, ')'},
{ltOperator, '|'},
{ltNegate, '!'},
{ltFalse, '0'},
{ltCloseParen, ')'},
{ltOperator, '>'},
{ltOpenParen, '('},
{ltStatement, 'c'},
{ltOperator, '^'},
{ltStatement, 'd'},
{ltCloseParen, ')'},
}},
} {
idx := 0
for r := range lex(c.input) {
switch {
case r.err != nil:
t.Fatalf("error occurred while lexing: %v (input: %s)", r.err, c.input)
case idx == len(c.expected):
t.Fatalf("too many lexemes (input: %s)", c.input)
case r.l != c.expected[idx]:
t.Fatalf("expected %v; got %v (input: %s)", c.expected[idx], r.l, c.input)
}
idx++
}
}
}
func TestLexError(t *testing.T) {
type testCase struct {
input string
error bool
}
for _, c := range []testCase{
{"", true},
{"()", true},
{">", true},
{"(=)", true},
{"(", true},
{")", true},
{"a >", true},
{"^ a", true},
{"(a & b > c)", false},
} {
err := false
for r := range lex(c.input) {
if r.err != nil {
err = true
}
}
if err != c.error {
fill := ""
if !c.error {
fill = "not "
}
t.Fatalf("expected '%s' to %serror", c.input, fill)
}
}
}