@@ -293,13 +293,14 @@ func TestTokenizer(t *testing.T) {
293
293
294
294
func TestTokenizer_NoPanic (t * testing.T ) {
295
295
testCases := []struct {
296
- name string
297
- path string
296
+ name string
297
+ path string
298
+ illegal bool
298
299
}{
299
300
{name : "identity" , path : "" },
300
301
{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 },
303
304
{name : "dot child" , path : "$.child" },
304
305
{name : "dot child with implicit root" , path : ".child" },
305
306
{name : "undotted child with implicit root" , path : "child" },
@@ -316,7 +317,7 @@ func TestTokenizer_NoPanic(t *testing.T) {
316
317
{name : "dot child with embedded space" , path : "$.child more" },
317
318
{name : "bracket child" , path : "$['child']" },
318
319
{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 },
320
321
{name : "bracket child with empty name" , path : "$['']" },
321
322
{name : "bracket child of bracket child" , path : "$['child1']['child2']" },
322
323
{name : "double quoted bracket child of bracket child" , path : `$['child1']["child2"]` },
@@ -332,42 +333,42 @@ func TestTokenizer_NoPanic(t *testing.T) {
332
333
{name : "bracket child followed by space" , path : "$['child'] " },
333
334
{name : "bracket dotted child" , path : "$['child1.child2']" },
334
335
{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 },
371
372
{name : "array union" , path : "$[0,1]" },
372
373
{name : "array union with whitespace" , path : "$[ 0 , 1 ]" },
373
374
{name : "bracket child with malformed array subscript" , path : "$['child'][1:2:3:4]" },
@@ -397,8 +398,8 @@ func TestTokenizer_NoPanic(t *testing.T) {
397
398
{name : "missing filter " , path : "$[?()]" },
398
399
{name : "unclosed filter" , path : "$[?(" },
399
400
{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 },
402
403
{
name :
"simple negative filter" ,
path :
"$[?([email protected] )]" },
403
404
{
name :
"misplaced filter negation" ,
path :
"$[?(@.child [email protected] )]" },
404
405
{name : "simple negative filter with extra whitespace" , path : "$[?( ! @.child)]" },
@@ -408,14 +409,14 @@ func TestTokenizer_NoPanic(t *testing.T) {
408
409
{name : "filter string equality with apparent boolean" , path : `$[?(@.child=="true")]` },
409
410
{name : "filter string equality with apparent null" , path : `$[?(@.child=="null")]` },
410
411
{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 },
412
413
{name : "filter integer equality with integer literal which is too large" , path : "$[?(@.child==9223372036854775808)]" },
413
414
{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 },
415
416
{
name :
"filter integer equality, literal on the left" ,
path :
"$[?([email protected] )]" },
416
417
{
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 },
419
420
{name : "filter boolean true equality, literal on the right" , path : "$[?(@.child== true )]" },
420
421
{name : "filter boolean false equality, literal on the right" , path : "$[?(@.child==false)]" },
421
422
{
name :
"filter boolean true equality, literal on the left" ,
path :
"$[?([email protected] )]" },
@@ -429,8 +430,8 @@ func TestTokenizer_NoPanic(t *testing.T) {
429
430
{
name :
"filter integer equality, root path on the left" ,
path :
"$[?([email protected] )]" },
430
431
{name : "filter string equality, literal on the right" , path : "$[?(@.child=='x')]" },
431
432
{
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 },
434
435
{name : "filter integer inequality, literal on the right" , path : "$[?(@.child!=1)]" },
435
436
{name : "filter inequality with missing left hand operator" , path : "$[?(!=1)]" },
436
437
{name : "filter equality with missing right hand value" , path : "$[?(@.child!=)]" },
@@ -460,7 +461,7 @@ func TestTokenizer_NoPanic(t *testing.T) {
460
461
{name : "filter less than or equal, string on the right" , path : "$[?(@.child<='x')]" },
461
462
{
name :
"filter less than or equal, string on the left" ,
path :
"$[?('x'<[email protected] )]" },
462
463
{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 },
464
465
{name : "filter conjunction with bracket children" , path : "$[?(@['child'][*]&&@['other'])]" },
465
466
{name : "filter invalid leading conjunction" , path : "$[?(&&" },
466
467
{name : "filter conjunction with extra whitespace" , path : "$[?(@.child && @.other)]" },
@@ -473,22 +474,22 @@ func TestTokenizer_NoPanic(t *testing.T) {
473
474
{
name :
"filter negation" ,
path :
"$[?([email protected] )]" },
474
475
{
name :
"filter negation of comparison (edge case)" ,
path :
"$[?([email protected] >1)]" },
475
476
{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 },
486
487
{name : "escaped single quote in bracket child name" , path : `$['single\']quote']` },
487
488
{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 },
489
490
{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 },
492
493
{name : "filter involving value of current node on left hand side" , path : "$[?(@==1)]" },
493
494
{name : "filter involving value of current node on right hand side" , path : "$[?(1==@ || 2== @ )]" },
494
495
}
@@ -502,7 +503,19 @@ func TestTokenizer_NoPanic(t *testing.T) {
502
503
}()
503
504
504
505
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
+ }
506
519
})
507
520
}
508
521
}
0 commit comments