@@ -5,6 +5,7 @@ import suite from '../test-suite/stringify.test.json'
55import schema from '../test-suite/stringify.test.schema.json'
66import { compile } from './compile'
77import { stringify } from './stringify'
8+ import type { JSONQueryStringifyOptions } from './types'
89
910const groupByCategory = compile ( [ 'groupBy' , [ 'get' , 'category' ] ] )
1011const testsByCategory = groupByCategory ( suite . groups ) as Record <
@@ -30,6 +31,46 @@ for (const [category, testGroups] of Object.entries(testsByCategory)) {
3031 } )
3132}
3233
34+ describe ( 'customization' , ( ) => {
35+ test ( 'should stringify a custom operator' , ( ) => {
36+ const options : JSONQueryStringifyOptions = {
37+ operators : [ { name : 'aboutEq' , op : '~=' , at : '==' } ]
38+ }
39+
40+ expect ( stringify ( [ 'aboutEq' , 2 , 3 ] , options ) ) . toEqual ( '2 ~= 3' )
41+ expect ( stringify ( [ 'filter' , [ 'aboutEq' , 2 , 3 ] ] , options ) ) . toEqual ( 'filter(2 ~= 3)' )
42+ expect ( stringify ( [ 'object' , { result : [ 'aboutEq' , 2 , 3 ] } ] , options ) ) . toEqual (
43+ '{ result: 2 ~= 3 }'
44+ )
45+ // existing operators should still be there
46+ expect ( stringify ( [ 'eq' , 2 , 3 ] , options ) ) . toEqual ( '2 == 3' )
47+
48+ // test precedence and parenthesis
49+ expect ( stringify ( [ 'aboutEq' , [ 'aboutEq' , 2 , 3 ] , 4 ] , options ) ) . toEqual ( '(2 ~= 3) ~= 4' )
50+ expect ( stringify ( [ 'aboutEq' , 2 , [ 'aboutEq' , 3 , 4 ] ] , options ) ) . toEqual ( '2 ~= (3 ~= 4)' )
51+ expect ( stringify ( [ 'aboutEq' , [ 'and' , 2 , 3 ] , 4 ] , options ) ) . toEqual ( '(2 and 3) ~= 4' )
52+ expect ( stringify ( [ 'aboutEq' , 2 , [ 'and' , 3 , 4 ] ] , options ) ) . toEqual ( '2 ~= (3 and 4)' )
53+ expect ( stringify ( [ 'and' , [ 'aboutEq' , 2 , 3 ] , 4 ] , options ) ) . toEqual ( '2 ~= 3 and 4' )
54+ expect ( stringify ( [ 'and' , 2 , [ 'aboutEq' , 3 , 4 ] ] , options ) ) . toEqual ( '2 and 3 ~= 4' )
55+ expect ( stringify ( [ 'aboutEq' , [ 'add' , 2 , 3 ] , 4 ] , options ) ) . toEqual ( '2 + 3 ~= 4' )
56+ expect ( stringify ( [ 'aboutEq' , 2 , [ 'add' , 3 , 4 ] ] , options ) ) . toEqual ( '2 ~= 3 + 4' )
57+ expect ( stringify ( [ 'add' , [ 'aboutEq' , 2 , 3 ] , 4 ] , options ) ) . toEqual ( '(2 ~= 3) + 4' )
58+ expect ( stringify ( [ 'add' , 2 , [ 'aboutEq' , 3 , 4 ] ] , options ) ) . toEqual ( '2 + (3 ~= 4)' )
59+ } )
60+
61+ test ( 'should stringify a custom operator which is leftAssociative' , ( ) => {
62+ const options : JSONQueryStringifyOptions = {
63+ operators : [ { name : 'aboutEq' , op : '~=' , at : '==' , leftAssociative : true } ]
64+ }
65+
66+ expect ( stringify ( [ 'aboutEq' , [ 'aboutEq' , 2 , 3 ] , 4 ] , options ) ) . toEqual ( '2 ~= 3 ~= 4' )
67+ expect ( stringify ( [ 'aboutEq' , 2 , [ 'aboutEq' , 3 , 4 ] ] , options ) ) . toEqual ( '2 ~= (3 ~= 4)' )
68+ } )
69+
70+ // Note: we do not test the option `CustomOperator.vararg`
71+ // since they have no effect on stringification, only on parsing.
72+ } )
73+
3374describe ( 'test-suite' , ( ) => {
3475 test ( 'should validate the stringify test-suite against its JSON schema' , ( ) => {
3576 const ajv = new Ajv ( { allErrors : false } )
0 commit comments