@@ -291,47 +291,48 @@ func TestTokenizer(t *testing.T) {
291
291
}
292
292
}
293
293
294
- func TestTokenizer_NoPanic (t * testing.T ) {
294
+ func TestTokenizer_categorize (t * testing.T ) {
295
295
testCases := []struct {
296
296
name string
297
297
path string
298
298
illegal bool
299
+ simple bool
299
300
}{
300
- {name : "identity" , path : "" },
301
- {name : "root" , path : "$" },
301
+ {name : "identity" , path : "" , simple : true },
302
+ {name : "root" , path : "$" , simple : true },
302
303
{name : "unmatched closing parenthesis" , path : ")" , illegal : true },
303
304
{name : "unmatched closing square bracket" , path : "]" , illegal : true },
304
- {name : "dot child" , path : "$.child" },
305
+ {name : "dot child" , path : "$.child" , simple : true },
305
306
{name : "dot child with implicit root" , path : ".child" },
306
307
{name : "undotted child with implicit root" , path : "child" },
307
- {name : "dot child with no name" , path : "$." },
308
- {name : "dot child with missing dot" , path : "$a" },
309
- {name : "dot child with trailing dot" , path : "$.child." },
310
- {name : "dot child of dot child" , path : "$.child1.child2" },
308
+ {name : "dot child with no name" , path : "$." , simple : true },
309
+ {name : "dot child with missing dot" , path : "$a" , simple : true },
310
+ {name : "dot child with trailing dot" , path : "$.child." , simple : true },
311
+ {name : "dot child of dot child" , path : "$.child1.child2" , simple : true },
311
312
{name : "dot child with array subscript" , path : "$.child[*]" },
312
313
{name : "dot child with malformed array subscript" , path : "$.child[1:2:3:4]" },
313
314
{name : "dot child with array subscript with zero step" , path : "$.child[1:2:0]" },
314
315
{name : "dot child with non-integer array subscript" , path : "$.child[1:2:a]" },
315
316
{name : "dot child with unclosed array subscript" , path : "$.child[*" },
316
- {name : "dot child with missing array subscript" , path : "$.child[]" },
317
- {name : "dot child with embedded space" , path : "$.child more" },
318
- {name : "bracket child" , path : "$['child']" },
319
- {name : "bracket child with double quotes" , path : `$["child"]` },
317
+ {name : "dot child with missing array subscript" , path : "$.child[]" , simple : true },
318
+ {name : "dot child with embedded space" , path : "$.child more" , simple : true },
319
+ {name : "bracket child" , path : "$['child']" , simple : true },
320
+ {name : "bracket child with double quotes" , path : `$["child"]` , simple : true },
320
321
{name : "bracket child with unmatched quotes" , path : `$["child']` , illegal : true },
321
- {name : "bracket child with empty name" , path : "$['']" },
322
- {name : "bracket child of bracket child" , path : "$['child1']['child2']" },
323
- {name : "double quoted bracket child of bracket child" , path : `$['child1']["child2"]` },
322
+ {name : "bracket child with empty name" , path : "$['']" , simple : true },
323
+ {name : "bracket child of bracket child" , path : "$['child1']['child2']" , simple : true },
324
+ {name : "double quoted bracket child of bracket child" , path : `$['child1']["child2"]` , simple : true },
324
325
{name : "bracket child union" , path : "$['child','child2']" },
325
326
{name : "bracket child union with whitespace" , path : "$[ 'child' , 'child2' ]" },
326
327
{name : "bracket child union with mixed quotes" , path : `$[ 'child' , "child2" ]` },
327
- {name : "bracket child quoted union literal" , path : "$[',']" },
328
+ {name : "bracket child quoted union literal" , path : "$[',']" , simple : true },
328
329
{name : "bracket child with array subscript" , path : "$['child'][*]" },
329
330
{name : "bracket child with malformed array subscript" , path : "$['child'][1:2:3:4]" },
330
331
{name : "bracket child with non-integer array subscript" , path : "$['child'][1:2:a]" },
331
332
{name : "bracket child with unclosed array subscript" , path : "$['child'][*" },
332
- {name : "bracket child with missing array subscript" , path : "$['child'][]" },
333
- {name : "bracket child followed by space" , path : "$['child'] " },
334
- {name : "bracket dotted child" , path : "$['child1.child2']" },
333
+ {name : "bracket child with missing array subscript" , path : "$['child'][]" , simple : true },
334
+ {name : "bracket child followed by space" , path : "$['child'] " , simple : true },
335
+ {name : "bracket dotted child" , path : "$['child1.child2']" , simple : true },
335
336
{name : "bracket child with array subscript" , path : "$['child'][*]" },
336
337
{name : "property name dot child" , path : "$.child~" , illegal : true },
337
338
{name : "property name dot child with implicit root" , path : ".child~" , illegal : true },
@@ -374,9 +375,9 @@ func TestTokenizer_NoPanic(t *testing.T) {
374
375
{name : "bracket child with malformed array subscript" , path : "$['child'][1:2:3:4]" },
375
376
{name : "bracket child with malformed array subscript in union" , path : "$['child'][0,1:2:3:4]" },
376
377
{name : "bracket child with non-integer array subscript" , path : "$['child'][1:2:a]" },
377
- {name : "bracket child of dot child" , path : "$.child1['child2']" },
378
+ {name : "bracket child of dot child" , path : "$.child1['child2']" , simple : true },
378
379
{name : "array slice of root" , path : "$[1:3]" },
379
- {name : "dot child of bracket child" , path : "$['child1'].child2" },
380
+ {name : "dot child of bracket child" , path : "$['child1'].child2" , simple : true },
380
381
{name : "recursive descent" , path : "$..child" },
381
382
{name : "recursive descent of dot child" , path : "$.child1..child2" },
382
383
{name : "recursive descent of bracket child" , path : "$['child1']..child2" },
@@ -484,10 +485,10 @@ func TestTokenizer_NoPanic(t *testing.T) {
484
485
{name : "filter regular expression to match float literal" , path : `$[?(.1=~/.*/)]` , illegal : true },
485
486
{name : "filter invalid regular expression" , path : `$[?(@.child=~/(.*/)]` , illegal : true },
486
487
{name : "unescaped single quote in bracket child name" , path : `$['single'quote']` , illegal : true },
487
- {name : "escaped single quote in bracket child name" , path : `$['single\']quote']` },
488
- {name : "escaped backslash in bracket child name" , path : `$['\\']` },
488
+ {name : "escaped single quote in bracket child name" , path : `$['single\']quote']` , simple : true },
489
+ {name : "escaped backslash in bracket child name" , path : `$['\\']` , simple : true },
489
490
{name : "unescaped single quote after escaped backslash in bracket child name" , path : `$['single\\'quote']` , illegal : true },
490
- {name : "unsupported escape sequence in bracket child name" , path : `$['\n']` },
491
+ {name : "unsupported escape sequence in bracket child name" , path : `$['\n']` , simple : true },
491
492
{name : "unclosed and empty bracket child name with space" , path : `$[ '` , illegal : true },
492
493
{name : "unclosed and empty bracket child name with formfeed" , path : "[\f '" , illegal : true },
493
494
{name : "filter involving value of current node on left hand side" , path : "$[?(@==1)]" },
@@ -496,11 +497,11 @@ func TestTokenizer_NoPanic(t *testing.T) {
496
497
497
498
for _ , tc := range testCases {
498
499
t .Run (tc .name , func (t * testing.T ) {
499
- defer func () {
500
- if r := recover (); r != nil {
501
- t .Errorf ("Tokenizer panicked for path: %s\n Panic: %v" , tc .path , r )
502
- }
503
- }()
500
+ // defer func() {
501
+ // if r := recover(); r != nil {
502
+ // t.Errorf("Tokenizer panicked for path: %s\nPanic: %v", tc.path, r)
503
+ // }
504
+ // }()
504
505
505
506
tokenizer := NewTokenizer (tc .path )
506
507
tokenizedJsonPath := tokenizer .Tokenize ()
@@ -516,6 +517,28 @@ func TestTokenizer_NoPanic(t *testing.T) {
516
517
if tc .illegal && ! foundIllegal {
517
518
t .Errorf (tokenizer .ErrorTokenString (tokenizedJsonPath [0 ], "Expected an illegal token" ))
518
519
}
520
+
521
+ if tc .simple && foundIllegal {
522
+ t .Errorf ("Expected a simple path, but found an illegal token" )
523
+ }
524
+
525
+ if tc .simple && ! tokenizedJsonPath .IsSimple () {
526
+ for _ , token := range tokenizedJsonPath {
527
+
528
+ simple := false
529
+ for _ , subToken := range SimpleTokens {
530
+ if token .Token == subToken {
531
+ simple = true
532
+ }
533
+ }
534
+ if ! simple {
535
+ t .Errorf (tokenizer .ErrorString (token , "Expected a simple path, but found a non-simple token" ))
536
+ }
537
+ }
538
+ }
539
+ if ! tc .simple && tokenizedJsonPath .IsSimple () {
540
+ t .Errorf (tokenizer .ErrorTokenString (tokenizedJsonPath [0 ], "Expected a non-simple path, but found it was simple" ))
541
+ }
519
542
})
520
543
}
521
544
}
0 commit comments