Skip to content

Commit 9268a03

Browse files
committed
chore: write unit tests for the functions that throw a "Too many arguments" error
1 parent 2e9a2d3 commit 9268a03

File tree

5 files changed

+113
-20
lines changed

5 files changed

+113
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ When using multiple operators, they will be evaluated according to their precede
208208
filter(.age >= 18 and .age <= 65)
209209
```
210210

211-
Note that some operators, like `and`, `or`, `+`, and `-`, support more than two values, like `2 + 3 + 4`. Others, like `^` and `==`, do not support more than two values. If needed, it is always possible to use parenthesis, like `(2 ^ 3) ^ 4`.
211+
Note that some operators, like `and`, `or`, `+`, and `-`, support more than two values and are evaluated left-to-right (left associative), like `2 + 3 + 4` Others, like `^` and `==`, do not support more than two values. If needed, it is always possible to use parenthesis, like `(2 ^ 3) ^ 4`.
212212

213213
The operators have the following precedence, from highest to lowest:
214214

src/compile.test.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import Ajv from 'ajv'
22
import { describe, expect, test } from 'vitest'
3-
import type { CompileTestSuite } from '../test-suite/compile.test'
3+
import type { CompileTestException, CompileTestSuite } from '../test-suite/compile.test'
44
import suite from '../test-suite/compile.test.json'
55
import schema from '../test-suite/compile.test.schema.json'
66
import { compile } from './compile'
77
import { buildFunction } from './functions'
88
import type { JSONQuery, JSONQueryCompileOptions } from './types'
99

10+
function isTestException(test: unknown): test is CompileTestException {
11+
return !!test && typeof (test as Record<string, unknown>).throws === 'string'
12+
}
13+
1014
const data = [
1115
{ name: 'Chris', age: 23, city: 'New York' },
1216
{ name: 'Emily', age: 19, city: 'Atlanta' },
@@ -31,13 +35,20 @@ const testsByCategory = groupByCategory(suite.tests) as Record<string, CompileTe
3135
for (const [category, tests] of Object.entries(testsByCategory)) {
3236
describe(category, () => {
3337
for (const currentTest of tests) {
34-
const { description, input, query, output } = currentTest
35-
36-
test(description, () => {
37-
const actualOutput = compile(query)(input)
38-
39-
expect({ input, query, output: actualOutput }).toEqual({ input, query, output })
40-
})
38+
if (isTestException(currentTest)) {
39+
test(currentTest.description, () => {
40+
const { input, query, throws } = currentTest
41+
42+
expect(() => compile(query)(input)).toThrow(throws)
43+
})
44+
} else {
45+
test(currentTest.description, () => {
46+
const { input, query, output } = currentTest
47+
const actualOutput = compile(query)(input)
48+
49+
expect({ input, query, output: actualOutput }).toEqual({ input, query, output })
50+
})
51+
}
4152
}
4253
})
4354
}

test-suite/compile.test.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import type { JSONQuery } from '../src/types'
22

3-
export interface CompileTest {
3+
export interface CompileTestOutput {
44
category: string
55
description: string
66
input: unknown
77
query: JSONQuery
88
output: unknown
99
}
1010

11+
export interface CompileTestException {
12+
category: string
13+
description: string
14+
input: unknown
15+
query: JSONQuery
16+
throws: string
17+
}
18+
19+
export type CompileTest = CompileTestOutput | CompileTestException
20+
1121
export interface CompileTestSuite {
1222
updated: string
1323
tests: CompileTest[]

test-suite/compile.test.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,13 @@
753753
"query": ["eq", 2, 2],
754754
"output": true
755755
},
756+
{
757+
"category": "eq",
758+
"description": "should throw when passing more than two arguments",
759+
"input": null,
760+
"query": ["eq", 2, 3, 4],
761+
"throws": "Too many arguments"
762+
},
756763

757764
{
758765
"category": "gt",
@@ -782,6 +789,13 @@
782789
"query": ["gt", 3, 2],
783790
"output": true
784791
},
792+
{
793+
"category": "gt",
794+
"description": "should throw when passing more than two arguments",
795+
"input": null,
796+
"query": ["gt", 2, 3, 4],
797+
"throws": "Too many arguments"
798+
},
785799

786800
{
787801
"category": "gte",
@@ -811,6 +825,13 @@
811825
"query": ["gte", 3, 2],
812826
"output": true
813827
},
828+
{
829+
"category": "gte",
830+
"description": "should throw when passing more than two arguments",
831+
"input": null,
832+
"query": ["gte", 2, 3, 4],
833+
"throws": "Too many arguments"
834+
},
814835

815836
{
816837
"category": "lt",
@@ -840,6 +861,13 @@
840861
"query": ["lt", 1, 2],
841862
"output": true
842863
},
864+
{
865+
"category": "lt",
866+
"description": "should throw when passing more than two arguments",
867+
"input": null,
868+
"query": ["lt", 2, 3, 4],
869+
"throws": "Too many arguments"
870+
},
843871

844872
{
845873
"category": "lte",
@@ -869,6 +897,13 @@
869897
"query": ["lte", 2, 2],
870898
"output": true
871899
},
900+
{
901+
"category": "lte",
902+
"description": "should throw when passing more than two arguments",
903+
"input": null,
904+
"query": ["lte", 2, 3, 4],
905+
"throws": "Too many arguments"
906+
},
872907

873908
{
874909
"category": "ne",
@@ -898,6 +933,13 @@
898933
"query": ["ne", 3, 2],
899934
"output": true
900935
},
936+
{
937+
"category": "ne",
938+
"description": "should throw when passing more than two arguments",
939+
"input": null,
940+
"query": ["ne", 2, 3, 4],
941+
"throws": "Too many arguments"
942+
},
901943

902944
{
903945
"category": "and",
@@ -1377,6 +1419,13 @@
13771419
"query": ["pow", ["get"], 0.5],
13781420
"output": 5
13791421
},
1422+
{
1423+
"category": "pow",
1424+
"description": "should throw when passing more than two arguments",
1425+
"input": null,
1426+
"query": ["pow", 3, 2, 3],
1427+
"throws": "Too many arguments"
1428+
},
13801429

13811430
{
13821431
"category": "mod",
@@ -1392,6 +1441,13 @@
13921441
"query": ["mod", 8, 3],
13931442
"output": 2
13941443
},
1444+
{
1445+
"category": "mod",
1446+
"description": "should throw when passing more than two arguments",
1447+
"input": null,
1448+
"query": ["mod", 20, 12, 3],
1449+
"throws": "Too many arguments"
1450+
},
13951451

13961452
{
13971453
"category": "abs",

test-suite/compile.test.schema.json

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,32 @@
99
"tests": {
1010
"type": "array",
1111
"items": {
12-
"type": "object",
13-
"properties": {
14-
"category": { "type": "string" },
15-
"description": { "type": "string" },
16-
"input": {},
17-
"query": {},
18-
"output": {}
19-
},
20-
"required": ["category", "description", "input", "query", "output"],
21-
"additionalProperties": false
12+
"oneOf": [
13+
{
14+
"type": "object",
15+
"properties": {
16+
"category": { "type": "string" },
17+
"description": { "type": "string" },
18+
"input": {},
19+
"query": {},
20+
"output": {}
21+
},
22+
"required": ["category", "description", "input", "query", "output"],
23+
"additionalProperties": false
24+
},
25+
{
26+
"type": "object",
27+
"properties": {
28+
"category": { "type": "string" },
29+
"description": { "type": "string" },
30+
"input": {},
31+
"query": {},
32+
"throws": { "type": "string" }
33+
},
34+
"required": ["category", "description", "input", "query", "throws"],
35+
"additionalProperties": false
36+
}
37+
]
2238
}
2339
}
2440
},

0 commit comments

Comments
 (0)