Skip to content

Commit e0588de

Browse files
committed
fix: move custom operator tests from Test Suite to JS tests
1 parent 9369c81 commit e0588de

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

src/stringify.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import suite from '../test-suite/stringify.test.json'
55
import schema from '../test-suite/stringify.test.schema.json'
66
import { compile } from './compile'
77
import { stringify } from './stringify'
8+
import type { JSONQueryStringifyOptions } from './types'
89

910
const groupByCategory = compile(['groupBy', ['get', 'category']])
1011
const 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+
3374
describe('test-suite', () => {
3475
test('should validate the stringify test-suite against its JSON schema', () => {
3576
const ajv = new Ajv({ allErrors: false })

test-suite/stringify.test.json

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,6 @@
126126
{ "input": ["mod", 2, 3, 4], "output": "2 % 3 % 4" }
127127
]
128128
},
129-
{
130-
"category": "operator",
131-
"description": "should stringify a custom operator",
132-
"options": {
133-
"operators": [{ "name": "aboutEq", "op": "~=", "at": "==" }]
134-
},
135-
"tests": [
136-
{ "input": ["aboutEq", 2, 3], "output": "2 ~= 3" },
137-
{ "input": ["filter", ["aboutEq", 2, 3]], "output": "filter(2 ~= 3)" },
138-
{ "input": ["object", { "result": ["aboutEq", 2, 3] }], "output": "{ result: 2 ~= 3 }" },
139-
{ "input": ["eq", 2, 3], "output": "2 == 3" }
140-
]
141-
},
142129
{
143130
"category": "function",
144131
"description": "should stringify a function",

test-suite/stringify.test.schema.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@
2020
"type": "object",
2121
"properties": {
2222
"indentation": { "type": "string" },
23-
"maxLineLength": { "type": "number" },
24-
"operators": {
25-
"type": "array",
26-
"items": { "type": "object" }
27-
}
23+
"maxLineLength": { "type": "number" }
2824
}
2925
},
3026
"tests": {

0 commit comments

Comments
 (0)