Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions compatible.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ import { LogicEngine, AsyncLogicEngine } from './index.js'

const tests = []

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

for (const file of files) {
if (file.endsWith('.json')) {
tests.push(...JSON.parse(fs.readFileSync(`./suites/${file}`).toString()).filter(i => typeof i !== 'string').map(i => {
if (Array.isArray(i)) return { rule: i[0], data: i[1] || null, result: i[2], description: JSON.stringify(i[0]) }
return i
}))
}
for (const file of indexContent) {
const testContent = JSON.parse(fs.readFileSync(`./suites/${file}`).toString())
tests.push(...testContent.filter(i => typeof i !== 'string').map(i => {
if (Array.isArray(i)) return { rule: i[0], data: i[1] || null, result: i[2], description: JSON.stringify(i[0]) }
return i
}))
}

function correction (x) {
Expand Down
46 changes: 29 additions & 17 deletions suites/additional.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
[
"# These are some tests from https://github.com/TotalTechGeek/json-logic-engine/commit/9125e91b5137938a8319de1103b0ebc5819e54e1",
[
[
{
"description": "Rule array with val",
"rule": [
1,
{
"val": "x"
},
3
],
{
"data": {
"x": 2
},
[
"result": [
1,
2,
3
]
],
[
{
},
{
"description": "If Operator fetching both condition and consequent from data",
"rule": {
"if": [
{
"val": "x"
Expand All @@ -31,16 +33,17 @@
99
]
},
{
"data": {
"x": true,
"y": 42
},
[
"result": [
42
]
],
[
{
},
{
"description": "Reduce Operator with val",
"rule": {
"reduce": [
{
"val": "integers"
Expand All @@ -60,7 +63,7 @@
}
]
},
{
"data": {
"integers": [
1,
2,
Expand All @@ -69,12 +72,21 @@
],
"start_with": 59
},
69
],
"result": 69
},
{
"description": "Simple Inlineable Val Chained",
"rule": { "val": { "cat": ["te", "st"] } },
"data": { "test": 1 },
"rule": {
"val": {
"cat": [
"te",
"st"
]
}
},
"data": {
"test": 1
},
"result": 1
}
]
35 changes: 35 additions & 0 deletions suites/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
"compatible.json",
"arithmetic/plus.json",
"arithmetic/plus.extra.json",
"arithmetic/multiply.json",
"arithmetic/multiply.extra.json",
"arithmetic/minus.json",
"arithmetic/minus.extra.json",
"arithmetic/divide.json",
"arithmetic/divide.extra.json",
"arithmetic/modulo.json",
"arithmetic/modulo.extra.json",
"comparison/greaterThan.json",
"comparison/greaterThanEquals.json",
"comparison/lessThan.json",
"comparison/lessThanEquals.json",
"comparison/softEquals.json",
"comparison/softNotEquals.json",
"comparison/strictEquals.json",
"comparison/strictNotEquals.json",
"control/and.json",
"control/if.json",
"control/or.json",
"truthiness.json",
"additional.json",
"coalesce.json",
"chained.json",
"iterators.extra.json",
"exists.json",
"scopes.json",
"throw.json",
"try.json",
"val.json",
"val-compat.json"
]