1 package gval
2
3 import (
4 "regexp/syntax"
5 "testing"
6 )
7
8 func TestParsingFailure(t *testing.T) {
9 testEvaluate(
10 []evaluationTest{
11 {
12 name: "Invalid equality comparator",
13 expression: "1 = 1",
14 wantErr: unexpected(`"="`, "operator"),
15 },
16 {
17 name: "Invalid equality comparator",
18 expression: "1 === 1",
19 wantErr: unexpected(`"="`, "extension"),
20 },
21 {
22 name: "Too many characters for logical operator",
23 expression: "true &&& false",
24 wantErr: unexpected(`"&"`, "extension"),
25 },
26 {
27
28 name: "Too many characters for logical operator",
29 expression: "true ||| false",
30 wantErr: unexpected(`"|"`, "extension"),
31 },
32 {
33
34 name: "Premature end to expression, via modifier",
35 expression: "10 > 5 +",
36 wantErr: unexpected("EOF", "extensions"),
37 },
38 {
39 name: "Premature end to expression, via comparator",
40 expression: "10 + 5 >",
41 wantErr: unexpected("EOF", "extensions"),
42 },
43 {
44 name: "Premature end to expression, via logical operator",
45 expression: "10 > 5 &&",
46 wantErr: unexpected("EOF", "extensions"),
47 },
48 {
49
50 name: "Premature end to expression, via ternary operator",
51 expression: "true ?",
52 wantErr: unexpected("EOF", "extensions"),
53 },
54 {
55 name: "Hanging REQ",
56 expression: "`wat` =~",
57 wantErr: unexpected("EOF", "extensions"),
58 },
59 {
60
61 name: "Invalid operator change to REQ",
62 expression: " / =~",
63 wantErr: unexpected(`"/"`, "extensions"),
64 },
65 {
66 name: "Invalid starting token, comparator",
67 expression: "> 10",
68 wantErr: unexpected(`">"`, "extensions"),
69 },
70 {
71 name: "Invalid starting token, modifier",
72 expression: "+ 5",
73 wantErr: unexpected(`"+"`, "extensions"),
74 },
75 {
76 name: "Invalid starting token, logical operator",
77 expression: "&& 5 < 10",
78 wantErr: unexpected(`"&"`, "extensions"),
79 },
80 {
81 name: "Invalid NUMERIC transition",
82 expression: "10 10",
83 wantErr: unexpected(`Int`, "operator"),
84 },
85 {
86 name: "Invalid STRING transition",
87 expression: "`foo` `foo`",
88 wantErr: `String while scanning operator`,
89 },
90 {
91 name: "Invalid operator transition",
92 expression: "10 > < 10",
93 wantErr: unexpected(`"<"`, "extensions"),
94 },
95 {
96
97 name: "Starting with unbalanced parens",
98 expression: " ) ( arg2",
99 wantErr: unexpected(`")"`, "extensions"),
100 },
101 {
102
103 name: "Unclosed bracket",
104 expression: "[foo bar",
105 wantErr: unexpected(`EOF`, "extensions"),
106 },
107 {
108
109 name: "Unclosed quote",
110 expression: "foo == `responseTime",
111 wantErr: "could not parse string",
112 },
113 {
114
115 name: "Constant regex pattern fail to compile",
116 expression: "foo =~ `[abc`",
117 wantErr: string(syntax.ErrMissingBracket),
118 },
119 {
120
121 name: "Constant unmatch regex pattern fail to compile",
122 expression: "foo !~ `[abc`",
123 wantErr: string(syntax.ErrMissingBracket),
124 },
125 {
126
127 name: "Unbalanced parentheses",
128 expression: "10 > (1 + 50",
129 wantErr: unexpected(`EOF`, "parentheses"),
130 },
131 {
132
133 name: "Multiple radix",
134 expression: "127.0.0.1",
135 wantErr: unexpected(`Float`, "operator"),
136 },
137 {
138
139 name: "Hanging accessor",
140 expression: "foo.Bar.",
141 wantErr: unexpected(`EOF`, "field"),
142 },
143 {
144 name: "Incomplete Hex",
145 expression: "0x",
146 wantErr: `strconv.ParseFloat: parsing "0x": invalid syntax`,
147 },
148 {
149 name: "Invalid Hex literal",
150 expression: "0x > 0",
151 wantErr: `strconv.ParseFloat: parsing "0x": invalid syntax`,
152 },
153 {
154 name: "Hex float (Unsupported)",
155 expression: "0x1.1",
156 wantErr: `strconv.ParseFloat: parsing "0x1.1": invalid syntax`,
157 },
158 {
159 name: "Hex invalid letter",
160 expression: "0x12g1",
161 wantErr: `strconv.ParseFloat: parsing "0x12": invalid syntax`,
162 },
163 {
164 name: "Error after camouflage",
165 expression: "0 + ,",
166 wantErr: `unexpected "," while scanning extensions`,
167 },
168 },
169 t,
170 )
171 }
172
173 func unknownOp(op string) string {
174 return "unknown operator " + op
175 }
176
177 func unexpected(token, unit string) string {
178 return "unexpected " + token + " while scanning " + unit
179 }
180
View as plain text