Debian Codegraph @

selector/parser/parser_test.go

raw symbols SCIP
1 package parser_test
2
3 import (
4 "testing"
5
6 "github.com/google/go-cmp/cmp"
7 "github.com/tomwright/dasel/v3/selector/ast"
8 "github.com/tomwright/dasel/v3/selector/lexer"
9 "github.com/tomwright/dasel/v3/selector/parser"
10 )
11
12 type happyTestCase struct {
13 input string
14 expected ast.Expr
15 }
16
17 func (tc happyTestCase) run(t *testing.T) {
18 tokens, err := lexer.NewTokenizer(tc.input).Tokenize()
19 if err != nil {
20 t.Fatal(err)
21 }
22 got, err := parser.NewParser(tokens).Parse()
23 if err != nil {
24 t.Fatal(err)
25 }
26 if !cmp.Equal(tc.expected, got) {
27 t.Errorf("unexpected result: %s", cmp.Diff(tc.expected, got))
28 }
29 }
30
31 func TestParser_Parse_HappyPath(t *testing.T) {
32 t.Run("branching", func(t *testing.T) {
33 t.Run("two branches", happyTestCase{
34 input: `branch("hello", len("world"))`,
35 expected: ast.BranchExprs(
36 ast.StringExpr{Value: "hello"},
37 ast.ChainExprs(
38 ast.CallExpr{
39 Function: "len",
40 Args: ast.Expressions{ast.StringExpr{Value: "world"}},
41 },
42 ),
43 ),
44 }.run)
45 t.Run("three branches", happyTestCase{
46 input: `branch("foo", "bar", "baz")`,
47 expected: ast.BranchExprs(
48 ast.StringExpr{Value: "foo"},
49 ast.StringExpr{Value: "bar"},
50 ast.StringExpr{Value: "baz"},
51 ),
52 }.run)
53 })
54
55 t.Run("literal access", func(t *testing.T) {
56 t.Run("string", happyTestCase{
57 input: `"hello world"`,
58 expected: ast.StringExpr{Value: "hello world"},
59 }.run)
60 t.Run("int", happyTestCase{
61 input: "42",
62 expected: ast.NumberIntExpr{Value: 42},
63 }.run)
64 t.Run("leading zero decimal", happyTestCase{
65 input: "010",
66 expected: ast.NumberIntExpr{Value: 10},
67 }.run)
68 t.Run("float", happyTestCase{
69 input: "42.1",
70 expected: ast.NumberFloatExpr{Value: 42.1},
71 }.run)
72 t.Run("whole number float", happyTestCase{
73 input: "42f",
74 expected: ast.NumberFloatExpr{Value: 42},
75 }.run)
76 t.Run("bool true lowercase", happyTestCase{
77 input: "true",
78 expected: ast.BoolExpr{Value: true},
79 }.run)
80 t.Run("bool true uppercase", happyTestCase{
81 input: "TRUE",
82 expected: ast.BoolExpr{Value: true},
83 }.run)
84 t.Run("bool true mixed case", happyTestCase{
85 input: "TrUe",
86 expected: ast.BoolExpr{Value: true},
87 }.run)
88 t.Run("bool false lowercase", happyTestCase{
89 input: "false",
90 expected: ast.BoolExpr{Value: false},
91 }.run)
92 t.Run("bool false uppercase", happyTestCase{
93 input: "FALSE",
94 expected: ast.BoolExpr{Value: false},
95 }.run)
96 t.Run("bool false mixed case", happyTestCase{
97 input: "FaLsE",
98 expected: ast.BoolExpr{Value: false},
99 }.run)
100 })
101
102 t.Run("property access", func(t *testing.T) {
103 t.Run("single property access", happyTestCase{
104 input: "foo",
105 expected: ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
106 }.run)
107 t.Run("chained property access", happyTestCase{
108 input: "foo.bar",
109 expected: ast.ChainExprs(
110 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
111 ast.PropertyExpr{Property: ast.StringExpr{Value: "bar"}},
112 ),
113 }.run)
114 t.Run("property access spread", happyTestCase{
115 input: "foo...",
116 expected: ast.ChainExprs(
117 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
118 ast.SpreadExpr{},
119 ),
120 }.run)
121 t.Run("property access spread into property access", happyTestCase{
122 input: "foo....bar",
123 expected: ast.ChainExprs(
124 ast.ChainExprs(
125 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
126 ast.SpreadExpr{},
127 ),
128 ast.PropertyExpr{Property: ast.StringExpr{Value: "bar"}},
129 ),
130 }.run)
131 })
132
133 t.Run("array access", func(t *testing.T) {
134 t.Run("chained with filter", happyTestCase{
135 input: "filter(name == \"foo\")[0]",
136 expected: ast.ChainExprs(
137 ast.FilterExpr{
138 Expr: ast.BinaryExpr{
139 Left: ast.PropertyExpr{Property: ast.StringExpr{Value: "name"}},
140 Operator: lexer.Token{
141 Kind: lexer.Equal,
142 Value: "==",
143 Pos: 12,
144 Len: 2,
145 },
146 Right: ast.StringExpr{Value: "foo"},
147 },
148 },
149 ast.PropertyExpr{Property: ast.NumberIntExpr{Value: 0}},
150 ),
151 }.run)
152 t.Run("chained with map", happyTestCase{
153 input: "map(foo)[0]",
154 expected: ast.ChainExprs(
155 ast.MapExpr{
156 Expr: ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
157 },
158 ast.PropertyExpr{Property: ast.NumberIntExpr{Value: 0}},
159 ),
160 }.run)
161 t.Run("root array", func(t *testing.T) {
162 t.Run("index", happyTestCase{
163 input: "$this[1]",
164 expected: ast.ChainExprs(
165 ast.VariableExpr{Name: "this"},
166 ast.PropertyExpr{Property: ast.NumberIntExpr{Value: 1}},
167 ),
168 }.run)
169 t.Run("range", func(t *testing.T) {
170 t.Run("start and end funcs", happyTestCase{
171 input: "$this[calcStart(1):calcEnd()]",
172 expected: ast.ChainExprs(
173 ast.VariableExpr{Name: "this"},
174 ast.RangeExpr{
175 Start: ast.CallExpr{
176 Function: "calcStart",
177 Args: ast.Expressions{
178 ast.NumberIntExpr{Value: 1},
179 },
180 },
181 End: ast.CallExpr{
182 Function: "calcEnd",
183 },
184 },
185 ),
186 }.run)
187 t.Run("start and end", happyTestCase{
188 input: "$this[5:10]",
189 expected: ast.ChainExprs(
190 ast.VariableExpr{Name: "this"},
191 ast.RangeExpr{Start: ast.NumberIntExpr{Value: 5}, End: ast.NumberIntExpr{Value: 10}},
192 ),
193 }.run)
194 t.Run("start", happyTestCase{
195 input: "$this[5:]",
196 expected: ast.ChainExprs(
197 ast.VariableExpr{Name: "this"},
198 ast.RangeExpr{Start: ast.NumberIntExpr{Value: 5}},
199 ),
200 }.run)
201 t.Run("end", happyTestCase{
202 input: "$this[:10]",
203 expected: ast.ChainExprs(
204 ast.VariableExpr{Name: "this"},
205 ast.RangeExpr{End: ast.NumberIntExpr{Value: 10}},
206 ),
207 }.run)
208 })
209 t.Run("spread", func(t *testing.T) {
210 t.Run("standard", happyTestCase{
211 input: "$this...",
212 expected: ast.ChainExprs(
213 ast.VariableExpr{Name: "this"},
214 ast.SpreadExpr{},
215 ),
216 }.run)
217 t.Run("brackets", happyTestCase{
218 input: "$this[...]",
219 expected: ast.ChainExprs(
220 ast.VariableExpr{Name: "this"},
221 ast.SpreadExpr{},
222 ),
223 }.run)
224 })
225 })
226 t.Run("property array", func(t *testing.T) {
227 t.Run("index", happyTestCase{
228 input: "foo[1]",
229 expected: ast.ChainExprs(
230 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
231 ast.PropertyExpr{Property: ast.NumberIntExpr{Value: 1}},
232 ),
233 }.run)
234 t.Run("range", func(t *testing.T) {
235 t.Run("start and end funcs", happyTestCase{
236 input: "foo[calcStart(1):calcEnd()]",
237 expected: ast.ChainExprs(
238 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
239 ast.RangeExpr{
240 Start: ast.CallExpr{
241 Function: "calcStart",
242 Args: ast.Expressions{
243 ast.NumberIntExpr{Value: 1},
244 },
245 },
246 End: ast.CallExpr{
247 Function: "calcEnd",
248 },
249 },
250 ),
251 }.run)
252 t.Run("start and end", happyTestCase{
253 input: "foo[5:10]",
254 expected: ast.ChainExprs(
255 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
256 ast.RangeExpr{Start: ast.NumberIntExpr{Value: 5}, End: ast.NumberIntExpr{Value: 10}},
257 ),
258 }.run)
259 t.Run("start", happyTestCase{
260 input: "foo[5:]",
261 expected: ast.ChainExprs(
262 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
263 ast.RangeExpr{Start: ast.NumberIntExpr{Value: 5}},
264 ),
265 }.run)
266 t.Run("end", happyTestCase{
267 input: "foo[:10]",
268 expected: ast.ChainExprs(
269 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
270 ast.RangeExpr{End: ast.NumberIntExpr{Value: 10}},
271 ),
272 }.run)
273 })
274 t.Run("spread", func(t *testing.T) {
275 t.Run("standard", happyTestCase{
276 input: "foo...",
277 expected: ast.ChainExprs(
278 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
279 ast.SpreadExpr{},
280 ),
281 }.run)
282 t.Run("brackets", happyTestCase{
283 input: "foo[...]",
284 expected: ast.ChainExprs(
285 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
286 ast.SpreadExpr{},
287 ),
288 }.run)
289 })
290 })
291 })
292
293 t.Run("map", func(t *testing.T) {
294 t.Run("single property", happyTestCase{
295 input: "foo.map(x)",
296 expected: ast.ChainExprs(
297 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
298 ast.MapExpr{
299 Expr: ast.PropertyExpr{Property: ast.StringExpr{Value: "x"}},
300 },
301 ),
302 }.run)
303 t.Run("nested property", happyTestCase{
304 input: "foo.map(x.y)",
305 expected: ast.ChainExprs(
306 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
307 ast.MapExpr{
308 Expr: ast.ChainExprs(
309 ast.PropertyExpr{Property: ast.StringExpr{Value: "x"}},
310 ast.PropertyExpr{Property: ast.StringExpr{Value: "y"}},
311 ),
312 },
313 ),
314 }.run)
315 })
316
317 t.Run("object", func(t *testing.T) {
318 t.Run("get single property", happyTestCase{
319 input: "{foo}",
320 expected: ast.ObjectExpr{Pairs: []ast.KeyValue{
321 {Key: ast.StringExpr{Value: "foo"}, Value: ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}}},
322 }},
323 }.run)
324 t.Run("get multiple properties", happyTestCase{
325 input: "{foo, bar, baz}",
326 expected: ast.ObjectExpr{Pairs: []ast.KeyValue{
327 {Key: ast.StringExpr{Value: "foo"}, Value: ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}}},
328 {Key: ast.StringExpr{Value: "bar"}, Value: ast.PropertyExpr{Property: ast.StringExpr{Value: "bar"}}},
329 {Key: ast.StringExpr{Value: "baz"}, Value: ast.PropertyExpr{Property: ast.StringExpr{Value: "baz"}}},
330 }},
331 }.run)
332 t.Run("set single property", happyTestCase{
333 input: `{"foo":1}`,
334 expected: ast.ObjectExpr{Pairs: []ast.KeyValue{
335 {Key: ast.StringExpr{Value: "foo"}, Value: ast.NumberIntExpr{Value: 1}},
336 }},
337 }.run)
338 t.Run("set multiple properties", happyTestCase{
339 input: `{foo: 1, bar: 2, baz: 3}`,
340 expected: ast.ObjectExpr{Pairs: []ast.KeyValue{
341 {Key: ast.StringExpr{Value: "foo"}, Value: ast.NumberIntExpr{Value: 1}},
342 {Key: ast.StringExpr{Value: "bar"}, Value: ast.NumberIntExpr{Value: 2}},
343 {Key: ast.StringExpr{Value: "baz"}, Value: ast.NumberIntExpr{Value: 3}},
344 }},
345 }.run)
346 t.Run("combine get set", happyTestCase{
347 input: `{
348 ...,
349 nestedSpread...,
350 foo,
351 bar: 2,
352 "baz": evalSomething(),
353 "Name": "Tom",
354 }`,
355 expected: ast.ObjectExpr{Pairs: []ast.KeyValue{
356 {Key: ast.SpreadExpr{}, Value: nil},
357 {Key: ast.SpreadExpr{}, Value: ast.PropertyExpr{Property: ast.StringExpr{Value: "nestedSpread"}}},
358 {Key: ast.StringExpr{Value: "foo"}, Value: ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}}},
359 {Key: ast.StringExpr{Value: "bar"}, Value: ast.NumberIntExpr{Value: 2}},
360 {Key: ast.StringExpr{Value: "baz"}, Value: ast.CallExpr{Function: "evalSomething"}},
361 {Key: ast.StringExpr{Value: "Name"}, Value: ast.StringExpr{Value: "Tom"}},
362 }},
363 }.run)
364 })
365
366 t.Run("variables", func(t *testing.T) {
367 t.Run("single variable", happyTestCase{
368 input: `$foo`,
369 expected: ast.VariableExpr{Name: "foo"},
370 }.run)
371 t.Run("variable passed to func", happyTestCase{
372 input: `len($foo)`,
373 expected: ast.CallExpr{Function: "len", Args: ast.Expressions{ast.VariableExpr{Name: "foo"}}},
374 }.run)
375 })
376
377 t.Run("combinations and grouping", func(t *testing.T) {
378 t.Run("string concat with grouping", happyTestCase{
379 input: `(foo.a) + (foo.b)`,
380 expected: ast.BinaryExpr{
381 Left: ast.ChainExprs(ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}}, ast.PropertyExpr{Property: ast.StringExpr{Value: "a"}}),
382 Operator: lexer.Token{Kind: lexer.Plus, Value: "+", Pos: 8, Len: 1},
383 Right: ast.ChainExprs(ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}}, ast.PropertyExpr{Property: ast.StringExpr{Value: "b"}}),
384 },
385 }.run)
386 t.Run("string concat with nested properties", happyTestCase{
387 input: `foo.a + foo.b`,
388 expected: ast.BinaryExpr{
389 Left: ast.ChainExprs(ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}}, ast.PropertyExpr{Property: ast.StringExpr{Value: "a"}}),
390 Operator: lexer.Token{Kind: lexer.Plus, Value: "+", Pos: 6, Len: 1},
391 Right: ast.ChainExprs(ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}}, ast.PropertyExpr{Property: ast.StringExpr{Value: "b"}}),
392 },
393 }.run)
394 })
395
396 t.Run("conditional", func(t *testing.T) {
397 t.Run("if", happyTestCase{
398 input: `if (foo == 1) { "yes" } else { "no" }`,
399 expected: ast.ConditionalExpr{
400 Cond: ast.BinaryExpr{
401 Left: ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
402 Operator: lexer.Token{Kind: lexer.Equal, Value: "==", Pos: 8, Len: 2},
403 Right: ast.NumberIntExpr{Value: 1},
404 },
405 Then: ast.StringExpr{Value: "yes"},
406 Else: ast.StringExpr{Value: "no"},
407 },
408 }.run)
409 t.Run("if elseif else", happyTestCase{
410 input: `if (foo == 1) { "yes" } elseif (foo == 2) { "maybe" } else { "no" }`,
411 expected: ast.ConditionalExpr{
412 Cond: ast.BinaryExpr{
413 Left: ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
414 Operator: lexer.Token{Kind: lexer.Equal, Value: "==", Pos: 8, Len: 2},
415 Right: ast.NumberIntExpr{Value: 1},
416 },
417 Then: ast.StringExpr{Value: "yes"},
418 Else: ast.ConditionalExpr{
419 Cond: ast.BinaryExpr{
420 Left: ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
421 Operator: lexer.Token{Kind: lexer.Equal, Value: "==", Pos: 36, Len: 2},
422 Right: ast.NumberIntExpr{Value: 2},
423 },
424 Then: ast.StringExpr{Value: "maybe"},
425 Else: ast.StringExpr{Value: "no"},
426 },
427 },
428 }.run)
429 t.Run("if elseif elseif else", happyTestCase{
430 input: `if (foo == 1) { "yes" } elseif (foo == 2) { "maybe" } elseif (foo == 3) { "probably" } else { "no" }`,
431 expected: ast.ConditionalExpr{
432 Cond: ast.BinaryExpr{
433 Left: ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
434 Operator: lexer.Token{Kind: lexer.Equal, Value: "==", Pos: 8, Len: 2},
435 Right: ast.NumberIntExpr{Value: 1},
436 },
437 Then: ast.StringExpr{Value: "yes"},
438 Else: ast.ConditionalExpr{
439 Cond: ast.BinaryExpr{
440 Left: ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
441 Operator: lexer.Token{Kind: lexer.Equal, Value: "==", Pos: 36, Len: 2},
442 Right: ast.NumberIntExpr{Value: 2},
443 },
444 Then: ast.StringExpr{Value: "maybe"},
445 Else: ast.ConditionalExpr{
446 Cond: ast.BinaryExpr{
447 Left: ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
448 Operator: lexer.Token{Kind: lexer.Equal, Value: "==", Pos: 66, Len: 2},
449 Right: ast.NumberIntExpr{Value: 3},
450 },
451 Then: ast.StringExpr{Value: "probably"},
452 Else: ast.StringExpr{Value: "no"},
453 },
454 },
455 },
456 }.run)
457 })
458
459 t.Run("ternary", func(t *testing.T) {
460 t.Run("simple bool", happyTestCase{
461 input: `true ? "yes" : "no"`,
462 expected: ast.ConditionalExpr{
463 Cond: ast.BoolExpr{Value: true},
464 Then: ast.StringExpr{Value: "yes"},
465 Else: ast.StringExpr{Value: "no"},
466 },
467 }.run)
468 t.Run("with comparison condition", happyTestCase{
469 input: `foo == 1 ? "yes" : "no"`,
470 expected: ast.ConditionalExpr{
471 Cond: ast.BinaryExpr{
472 Left: ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
473 Operator: lexer.Token{Kind: lexer.Equal, Value: "==", Pos: 4, Len: 2},
474 Right: ast.NumberIntExpr{Value: 1},
475 },
476 Then: ast.StringExpr{Value: "yes"},
477 Else: ast.StringExpr{Value: "no"},
478 },
479 }.run)
480 t.Run("with logical condition", happyTestCase{
481 input: `a > 1 && b > 2 ? "yes" : "no"`,
482 expected: ast.ConditionalExpr{
483 Cond: ast.BinaryExpr{
484 Left: ast.BinaryExpr{
485 Left: ast.PropertyExpr{Property: ast.StringExpr{Value: "a"}},
486 Operator: lexer.Token{Kind: lexer.GreaterThan, Value: ">", Pos: 2, Len: 1},
487 Right: ast.NumberIntExpr{Value: 1},
488 },
489 Operator: lexer.Token{Kind: lexer.And, Value: "&&", Pos: 6, Len: 2},
490 Right: ast.BinaryExpr{
491 Left: ast.PropertyExpr{Property: ast.StringExpr{Value: "b"}},
492 Operator: lexer.Token{Kind: lexer.GreaterThan, Value: ">", Pos: 11, Len: 1},
493 Right: ast.NumberIntExpr{Value: 2},
494 },
495 },
496 Then: ast.StringExpr{Value: "yes"},
497 Else: ast.StringExpr{Value: "no"},
498 },
499 }.run)
500 t.Run("property expressions", happyTestCase{
501 input: `active ? name : "unknown"`,
502 expected: ast.ConditionalExpr{
503 Cond: ast.PropertyExpr{Property: ast.StringExpr{Value: "active"}},
504 Then: ast.PropertyExpr{Property: ast.StringExpr{Value: "name"}},
505 Else: ast.StringExpr{Value: "unknown"},
506 },
507 }.run)
508 t.Run("nested in parentheses", happyTestCase{
509 input: `true ? (false ? "a" : "b") : "c"`,
510 expected: ast.ConditionalExpr{
511 Cond: ast.BoolExpr{Value: true},
512 Then: ast.ConditionalExpr{
513 Cond: ast.BoolExpr{Value: false},
514 Then: ast.StringExpr{Value: "a"},
515 Else: ast.StringExpr{Value: "b"},
516 },
517 Else: ast.StringExpr{Value: "c"},
518 },
519 }.run)
520 t.Run("number expressions", happyTestCase{
521 input: `true ? 1 : 2`,
522 expected: ast.ConditionalExpr{
523 Cond: ast.BoolExpr{Value: true},
524 Then: ast.NumberIntExpr{Value: 1},
525 Else: ast.NumberIntExpr{Value: 2},
526 },
527 }.run)
528 t.Run("arithmetic in branches", happyTestCase{
529 input: `true ? 1 + 2 : 3 + 4`,
530 expected: ast.ConditionalExpr{
531 Cond: ast.BoolExpr{Value: true},
532 Then: ast.BinaryExpr{
533 Left: ast.NumberIntExpr{Value: 1},
534 Operator: lexer.Token{Kind: lexer.Plus, Value: "+", Pos: 9, Len: 1},
535 Right: ast.NumberIntExpr{Value: 2},
536 },
537 Else: ast.BinaryExpr{
538 Left: ast.NumberIntExpr{Value: 3},
539 Operator: lexer.Token{Kind: lexer.Plus, Value: "+", Pos: 17, Len: 1},
540 Right: ast.NumberIntExpr{Value: 4},
541 },
542 },
543 }.run)
544 t.Run("func call in branches", happyTestCase{
545 input: `true ? len("abc") : len("de")`,
546 expected: ast.ConditionalExpr{
547 Cond: ast.BoolExpr{Value: true},
548 Then: ast.CallExpr{Function: "len", Args: ast.Expressions{ast.StringExpr{Value: "abc"}}},
549 Else: ast.CallExpr{Function: "len", Args: ast.Expressions{ast.StringExpr{Value: "de"}}},
550 },
551 }.run)
552 t.Run("chained property in condition", happyTestCase{
553 input: `foo.bar == "x" ? "yes" : "no"`,
554 expected: ast.ConditionalExpr{
555 Cond: ast.BinaryExpr{
556 Left: ast.ChainExprs(
557 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
558 ast.PropertyExpr{Property: ast.StringExpr{Value: "bar"}},
559 ),
560 Operator: lexer.Token{Kind: lexer.Equal, Value: "==", Pos: 8, Len: 2},
561 Right: ast.StringExpr{Value: "x"},
562 },
563 Then: ast.StringExpr{Value: "yes"},
564 Else: ast.StringExpr{Value: "no"},
565 },
566 }.run)
567 t.Run("variable in condition", happyTestCase{
568 input: `$x > 5 ? "big" : "small"`,
569 expected: ast.ConditionalExpr{
570 Cond: ast.BinaryExpr{
571 Left: ast.VariableExpr{Name: "x"},
572 Operator: lexer.Token{Kind: lexer.GreaterThan, Value: ">", Pos: 3, Len: 1},
573 Right: ast.NumberIntExpr{Value: 5},
574 },
575 Then: ast.StringExpr{Value: "big"},
576 Else: ast.StringExpr{Value: "small"},
577 },
578 }.run)
579 t.Run("null in else branch", happyTestCase{
580 input: `true ? "yes" : null`,
581 expected: ast.ConditionalExpr{
582 Cond: ast.BoolExpr{Value: true},
583 Then: ast.StringExpr{Value: "yes"},
584 Else: ast.NullExpr{},
585 },
586 }.run)
587 t.Run("group expression in condition", happyTestCase{
588 input: `(1 + 2) == 3 ? "yes" : "no"`,
589 expected: ast.ConditionalExpr{
590 Cond: ast.BinaryExpr{
591 Left: ast.BinaryExpr{
592 Left: ast.NumberIntExpr{Value: 1},
593 Operator: lexer.Token{Kind: lexer.Plus, Value: "+", Pos: 3, Len: 1},
594 Right: ast.NumberIntExpr{Value: 2},
595 },
596 Operator: lexer.Token{Kind: lexer.Equal, Value: "==", Pos: 8, Len: 2},
597 Right: ast.NumberIntExpr{Value: 3},
598 },
599 Then: ast.StringExpr{Value: "yes"},
600 Else: ast.StringExpr{Value: "no"},
601 },
602 }.run)
603 })
604
605 t.Run("ternary errors", func(t *testing.T) {
606 errorCases := []struct {
607 name string
608 input string
609 }{
610 {"missing colon and else", `true ? "yes"`},
611 {"missing else expression", `true ? "yes" :`},
612 {"missing then and else", `true ?`},
613 {"leading question mark", `? "yes" : "no"`},
614 }
615 for _, tc := range errorCases {
616 t.Run(tc.name, func(t *testing.T) {
617 tokens, err := lexer.NewTokenizer(tc.input).Tokenize()
618 if err != nil {
619 t.Fatal(err)
620 }
621 _, err = parser.NewParser(tokens).Parse()
622 if err == nil {
623 t.Errorf("expected error for input %q, got nil", tc.input)
624 }
625 })
626 }
627 })
628
629 t.Run("coalesce", func(t *testing.T) {
630 t.Run("chained on left side", happyTestCase{
631 input: `foo ?? bar ?? baz`,
632 expected: ast.BinaryExpr{
633 Left: ast.BinaryExpr{
634 Left: ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
635 Operator: lexer.Token{Kind: lexer.DoubleQuestionMark, Value: "??", Pos: 4, Len: 2},
636 Right: ast.PropertyExpr{Property: ast.StringExpr{Value: "bar"}},
637 },
638 Operator: lexer.Token{Kind: lexer.DoubleQuestionMark, Value: "??", Pos: 11, Len: 2},
639 Right: ast.PropertyExpr{Property: ast.StringExpr{Value: "baz"}},
640 },
641 }.run)
642
643 t.Run("chained nested on left side", happyTestCase{
644 input: `nested.one ?? nested.two ?? nested.three ?? 10`,
645 expected: ast.BinaryExpr{
646 Left: ast.BinaryExpr{
647 Left: ast.BinaryExpr{
648 Left: ast.ChainExprs(
649 ast.PropertyExpr{Property: ast.StringExpr{Value: "nested"}},
650 ast.PropertyExpr{Property: ast.StringExpr{Value: "one"}},
651 ),
652 Operator: lexer.Token{Kind: lexer.DoubleQuestionMark, Value: "??", Pos: 11, Len: 2},
653 Right: ast.ChainExprs(
654 ast.PropertyExpr{Property: ast.StringExpr{Value: "nested"}},
655 ast.PropertyExpr{Property: ast.StringExpr{Value: "two"}},
656 ),
657 },
658 Operator: lexer.Token{Kind: lexer.DoubleQuestionMark, Value: "??", Pos: 25, Len: 2},
659 Right: ast.ChainExprs(
660 ast.PropertyExpr{Property: ast.StringExpr{Value: "nested"}},
661 ast.PropertyExpr{Property: ast.StringExpr{Value: "three"}},
662 ),
663 },
664 Operator: lexer.Token{Kind: lexer.DoubleQuestionMark, Value: "??", Pos: 41, Len: 2},
665 Right: ast.NumberIntExpr{Value: 10},
666 },
667 }.run)
668 })
669
670 t.Run("any", func(t *testing.T) {
671 t.Run("simple predicate", happyTestCase{
672 input: "foo.any($this > 2)",
673 expected: ast.ChainExprs(
674 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
675 ast.AnyExpr{
676 Expr: ast.BinaryExpr{
677 Left: ast.VariableExpr{Name: "this"},
678 Operator: lexer.Token{
679 Kind: lexer.GreaterThan,
680 Value: ">",
681 Pos: 14,
682 Len: 1,
683 },
684 Right: ast.NumberIntExpr{Value: 2},
685 },
686 },
687 ),
688 }.run)
689 })
690
691 t.Run("all", func(t *testing.T) {
692 t.Run("simple predicate", happyTestCase{
693 input: "foo.all($this > 0)",
694 expected: ast.ChainExprs(
695 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
696 ast.AllExpr{
697 Expr: ast.BinaryExpr{
698 Left: ast.VariableExpr{Name: "this"},
699 Operator: lexer.Token{
700 Kind: lexer.GreaterThan,
701 Value: ">",
702 Pos: 14,
703 Len: 1,
704 },
705 Right: ast.NumberIntExpr{Value: 0},
706 },
707 },
708 ),
709 }.run)
710 })
711
712 t.Run("count", func(t *testing.T) {
713 t.Run("simple predicate", happyTestCase{
714 input: "foo.count($this == 1)",
715 expected: ast.ChainExprs(
716 ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
717 ast.CountExpr{
718 Expr: ast.BinaryExpr{
719 Left: ast.VariableExpr{Name: "this"},
720 Operator: lexer.Token{
721 Kind: lexer.Equal,
722 Value: "==",
723 Pos: 16,
724 Len: 2,
725 },
726 Right: ast.NumberIntExpr{Value: 1},
727 },
728 },
729 ),
730 }.run)
731 })
732
733 t.Run("groupBy", func(t *testing.T) {
734 t.Run("simple property", happyTestCase{
735 input: "groupBy(name)",
736 expected: ast.GroupByExpr{
737 Expr: ast.PropertyExpr{Property: ast.StringExpr{Value: "name"}},
738 },
739 }.run)
740 })
741
742 t.Run("reduce", func(t *testing.T) {
743 // reduce($this, 0, $acc + $this)
744 t.Run("sum", happyTestCase{
745 input: "reduce($this, 0, $acc + $this)",
746 expected: ast.ReduceExpr{
747 Expr: ast.VariableExpr{Name: "this"},
748 Init: ast.NumberIntExpr{Value: 0},
749 Update: ast.BinaryExpr{
750 Left: ast.VariableExpr{Name: "acc"},
751 Operator: lexer.Token{Kind: lexer.Plus, Value: "+", Pos: 22, Len: 1},
752 Right: ast.VariableExpr{Name: "this"},
753 },
754 },
755 }.run)
756 })
757
758 t.Run("mapValues", func(t *testing.T) {
759 t.Run("multiply", happyTestCase{
760 input: "mapValues($this * 2)",
761 expected: ast.MapValuesExpr{
762 Expr: ast.BinaryExpr{
763 Left: ast.VariableExpr{Name: "this"},
764 Operator: lexer.Token{Kind: lexer.Star, Value: "*", Pos: 16, Len: 1},
765 Right: ast.NumberIntExpr{Value: 2},
766 },
767 },
768 }.run)
769 })
770
771 t.Run("sortBy", func(t *testing.T) {
772 t.Run("ascending", happyTestCase{
773 input: "sortBy(age)",
774 expected: ast.SortByExpr{
775 Expr: ast.PropertyExpr{Property: ast.StringExpr{Value: "age"}},
776 Descending: false,
777 },
778 }.run)
779 t.Run("descending", happyTestCase{
780 input: "sortBy(age, desc)",
781 expected: ast.SortByExpr{
782 Expr: ast.PropertyExpr{Property: ast.StringExpr{Value: "age"}},
783 Descending: true,
784 },
785 }.run)
786 })
787
788 t.Run("each", func(t *testing.T) {
789 t.Run("add one", happyTestCase{
790 input: "each($this + 1)",
791 expected: ast.EachExpr{
792 Expr: ast.BinaryExpr{
793 Left: ast.VariableExpr{Name: "this"},
794 Operator: lexer.Token{Kind: lexer.Plus, Value: "+", Pos: 11, Len: 1},
795 Right: ast.NumberIntExpr{Value: 1},
796 },
797 },
798 }.run)
799 })
800
801 t.Run("array literal", func(t *testing.T) {
802 t.Run("simple", happyTestCase{
803 input: "[1, 2, 3]",
804 expected: ast.ArrayExpr{
805 Exprs: ast.Expressions{
806 ast.NumberIntExpr{Value: 1},
807 ast.NumberIntExpr{Value: 2},
808 ast.NumberIntExpr{Value: 3},
809 },
810 },
811 }.run)
812 })
813
814 t.Run("search", func(t *testing.T) {
815 t.Run("with has", happyTestCase{
816 input: `search(has("x"))`,
817 expected: ast.SearchExpr{
818 Expr: ast.CallExpr{
819 Function: "has",
820 Args: ast.Expressions{ast.StringExpr{Value: "x"}},
821 },
822 },
823 }.run)
824 })
825
826 t.Run("recursive descent", func(t *testing.T) {
827 t.Run("property", happyTestCase{
828 input: "..name",
829 expected: ast.RecursiveDescentExpr{
830 IsWildcard: false,
831 Expr: ast.PropertyExpr{Property: ast.StringExpr{Value: "name"}},
832 },
833 }.run)
834 t.Run("wildcard", happyTestCase{
835 input: "..*",
836 expected: ast.RecursiveDescentExpr{
837 IsWildcard: true,
838 },
839 }.run)
840 })
841
842 t.Run("regex in binary expr", func(t *testing.T) {
843 t.Run("like operator", func(t *testing.T) {
844 input := `name =~ r/foo.*/`
845 tokens, err := lexer.NewTokenizer(input).Tokenize()
846 if err != nil {
847 t.Fatal(err)
848 }
849 got, err := parser.NewParser(tokens).Parse()
850 if err != nil {
851 t.Fatal(err)
852 }
853 binExpr, ok := got.(ast.BinaryExpr)
854 if !ok {
855 t.Fatalf("expected BinaryExpr, got %T", got)
856 }
857 if binExpr.Operator.Kind != lexer.Like {
858 t.Errorf("expected Like operator, got %v", binExpr.Operator.Kind)
859 }
860 regexExpr, ok := binExpr.Right.(ast.RegexExpr)
861 if !ok {
862 t.Fatalf("expected RegexExpr on right, got %T", binExpr.Right)
863 }
864 if regexExpr.Regex.String() != `foo.*` {
865 t.Errorf("expected regex 'foo.*', got %s", regexExpr.Regex.String())
866 }
867 })
868 })
869 }