-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcompatible.test.js
87 lines (80 loc) · 2.85 KB
/
compatible.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* eslint-disable no-ex-assign */
import fs from 'fs'
import { LogicEngine, AsyncLogicEngine } from './index.js'
const tests = []
// get all json files from "suites" directory
const files = fs.readdirSync('./suites')
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
}))
}
}
function correction (x) {
// eslint-disable-next-line no-compare-neg-zero
if (x === -0) return 0
if (Number.isNaN(x)) return { error: 'NaN' }
return x
}
const engines = []
for (let i = 0; i < 8; i++) {
let res = 'sync'
let engine = new LogicEngine(undefined, { compatible: true })
// sync / async
if (i & 1) {
engine = new AsyncLogicEngine(undefined, { compatible: true })
res = 'async'
}
// inline / disabled
if (i & 2) {
engine.disableInline = true
res += ' no-inline'
}
// optimized / not optimized
if (i & 4) {
engine.disableInterpretedOptimization = true
res += ' no-optimized'
}
engines.push([engine, res])
}
describe('All of the compatible tests', () => {
for (const testCase of tests) {
for (const engine of engines) {
test(`${engine[1]} ${JSON.stringify(testCase.rule)} ${JSON.stringify(
testCase.data
)}`, async () => {
try {
let result = await engine[0].run(testCase.rule, testCase.data)
if ((result || 0).toNumber) result = Number(result)
if (Array.isArray(result)) result = result.map(i => (i || 0).toNumber ? Number(i) : i)
expect(correction(result)).toStrictEqual(testCase.result)
expect(testCase.error).toBeUndefined()
} catch (err) {
if (err.message && err.message.includes('expect')) throw err
if (Number.isNaN(err)) err = { type: 'NaN' }
else if (err.message) err = { type: err.message }
expect(err).toMatchObject(testCase.error)
}
})
test(`${engine[1]} ${JSON.stringify(testCase.rule)} ${JSON.stringify(
testCase.data
)} (built)`, async () => {
try {
const f = await engine[0].build(testCase.rule)
let result = await f(testCase.data)
if ((result || 0).toNumber) result = Number(result)
if (Array.isArray(result)) result = result.map(i => i.toNumber ? Number(i) : i)
expect(correction(result)).toStrictEqual(testCase.result)
expect(testCase.error).toBeUndefined()
} catch (err) {
if (err.message && err.message.includes('expect')) throw err
if (Number.isNaN(err)) err = { type: 'NaN' }
else if (err.message) err = { type: err.message }
expect(err).toMatchObject(testCase.error)
}
})
}
}
})