forked from Knetic/govaluate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsingFailure_test.go
239 lines (198 loc) · 5.49 KB
/
parsingFailure_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package govaluate
import (
"fmt"
"regexp/syntax"
"strings"
"testing"
)
const (
UNEXPECTED_END string = "Unexpected end of expression"
INVALID_TOKEN_TRANSITION = "Cannot transition token types"
INVALID_TOKEN_KIND = "Invalid token"
UNCLOSED_QUOTES = "Unclosed string literal"
UNCLOSED_BRACKETS = "Unclosed parameter bracket"
UNBALANCED_PARENTHESIS = "Unbalanced parenthesis"
INVALID_NUMERIC = "Unable to parse numeric value"
UNDEFINED_FUNCTION = "Undefined function"
HANGING_ACCESSOR = "Hanging accessor on token"
UNEXPORTED_ACCESSOR = "Unable to access unexported"
INVALID_HEX = "Unable to parse hex value"
)
/*
Represents a test for parsing failures
*/
type ParsingFailureTest struct {
Name string
Input string
Expected string
}
func TestParsingFailure(test *testing.T) {
parsingTests := []ParsingFailureTest{
ParsingFailureTest{
Name: "Invalid equality comparator",
Input: "1 = 1",
Expected: INVALID_TOKEN_KIND,
},
ParsingFailureTest{
Name: "Invalid equality comparator",
Input: "1 === 1",
Expected: INVALID_TOKEN_KIND,
},
ParsingFailureTest{
Name: "Too many characters for logical operator",
Input: "true &&& false",
Expected: INVALID_TOKEN_KIND,
},
ParsingFailureTest{
Name: "Too many characters for logical operator",
Input: "true ||| false",
Expected: INVALID_TOKEN_KIND,
},
ParsingFailureTest{
Name: "Premature end to expression, via modifier",
Input: "10 > 5 +",
Expected: UNEXPECTED_END,
},
ParsingFailureTest{
Name: "Premature end to expression, via comparator",
Input: "10 + 5 >",
Expected: UNEXPECTED_END,
},
ParsingFailureTest{
Name: "Premature end to expression, via logical operator",
Input: "10 > 5 &&",
Expected: UNEXPECTED_END,
},
ParsingFailureTest{
Name: "Premature end to expression, via ternary operator",
Input: "true ?",
Expected: UNEXPECTED_END,
},
ParsingFailureTest{
Name: "Hanging REQ",
Input: "'wat' =~",
Expected: UNEXPECTED_END,
},
ParsingFailureTest{
Name: "Invalid operator change to REQ",
Input: " / =~",
Expected: INVALID_TOKEN_TRANSITION,
},
ParsingFailureTest{
Name: "Invalid starting token, comparator",
Input: "> 10",
Expected: INVALID_TOKEN_TRANSITION,
},
ParsingFailureTest{
Name: "Invalid starting token, modifier",
Input: "+ 5",
Expected: INVALID_TOKEN_TRANSITION,
},
ParsingFailureTest{
Name: "Invalid starting token, logical operator",
Input: "&& 5 < 10",
Expected: INVALID_TOKEN_TRANSITION,
},
ParsingFailureTest{
Name: "Invalid NUMERIC transition",
Input: "10 10",
Expected: INVALID_TOKEN_TRANSITION,
},
ParsingFailureTest{
Name: "Invalid STRING transition",
Input: "'foo' 'foo'",
Expected: INVALID_TOKEN_TRANSITION,
},
ParsingFailureTest{
Name: "Invalid operator transition",
Input: "10 > < 10",
Expected: INVALID_TOKEN_TRANSITION,
},
ParsingFailureTest{
Name: "Starting with unbalanced parens",
Input: " ) ( arg2",
Expected: INVALID_TOKEN_TRANSITION,
},
ParsingFailureTest{
Name: "Unclosed bracket",
Input: "[foo bar",
Expected: UNCLOSED_BRACKETS,
},
ParsingFailureTest{
Name: "Unclosed quote",
Input: "foo == 'responseTime",
Expected: UNCLOSED_QUOTES,
},
ParsingFailureTest{
Name: "Constant regex pattern fail to compile",
Input: "foo =~ '[abc'",
Expected: string(syntax.ErrMissingBracket),
},
ParsingFailureTest{
Name: "Unbalanced parenthesis",
Input: "10 > (1 + 50",
Expected: UNBALANCED_PARENTHESIS,
},
ParsingFailureTest{
Name: "Multiple radix",
Input: "127.0.0.1",
Expected: INVALID_NUMERIC,
},
ParsingFailureTest{
Name: "Undefined function",
Input: "foobar()",
Expected: UNDEFINED_FUNCTION,
},
ParsingFailureTest{
Name: "Hanging accessor",
Input: "foo.Bar.",
Expected: HANGING_ACCESSOR,
},
ParsingFailureTest{
// this is expected to change once there are structtags in place that allow aliasing of fields
Name: "Unexported parameter access",
Input: "foo.bar",
Expected: UNEXPORTED_ACCESSOR,
},
ParsingFailureTest{
Name: "Incomplete Hex",
Input: "0x",
Expected: INVALID_TOKEN_TRANSITION,
},
ParsingFailureTest{
Name: "Invalid Hex literal",
Input: "0x > 0",
Expected: INVALID_HEX,
},
ParsingFailureTest{
Name: "Hex float (Unsupported)",
Input: "0x1.1",
Expected: INVALID_TOKEN_TRANSITION,
},
ParsingFailureTest{
Name: "Hex invalid letter",
Input: "0x12g1",
Expected: INVALID_TOKEN_TRANSITION,
},
}
runParsingFailureTests(parsingTests, test)
}
func runParsingFailureTests(parsingTests []ParsingFailureTest, test *testing.T) {
var err error
fmt.Printf("Running %d parsing test cases...\n", len(parsingTests))
for _, testCase := range parsingTests {
_, err = NewEvaluableExpression(testCase.Input)
if err == nil {
test.Logf("Test '%s' failed", testCase.Name)
test.Logf("Expected a parsing error, found no error.")
test.Fail()
continue
}
if !strings.Contains(err.Error(), testCase.Expected) {
test.Logf("Test '%s' failed", testCase.Name)
test.Logf("Got error: '%s', expected '%s'", err.Error(), testCase.Expected)
test.Fail()
continue
}
}
}