Skip to content

Commit b4b3bc7

Browse files
committed
Improved test suite
1. Added Index file to link all test files 2. compatible.test.js now loads from index 3. Fixed additional.json to new test case format
1 parent d6f02aa commit b4b3bc7

File tree

3 files changed

+72
-29
lines changed

3 files changed

+72
-29
lines changed

compatible.test.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@ import { LogicEngine, AsyncLogicEngine } from './index.js'
44

55
const tests = []
66

7-
// get all json files from "suites" directory, 1 layer of depth
8-
const files = fs.readdirSync('./suites', { withFileTypes: true }).flatMap(i => {
9-
if (i.isDirectory()) return fs.readdirSync(`./suites/${i.name}`).map(j => `${i.name}/${j}`)
10-
return i.name
11-
})
7+
// Load test files from index.json
8+
const indexContent = JSON.parse(fs.readFileSync('./suites/index.json').toString())
129

13-
for (const file of files) {
14-
if (file.endsWith('.json')) {
15-
tests.push(...JSON.parse(fs.readFileSync(`./suites/${file}`).toString()).filter(i => typeof i !== 'string').map(i => {
16-
if (Array.isArray(i)) return { rule: i[0], data: i[1] || null, result: i[2], description: JSON.stringify(i[0]) }
17-
return i
18-
}))
19-
}
10+
for (const file of indexContent) {
11+
const testContent = JSON.parse(fs.readFileSync(`./suites/${file}`).toString())
12+
tests.push(...testContent.filter(i => typeof i !== 'string').map(i => {
13+
if (Array.isArray(i)) return { rule: i[0], data: i[1] || null, result: i[2], description: JSON.stringify(i[0]) }
14+
return i
15+
}))
2016
}
2117

2218
function correction (x) {

suites/additional.json

+29-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
[
22
"# These are some tests from https://github.com/TotalTechGeek/json-logic-engine/commit/9125e91b5137938a8319de1103b0ebc5819e54e1",
3-
[
4-
[
3+
{
4+
"description": "Rule array with val",
5+
"rule": [
56
1,
67
{
78
"val": "x"
89
},
910
3
1011
],
11-
{
12+
"data": {
1213
"x": 2
1314
},
14-
[
15+
"result": [
1516
1,
1617
2,
1718
3
1819
]
19-
],
20-
[
21-
{
20+
},
21+
{
22+
"description": "If Operator fetching both condition and consequent from data",
23+
"rule": {
2224
"if": [
2325
{
2426
"val": "x"
@@ -31,16 +33,17 @@
3133
99
3234
]
3335
},
34-
{
36+
"data": {
3537
"x": true,
3638
"y": 42
3739
},
38-
[
40+
"result": [
3941
42
4042
]
41-
],
42-
[
43-
{
43+
},
44+
{
45+
"description": "Reduce Operator with val",
46+
"rule": {
4447
"reduce": [
4548
{
4649
"val": "integers"
@@ -60,7 +63,7 @@
6063
}
6164
]
6265
},
63-
{
66+
"data": {
6467
"integers": [
6568
1,
6669
2,
@@ -69,12 +72,21 @@
6972
],
7073
"start_with": 59
7174
},
72-
69
73-
],
75+
"result": 69
76+
},
7477
{
7578
"description": "Simple Inlineable Val Chained",
76-
"rule": { "val": { "cat": ["te", "st"] } },
77-
"data": { "test": 1 },
79+
"rule": {
80+
"val": {
81+
"cat": [
82+
"te",
83+
"st"
84+
]
85+
}
86+
},
87+
"data": {
88+
"test": 1
89+
},
7890
"result": 1
7991
}
8092
]

suites/index.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[
2+
"compatible.json",
3+
"arithmetic/plus.json",
4+
"arithmetic/plus.extra.json",
5+
"arithmetic/multiply.json",
6+
"arithmetic/multiply.extra.json",
7+
"arithmetic/minus.json",
8+
"arithmetic/minus.extra.json",
9+
"arithmetic/divide.json",
10+
"arithmetic/divide.extra.json",
11+
"arithmetic/modulo.json",
12+
"arithmetic/modulo.extra.json",
13+
"comparison/greaterThan.json",
14+
"comparison/greaterThanEquals.json",
15+
"comparison/lessThan.json",
16+
"comparison/lessThanEquals.json",
17+
"comparison/softEquals.json",
18+
"comparison/softNotEquals.json",
19+
"comparison/strictEquals.json",
20+
"comparison/strictNotEquals.json",
21+
"control/and.json",
22+
"control/if.json",
23+
"control/or.json",
24+
"truthiness.json",
25+
"additional.json",
26+
"coalesce.json",
27+
"chained.json",
28+
"iterators.extra.json",
29+
"exists.json",
30+
"scopes.json",
31+
"throw.json",
32+
"try.json",
33+
"val.json",
34+
"val-compat.json"
35+
]

0 commit comments

Comments
 (0)