-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_test.go
197 lines (176 loc) · 4.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package liquid
// https://github.com/Shopify/liquid/blob/master/test/unit/parser_unit_test.rb
import "testing"
// checkConsume tests the result of a consume call
func checkConsume(t *testing.T, p *Parser, tType, want string) {
got, err := p.consume(tType)
if err != nil {
t.Error(err)
return
}
if got != want {
t.Errorf("Bad token, want: %v, got: %v", want, got)
}
}
func TestConsume(t *testing.T) {
p, err := NewParser("wat: 7")
if err != nil {
t.Error(err)
return
}
checkConsume(t, p, tIdentifier, "wat")
checkConsume(t, p, tColon, ":")
checkConsume(t, p, tNumberLiteral, "7")
}
func TestJump(t *testing.T) {
p, err := NewParser("wat: 7")
if err != nil {
t.Error(err)
return
}
p.jump(2)
checkConsume(t, p, tNumberLiteral, "7")
}
// checkTryConsume checks the result of an attempted consume call
func checkTryConsume(t *testing.T, p *Parser, tType, want string, expectSuccess bool) {
got, err := p.consume(tType)
if err != nil && expectSuccess {
t.Error(err)
return
}
if got != want {
t.Errorf("Bad token, want: %v, got: %v", want, got)
}
}
func TestTryConsume(t *testing.T) {
p, err := NewParser("wat: 7")
if err != nil {
t.Error(err)
return
}
checkTryConsume(t, p, tIdentifier, "wat", true)
checkTryConsume(t, p, tDot, "", false)
checkTryConsume(t, p, tColon, ":", true)
checkTryConsume(t, p, tNumberLiteral, "7", true)
}
// checkTryID checks that consuming an identifier succeeds
// it is just a special case of checkConsume and should probably be refactored
func checkTryID(t *testing.T, p *Parser, want string, expectSuccess bool) {
got, err := p.consume(tIdentifier)
if err != nil && expectSuccess {
t.Error(err)
return
}
if got != want && expectSuccess {
t.Errorf("Bad token, want: %v, got: %v", want, got)
}
}
func TestTryID(t *testing.T) {
p, err := NewParser("wat 6 Peter Hegemon")
if err != nil {
t.Error(err)
return
}
checkTryID(t, p, "wat", true)
checkTryID(t, p, "endgame", false)
checkConsume(t, p, tNumberLiteral, "6")
checkTryID(t, p, "Peter", true)
checkTryID(t, p, "Achilles", false)
}
// checkLook checks that a lookahead matches our expected value
func checkLook(t *testing.T, p *Parser, offset uint64, tType string, want bool) {
got, err := p.lookahead(tType, offset)
if err != nil {
t.Error(err)
return
}
if got != want {
t.Errorf("Lookahead for %v failed, want: %v, got: %v", tType, want, got)
}
}
func TestLook(t *testing.T) {
p, err := NewParser("wat 6 Peter Hegemon")
if err != nil {
t.Error(err)
return
}
checkLook(t, p, 0, tIdentifier, true)
checkConsume(t, p, tIdentifier, "wat")
checkLook(t, p, 0, tComparisonOperator, false)
checkLook(t, p, 0, tNumberLiteral, true)
checkLook(t, p, 1, tIdentifier, true)
checkLook(t, p, 1, tNumberLiteral, false)
}
func checkExpression(t *testing.T, p *Parser, want string) {
got, err := p.expression()
if err != nil {
t.Error(err)
return
}
if got != want {
t.Errorf("Expression consume failed, want: %v, got: %v", want, got)
}
}
func TestExpressions(t *testing.T) {
p, err := NewParser(`hi.there hi?[5].there? hi.there.bob`)
if err != nil {
t.Error(err)
return
}
checkExpression(t, p, "hi.there")
checkExpression(t, p, "hi?[5].there?")
checkExpression(t, p, "hi.there.bob")
p, err = NewParser(`567 6.0 'lol' "wut"`)
if err != nil {
t.Error(err)
return
}
checkExpression(t, p, `567`)
checkExpression(t, p, `6.0`)
checkExpression(t, p, `'lol'`)
checkExpression(t, p, `"wut"`)
}
func TestRanges(t *testing.T) {
p, err := NewParser(`(5..7) (1.5..9.6) (young..old) (hi[5].wat..old)`)
if err != nil {
t.Error(err)
return
}
checkExpression(t, p, `(5..7)`)
checkExpression(t, p, `(1.5..9.6)`)
checkExpression(t, p, `(young..old)`)
checkExpression(t, p, `(hi[5].wat..old)`)
}
func checkArgument(t *testing.T, p *Parser, want string) {
got, err := p.argument()
if err != nil {
t.Error(err)
return
}
if got != want {
t.Errorf("Argument consume failed, want: %v, got: %v", want, got)
}
}
func TestArguments(t *testing.T) {
p, err := NewParser(`filter: hi.there[5], keyarg: 7`)
if err != nil {
t.Error(err)
return
}
checkConsume(t, p, tIdentifier, "filter")
checkConsume(t, p, tColon, ":")
checkArgument(t, p, "hi.there[5]")
checkConsume(t, p, tComma, ",")
checkArgument(t, p, "keyarg: 7")
}
func TestInvalidExpression(t *testing.T) {
p, err := NewParser(`==`)
if err != nil {
t.Error(err)
return
}
_, err = p.expression()
if err == nil {
t.Errorf("Expected error when parsing for expression")
}
}