Skip to content

Commit 236113f

Browse files
committed
chore: clean up tests
1 parent c8a3ec1 commit 236113f

File tree

1 file changed

+77
-64
lines changed

1 file changed

+77
-64
lines changed

pkg/jsonpath/token_test.go

Lines changed: 77 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,14 @@ func TestTokenizer(t *testing.T) {
293293

294294
func TestTokenizer_NoPanic(t *testing.T) {
295295
testCases := []struct {
296-
name string
297-
path string
296+
name string
297+
path string
298+
illegal bool
298299
}{
299300
{name: "identity", path: ""},
300301
{name: "root", path: "$"},
301-
{name: "unmatched closing parenthesis", path: ")"},
302-
{name: "unmatched closing square bracket", path: "]"},
302+
{name: "unmatched closing parenthesis", path: ")", illegal: true},
303+
{name: "unmatched closing square bracket", path: "]", illegal: true},
303304
{name: "dot child", path: "$.child"},
304305
{name: "dot child with implicit root", path: ".child"},
305306
{name: "undotted child with implicit root", path: "child"},
@@ -316,7 +317,7 @@ func TestTokenizer_NoPanic(t *testing.T) {
316317
{name: "dot child with embedded space", path: "$.child more"},
317318
{name: "bracket child", path: "$['child']"},
318319
{name: "bracket child with double quotes", path: `$["child"]`},
319-
{name: "bracket child with unmatched quotes", path: `$["child']`},
320+
{name: "bracket child with unmatched quotes", path: `$["child']`, illegal: true},
320321
{name: "bracket child with empty name", path: "$['']"},
321322
{name: "bracket child of bracket child", path: "$['child1']['child2']"},
322323
{name: "double quoted bracket child of bracket child", path: `$['child1']["child2"]`},
@@ -332,42 +333,42 @@ func TestTokenizer_NoPanic(t *testing.T) {
332333
{name: "bracket child followed by space", path: "$['child'] "},
333334
{name: "bracket dotted child", path: "$['child1.child2']"},
334335
{name: "bracket child with array subscript", path: "$['child'][*]"},
335-
{name: "property name dot child", path: "$.child~"},
336-
{name: "property name dot child with implicit root", path: ".child~"},
337-
{name: "property name undotted child with implicit root", path: "child~"},
338-
{name: "property name dot child with no name", path: "$.~"},
339-
{name: "property name dot child with missing dot", path: "$a~"},
340-
{name: "property name dot child with trailing chars", path: "$.child~.test"},
341-
{name: "property name undotted child with trailing chars", path: "child~.test"},
342-
{name: "property name dot child with trailing dot", path: "$.child.~"},
343-
{name: "property name dot child of dot child", path: "$.child1.child2~"},
344-
{name: "property name dot child with wildcard array subscript", path: "$.child[*]~"},
345-
{name: "property name dot child with an array subscript", path: "$.child[0]~"},
346-
{name: "property name dot child with array subscript with zero step", path: "$.child[1:2:0]~"},
347-
{name: "property name dot child with non-integer array subscript", path: "$.child[1:2:a]~"},
348-
{name: "property name dot child with unclosed array subscript", path: "$.child[*~"},
349-
{name: "property name dot child with missing array subscript", path: "$.child[]~"},
350-
{name: "property name dot child with embedded space", path: "$.child more~"},
351-
{name: "property name bracket child", path: "$['child']~"},
352-
{name: "property name bracket child with double quotes", path: `$["child"]~`},
353-
{name: "property name bracket child with unmatched quotes", path: `$["child']~`},
354-
{name: "property name bracket child with empty name", path: "$['']~"},
355-
{name: "property name bracket child of bracket child", path: "$['child1']['child2']~"},
356-
{name: "property name double quoted bracket child of bracket child", path: `$['child1']["child2"]~`},
357-
{name: "property name bracket child union", path: "$['child','child2']~"},
358-
{name: "property name bracket child union with whitespace", path: "$[ 'child' , 'child2' ]~"},
359-
{name: "property name bracket child union with mixed quotes", path: `$[ 'child' , "child2" ]~`},
360-
{name: "property name bracket child quoted union literal", path: "$[',']~"},
361-
{name: "property name bracket child with wildcard array subscript", path: "$['child'][*]~"},
362-
{name: "property name bracket child with wildcard array subscript and trailing chars", path: "$['child'][*]~.child"},
363-
{name: "property name bracket child with ~ in name", path: "$['child~']~"},
364-
{name: "bracket child with array subscript", path: "$['child'][1]~"},
365-
{name: "property name bracket child with non-integer array subscript", path: "$['child'][1:2:a]~"},
366-
{name: "property name bracket child with unclosed array subscript", path: "$['child'][*~"},
367-
{name: "property name bracket child with missing array subscript", path: "$['child'][]~"},
368-
{name: "property name bracket child separated a by space", path: "$['child'] ~"},
369-
{name: "property name bracket child followed by space", path: "$['child']~ "},
370-
{name: "property name bracket dotted child", path: "$['child1.child2']~"},
336+
{name: "property name dot child", path: "$.child~", illegal: true},
337+
{name: "property name dot child with implicit root", path: ".child~", illegal: true},
338+
{name: "property name undotted child with implicit root", path: "child~", illegal: true},
339+
{name: "property name dot child with no name", path: "$.~", illegal: true},
340+
{name: "property name dot child with missing dot", path: "$a~", illegal: true},
341+
{name: "property name dot child with trailing chars", path: "$.child~.test", illegal: true},
342+
{name: "property name undotted child with trailing chars", path: "child~.test", illegal: true},
343+
{name: "property name dot child with trailing dot", path: "$.child.~", illegal: true},
344+
{name: "property name dot child of dot child", path: "$.child1.child2~", illegal: true},
345+
{name: "property name dot child with wildcard array subscript", path: "$.child[*]~", illegal: true},
346+
{name: "property name dot child with an array subscript", path: "$.child[0]~", illegal: true},
347+
{name: "property name dot child with array subscript with zero step", path: "$.child[1:2:0]~", illegal: true},
348+
{name: "property name dot child with non-integer array subscript", path: "$.child[1:2:a]~", illegal: true},
349+
{name: "property name dot child with unclosed array subscript", path: "$.child[*~", illegal: true},
350+
{name: "property name dot child with missing array subscript", path: "$.child[]~", illegal: true},
351+
{name: "property name dot child with embedded space", path: "$.child more~", illegal: true},
352+
{name: "property name bracket child", path: "$['child']~", illegal: true},
353+
{name: "property name bracket child with double quotes", path: `$["child"]~`, illegal: true},
354+
{name: "property name bracket child with unmatched quotes", path: `$["child']~`, illegal: true},
355+
{name: "property name bracket child with empty name", path: "$['']~", illegal: true},
356+
{name: "property name bracket child of bracket child", path: "$['child1']['child2']~", illegal: true},
357+
{name: "property name double quoted bracket child of bracket child", path: `$['child1']["child2"]~`, illegal: true},
358+
{name: "property name bracket child union", path: "$['child','child2']~", illegal: true},
359+
{name: "property name bracket child union with whitespace", path: "$[ 'child' , 'child2' ]~", illegal: true},
360+
{name: "property name bracket child union with mixed quotes", path: `$[ 'child' , "child2" ]~`, illegal: true},
361+
{name: "property name bracket child quoted union literal", path: "$[',']~", illegal: true},
362+
{name: "property name bracket child with wildcard array subscript", path: "$['child'][*]~", illegal: true},
363+
{name: "property name bracket child with wildcard array subscript and trailing chars", path: "$['child'][*]~.child", illegal: true},
364+
{name: "property name bracket child with ~ in name", path: "$['child~']~", illegal: true},
365+
{name: "bracket child with array subscript", path: "$['child'][1]~", illegal: true},
366+
{name: "property name bracket child with non-integer array subscript", path: "$['child'][1:2:a]~", illegal: true},
367+
{name: "property name bracket child with unclosed array subscript", path: "$['child'][*~", illegal: true},
368+
{name: "property name bracket child with missing array subscript", path: "$['child'][]~", illegal: true},
369+
{name: "property name bracket child separated a by space", path: "$['child'] ~", illegal: true},
370+
{name: "property name bracket child followed by space", path: "$['child']~ ", illegal: true},
371+
{name: "property name bracket dotted child", path: "$['child1.child2']~", illegal: true},
371372
{name: "array union", path: "$[0,1]"},
372373
{name: "array union with whitespace", path: "$[ 0 , 1 ]"},
373374
{name: "bracket child with malformed array subscript", path: "$['child'][1:2:3:4]"},
@@ -397,8 +398,8 @@ func TestTokenizer_NoPanic(t *testing.T) {
397398
{name: "missing filter ", path: "$[?()]"},
398399
{name: "unclosed filter", path: "$[?("},
399400
{name: "filter with missing operator", path: "$[?(@.child @.other)]"},
400-
{name: "filter with malformed term", path: "$[?([)]"},
401-
{name: "filter with misplaced open bracket", path: "$[?(@.child ()]"},
401+
{name: "filter with malformed term", path: "$[?([)]", illegal: true},
402+
{name: "filter with misplaced open bracket", path: "$[?(@.child ()]", illegal: true},
402403
{name: "simple negative filter", path: "$[?([email protected])]"},
403404
{name: "misplaced filter negation", path: "$[?(@.child [email protected])]"},
404405
{name: "simple negative filter with extra whitespace", path: "$[?( ! @.child)]"},
@@ -408,14 +409,14 @@ func TestTokenizer_NoPanic(t *testing.T) {
408409
{name: "filter string equality with apparent boolean", path: `$[?(@.child=="true")]`},
409410
{name: "filter string equality with apparent null", path: `$[?(@.child=="null")]`},
410411
{name: "filter string equality, double-quoted literal on the right", path: `$[?(@.child=="x")]`},
411-
{name: "filter integer equality with invalid literal", path: "$[?(@.child==-)]"},
412+
{name: "filter integer equality with invalid literal", path: "$[?(@.child==-)]", illegal: true},
412413
{name: "filter integer equality with integer literal which is too large", path: "$[?(@.child==9223372036854775808)]"},
413414
{name: "filter integer equality with invalid float literal", path: "$[?(@.child==1.2.3)]"},
414-
{name: "filter integer equality with invalid string literal", path: "$[?(@.child=='x)]"},
415+
{name: "filter integer equality with invalid string literal", path: "$[?(@.child=='x)]", illegal: true},
415416
{name: "filter integer equality, literal on the left", path: "$[?([email protected])]"},
416417
{name: "filter float equality, literal on the left", path: "$[?([email protected])]"},
417-
{name: "filter fractional float equality, literal on the left", path: "$[?([email protected])]"},
418-
{name: "filter fractional float equality, literal on the right", path: "$[?(@.child== -1.5e-1 )]"},
418+
{name: "filter fractional float equality, literal on the left", path: "$[?([email protected])]", illegal: true},
419+
{name: "filter fractional float equality, literal on the right", path: "$[?(@.child== -1.5e-1 )]", illegal: true},
419420
{name: "filter boolean true equality, literal on the right", path: "$[?(@.child== true )]"},
420421
{name: "filter boolean false equality, literal on the right", path: "$[?(@.child==false)]"},
421422
{name: "filter boolean true equality, literal on the left", path: "$[?([email protected])]"},
@@ -429,8 +430,8 @@ func TestTokenizer_NoPanic(t *testing.T) {
429430
{name: "filter integer equality, root path on the left", path: "$[?([email protected])]"},
430431
{name: "filter string equality, literal on the right", path: "$[?(@.child=='x')]"},
431432
{name: "filter string equality, literal on the left", path: "$[?('x'[email protected])]"},
432-
{name: "filter string equality, literal on the left with unmatched string delimiter", path: "$[?('[email protected])]"},
433-
{name: "filter string equality with unmatched string delimiter", path: "$[?(@.child=='x)]"},
433+
{name: "filter string equality, literal on the left with unmatched string delimiter", path: "$[?('[email protected])]", illegal: true},
434+
{name: "filter string equality with unmatched string delimiter", path: "$[?(@.child=='x)]", illegal: true},
434435
{name: "filter integer inequality, literal on the right", path: "$[?(@.child!=1)]"},
435436
{name: "filter inequality with missing left hand operator", path: "$[?(!=1)]"},
436437
{name: "filter equality with missing right hand value", path: "$[?(@.child!=)]"},
@@ -460,7 +461,7 @@ func TestTokenizer_NoPanic(t *testing.T) {
460461
{name: "filter less than or equal, string on the right", path: "$[?(@.child<='x')]"},
461462
{name: "filter less than or equal, string on the left", path: "$[?('x'<[email protected])]"},
462463
{name: "filter conjunction", path: "$[?(@.child&&@.other)]"},
463-
{name: "filter conjunction with literals and whitespace", path: "$[?(@.child == 'x' && -9 == @.other)]"},
464+
{name: "filter conjunction with literals and whitespace", path: "$[?(@.child == 'x' && -9 == @.other)]", illegal: true},
464465
{name: "filter conjunction with bracket children", path: "$[?(@['child'][*]&&@['other'])]"},
465466
{name: "filter invalid leading conjunction", path: "$[?(&&"},
466467
{name: "filter conjunction with extra whitespace", path: "$[?(@.child && @.other)]"},
@@ -473,22 +474,22 @@ func TestTokenizer_NoPanic(t *testing.T) {
473474
{name: "filter negation", path: "$[?([email protected])]"},
474475
{name: "filter negation of comparison (edge case)", path: "$[?([email protected]>1)]"},
475476
{name: "filter negation of bracket", path: "$[?(!(@.child))]"},
476-
{name: "filter regular expression", path: "$[?(@.child=~/.*/)]"},
477-
{name: "filter regular expression with escaped /", path: `$[?(@.child=~/\/.*/)]`},
478-
{name: "filter regular expression with escaped \\", path: `$[?(@.child=~/\\/)]`},
479-
{name: "filter regular expression with missing leading /", path: `$[?(@.child=~.*/)]`},
480-
{name: "filter regular expression with missing trailing /", path: `$[?(@.child=~/.*)]`},
481-
{name: "filter regular expression to match string literal", path: `$[?('x'=~/.*/)]`},
482-
{name: "filter regular expression to match integer literal", path: `$[?(0=~/.*/)]`},
483-
{name: "filter regular expression to match float literal", path: `$[?(.1=~/.*/)]`},
484-
{name: "filter invalid regular expression", path: `$[?(@.child=~/(.*/)]`},
485-
{name: "unescaped single quote in bracket child name", path: `$['single'quote']`},
477+
{name: "filter regular expression", path: "$[?(@.child=~/.*/)]", illegal: true},
478+
{name: "filter regular expression with escaped /", path: `$[?(@.child=~/\/.*/)]`, illegal: true},
479+
{name: "filter regular expression with escaped \\", path: `$[?(@.child=~/\\/)]`, illegal: true},
480+
{name: "filter regular expression with missing leading /", path: `$[?(@.child=~.*/)]`, illegal: true},
481+
{name: "filter regular expression with missing trailing /", path: `$[?(@.child=~/.*)]`, illegal: true},
482+
{name: "filter regular expression to match string literal", path: `$[?('x'=~/.*/)]`, illegal: true},
483+
{name: "filter regular expression to match integer literal", path: `$[?(0=~/.*/)]`, illegal: true},
484+
{name: "filter regular expression to match float literal", path: `$[?(.1=~/.*/)]`, illegal: true},
485+
{name: "filter invalid regular expression", path: `$[?(@.child=~/(.*/)]`, illegal: true},
486+
{name: "unescaped single quote in bracket child name", path: `$['single'quote']`, illegal: true},
486487
{name: "escaped single quote in bracket child name", path: `$['single\']quote']`},
487488
{name: "escaped backslash in bracket child name", path: `$['\\']`},
488-
{name: "unescaped single quote after escaped backslash in bracket child name", path: `$['single\\'quote']`},
489+
{name: "unescaped single quote after escaped backslash in bracket child name", path: `$['single\\'quote']`, illegal: true},
489490
{name: "unsupported escape sequence in bracket child name", path: `$['\n']`},
490-
{name: "unclosed and empty bracket child name with space", path: `$[ '`},
491-
{name: "unclosed and empty bracket child name with formfeed", path: "[\f'"},
491+
{name: "unclosed and empty bracket child name with space", path: `$[ '`, illegal: true},
492+
{name: "unclosed and empty bracket child name with formfeed", path: "[\f'", illegal: true},
492493
{name: "filter involving value of current node on left hand side", path: "$[?(@==1)]"},
493494
{name: "filter involving value of current node on right hand side", path: "$[?(1==@ || 2== @ )]"},
494495
}
@@ -502,7 +503,19 @@ func TestTokenizer_NoPanic(t *testing.T) {
502503
}()
503504

504505
tokenizer := NewTokenizer(tc.path)
505-
_ = tokenizer.Tokenize()
506+
tokenizedJsonPath := tokenizer.Tokenize()
507+
foundIllegal := false
508+
for _, token := range tokenizedJsonPath {
509+
if token.Token == ILLEGAL {
510+
foundIllegal = true
511+
if !tc.illegal {
512+
t.Errorf(tokenizer.ErrorString(token, "Illegal Token"))
513+
}
514+
}
515+
}
516+
if tc.illegal && !foundIllegal {
517+
t.Errorf(tokenizer.ErrorTokenString(tokenizedJsonPath[0], "Expected an illegal token"))
518+
}
506519
})
507520
}
508521
}

0 commit comments

Comments
 (0)