-
Notifications
You must be signed in to change notification settings - Fork 11
/
compatible.test.js
153 lines (134 loc) · 5.17 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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 i
return [i.rule, i.data || {}, i.result]
}))
}
}
// eslint-disable-next-line no-labels
inline: {
const logic = new LogicEngine(undefined, { compatible: true })
const asyncLogic = new AsyncLogicEngine(undefined, { compatible: true })
const logicWithoutOptimization = new LogicEngine(undefined, { compatible: true })
const asyncLogicWithoutOptimization = new AsyncLogicEngine(undefined, { compatible: true })
describe('All of the compatible tests', () => {
tests.forEach((testCase) => {
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)}`, () => {
expect(logic.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (async)`, async () => {
expect(await asyncLogic.run(testCase[0], testCase[1])).toStrictEqual(
testCase[2]
)
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (built)`, () => {
const f = logic.build(testCase[0])
expect(f(testCase[1])).toStrictEqual(testCase[2])
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (asyncBuilt)`, async () => {
const f = await asyncLogic.build(testCase[0])
expect(await f(testCase[1])).toStrictEqual(testCase[2])
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (noOptimization)`, () => {
expect(logicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (asyncNoOptimization)`, async () => {
expect(await asyncLogicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(
testCase[2]
)
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (builtNoOptimization)`, () => {
const f = logicWithoutOptimization.build(testCase[0])
expect(f(testCase[1])).toStrictEqual(testCase[2])
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (asyncBuiltNoOptimization)`, async () => {
const f = await asyncLogicWithoutOptimization.build(testCase[0])
expect(await f(testCase[1])).toStrictEqual(testCase[2])
})
})
})
}
// eslint-disable-next-line no-labels
notInline: {
const logic = new LogicEngine(undefined, { compatible: true })
const asyncLogic = new AsyncLogicEngine(undefined, { compatible: true })
const logicWithoutOptimization = new LogicEngine(undefined, { compatible: true })
const asyncLogicWithoutOptimization = new AsyncLogicEngine(undefined, { compatible: true })
logicWithoutOptimization.disableInline = true
logic.disableInline = true
asyncLogic.disableInline = true
asyncLogicWithoutOptimization.disableInline = true
// using a loop to disable the inline compilation mechanism.
tests.forEach((testCase) => {
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)}`, () => {
expect(logic.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (async)`, async () => {
expect(await asyncLogic.run(testCase[0], testCase[1])).toStrictEqual(
testCase[2]
)
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (built)`, () => {
const f = logic.build(testCase[0])
expect(f(testCase[1])).toStrictEqual(testCase[2])
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (asyncBuilt)`, async () => {
const f = await asyncLogic.build(testCase[0])
expect(await f(testCase[1])).toStrictEqual(testCase[2])
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (noOptimization)`, () => {
expect(logicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (asyncNoOptimization)`, async () => {
expect(await asyncLogicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(
testCase[2]
)
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (builtNoOptimization)`, () => {
const f = logicWithoutOptimization.build(testCase[0])
expect(f(testCase[1])).toStrictEqual(testCase[2])
})
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (asyncBuiltNoOptimization)`, async () => {
const f = await asyncLogicWithoutOptimization.build(testCase[0])
expect(await f(testCase[1])).toStrictEqual(testCase[2])
})
})
}