1
1
import { expect } from 'chai' ;
2
2
import forEach from 'mocha-each' ;
3
3
4
- import { parse } from '../parser .mjs' ;
4
+ import parse from '../index .mjs' ;
5
5
6
6
describe ( 'Parser' , ( ) => {
7
7
it ( 'goessner samples' , ( ) => {
@@ -200,6 +200,37 @@ describe('Parser', () => {
200
200
} ) ;
201
201
202
202
it ( 'filter expressions' , ( ) => {
203
+ expect ( parse ( '$[(@.length-1)]' ) ) . to . deep . equal ( [
204
+ {
205
+ type : 'SliceExpression' ,
206
+ value : [ - 1 , Infinity , 1 ] ,
207
+ deep : false ,
208
+ } ,
209
+ ] ) ;
210
+ expect ( parse ( '$[( @.length - 2 )]' ) ) . to . deep . equal ( [
211
+ {
212
+ type : 'SliceExpression' ,
213
+ value : [ - 2 , Infinity , 1 ] ,
214
+ deep : false ,
215
+ } ,
216
+ ] ) ;
217
+ expect ( parse ( '$[( @[ "length" ] - 10 )]' ) ) . to . deep . equal ( [
218
+ {
219
+ type : 'SliceExpression' ,
220
+ value : [ - 10 , Infinity , 1 ] ,
221
+ deep : false ,
222
+ } ,
223
+ ] ) ;
224
+ expect ( parse ( '$[( @["length"] - 5 )]' ) ) . to . deep . equal ( [
225
+ {
226
+ type : 'SliceExpression' ,
227
+ value : [ - 5 , Infinity , 1 ] ,
228
+ deep : false ,
229
+ } ,
230
+ ] ) ;
231
+ } ) ;
232
+
233
+ it ( 'script filter expressions' , ( ) => {
203
234
expect ( parse ( '$[?(@property === "@.schema")]' ) ) . to . deep . equal ( [
204
235
{
205
236
type : 'ScriptFilterExpression' ,
@@ -346,6 +377,36 @@ describe('Parser', () => {
346
377
} ,
347
378
) ;
348
379
380
+ it ( 'skips whitespaces' , ( ) => {
381
+ expect ( parse ( '$.[ name ] [?( @.abc )]\t ..@@test( )' ) ) . to . deep . equal ( [
382
+ {
383
+ type : 'MemberExpression' ,
384
+ value : 'name' ,
385
+ deep : true ,
386
+ } ,
387
+ {
388
+ type : 'ScriptFilterExpression' ,
389
+ value : ' @.abc ' ,
390
+ deep : false ,
391
+ } ,
392
+ {
393
+ type : 'ScriptFilterExpression' ,
394
+ value : '@@test( )' ,
395
+ deep : true ,
396
+ } ,
397
+ ] ) ;
398
+ } ) ;
399
+
400
+ it . skip ( 'handles escapable' , ( ) => {
401
+ expect ( parse ( `$["'name\\"'","test\\\\",'"a']` ) ) . to . deep . equal ( [
402
+ {
403
+ type : 'MultipleMemberExpression' ,
404
+ value : [ 'name"' , 'test\\' ] ,
405
+ deep : false ,
406
+ } ,
407
+ ] ) ;
408
+ } ) ;
409
+
349
410
describe ( 'invalid expressions' , ( ) => {
350
411
it ( 'empty expression or does not start with $' , ( ) => {
351
412
expect ( ( ) => parse ( '' ) ) . to . throw ( 'Expected "$" but end of input found.' ) ;
@@ -355,56 +416,85 @@ describe('Parser', () => {
355
416
356
417
it ( 'invalid member expression' , ( ) => {
357
418
expect ( ( ) => parse ( '$info' ) ) . to . throw (
358
- 'Expected ".", "..", "^", "~", or end of input but "i" found.' ,
419
+ 'Expected ".", "..", "^", "~", or end of input but "i" found at 1 .' ,
359
420
) ;
360
421
expect ( ( ) => parse ( '$.' ) ) . to . throw (
361
- 'Expected "*", "@", "[", [$_\\-], [0-9], or [A-Za-z] but end of input found.' ,
422
+ 'Expected valid name but end of input found at 2 .' ,
362
423
) ;
363
424
} ) ;
364
425
365
426
it ( 'key expression used in the wrong place' , ( ) => {
366
427
expect ( ( ) => parse ( '$.name~.a' ) ) . to . throw (
367
- 'Expected "^", "~", or end of input but "." found.' ,
428
+ 'Expected "^", "~", or end of input but "." found at 7 .' ,
368
429
) ;
369
430
} ) ;
370
431
371
432
it ( 'unclosed quotes' , ( ) => {
372
433
expect ( ( ) => parse ( '$.name["a]' ) ) . to . throw (
373
- `Expected "\\ "" or [^"] but end of input found.` ,
434
+ `Expected """ but end of input found at 10 .` ,
374
435
) ;
375
436
expect ( ( ) => parse ( '$.name["\']' ) ) . to . throw (
376
- `Expected "\\ "" or [^"] but end of input found.` ,
437
+ `Expected """ but end of input found at 10 .` ,
377
438
) ;
378
439
} ) ;
379
440
380
441
it ( 'invalid step in slice expressions' , ( ) => {
381
442
expect ( ( ) => parse ( '$.name[::test]' ) ) . to . throw (
382
- 'Expected "-" or [0-9] but "t" found.' ,
443
+ 'Expected "-" or [0-9] but "t" found at 9.' ,
444
+ ) ;
445
+ expect ( ( ) => parse ( '$.name[::-]' ) ) . to . throw (
446
+ 'Expected [0-9] but "]" found at 10.' ,
383
447
) ;
384
448
} ) ;
385
449
386
450
it ( 'invalid shorthands' , ( ) => {
387
- expect ( ( ) => parse ( '$..@@()' ) ) . to . throw ( 'Expected [a-z] but "(" found.' ) ;
451
+ expect ( ( ) => parse ( '$..@@()' ) ) . to . throw (
452
+ 'Expected [a-z] but "(" found at 5.' ,
453
+ ) ;
388
454
expect ( ( ) => parse ( '$..@@test)' ) ) . to . throw (
389
- 'Expected "()" or [a-z] but ")" found.' ,
455
+ 'Expected "(" but ")" found at 9 .' ,
390
456
) ;
391
457
expect ( ( ) => parse ( '$..@@test(' ) ) . to . throw (
392
- 'Expected "()" or [a-z] but "(" found.' ,
393
- ) ;
394
- expect ( ( ) => parse ( '$..@@test)' ) ) . to . throw (
395
- 'Expected "()" or [a-z] but ")" found.' ,
458
+ 'Expected ")" but end of input found at 10.' ,
396
459
) ;
397
460
expect ( ( ) => parse ( '$..@' ) ) . to . throw (
398
- 'Expected "@" or [a-z] but end of input found.' ,
461
+ 'Expected [a-z] but end of input found at 4.' ,
462
+ ) ;
463
+ } ) ;
464
+
465
+ it ( 'invalid filter expressions' , ( ) => {
466
+ expect ( ( ) => parse ( '$[(' ) ) . to . throw (
467
+ 'Expected "@" but end of input found at 3.' ,
468
+ ) ;
469
+ expect ( ( ) => parse ( '$[(@' ) ) . to . throw (
470
+ 'Expected "." or "[" but end of input found at 4.' ,
471
+ ) ;
472
+ expect ( ( ) => parse ( '$[(@.len - 1)]' ) ) . to . throw (
473
+ 'Expected "length" but "len - " found at 11.' ,
474
+ ) ;
475
+ expect ( ( ) => parse ( '$[(@length - 1)]' ) ) . to . throw (
476
+ 'Expected "." or "[" but "l" found at 4.' ,
477
+ ) ;
478
+ expect ( ( ) => parse ( '$[(@[length]-2)]' ) ) . to . throw (
479
+ `Expected """ or "'" at 5.` ,
480
+ ) ;
481
+ expect ( ( ) => parse ( '$[(@.length + 1))' ) ) . to . throw (
482
+ 'Expected "-" but "+" found at 12.' ,
483
+ ) ;
484
+ expect ( ( ) => parse ( '$[(@.length - -5))' ) ) . to . throw (
485
+ 'Expected positive number but "-5" found at 14.' ,
486
+ ) ;
487
+ expect ( ( ) => parse ( '$[(@.length - 0))' ) ) . to . throw (
488
+ 'Expected positive number but "0" found at 14.' ,
399
489
) ;
400
490
} ) ;
401
491
402
492
it ( 'unclosed brackets' , ( ) => {
403
493
expect ( ( ) => parse ( '$.name[0' ) ) . to . throw (
404
- 'Expected "\'", ",", ":", "\\"", "]", [$_\\-], [0-9], or [A-Za-z] but end of input found.' ,
494
+ 'Expected "]" but end of input found at 8 .' ,
405
495
) ;
406
496
expect ( ( ) => parse ( '$.store["[name]"' ) ) . to . throw (
407
- 'Expected "\'", ",", "\\"", "]", [$_\\-], [0-9], or [A-Za-z] but end of input found.' ,
497
+ 'Expected "]" but end of input found at 16 .' ,
408
498
) ;
409
499
} ) ;
410
500
} ) ;
0 commit comments