-
Notifications
You must be signed in to change notification settings - Fork 11
/
general.test.js
279 lines (234 loc) · 11.1 KB
/
general.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import assert from 'assert'
import { LogicEngine, AsyncLogicEngine } from './index.js'
const normalEngines = [
new LogicEngine(),
new AsyncLogicEngine(),
new LogicEngine(undefined, { disableInterpretedOptimization: true }),
new AsyncLogicEngine(undefined, { disableInterpretedOptimization: true })
]
const permissiveEngines = [
new LogicEngine(undefined, { permissive: true }),
new AsyncLogicEngine(undefined, { permissive: true }),
new LogicEngine(undefined, { disableInterpretedOptimization: true, permissive: true }),
new AsyncLogicEngine(undefined, { disableInterpretedOptimization: true, permissive: true })
]
async function testEngineAsync (engine, rule, data, expected, matcher = 'deepStrictEqual') {
// run
if (expected === Error) {
try {
await engine.run(rule, data)
throw new Error('Should have failed')
} catch (e) {}
} else {
const result = await engine.run(rule, data)
assert[matcher](result, expected)
}
// build
if (expected === Error) {
try {
const built = await engine.build(rule)
await built(data)
throw new Error('Should have failed')
} catch (e) {}
} else {
const built = await engine.build(rule)
const builtResult = await built(data)
assert[matcher](builtResult, expected)
}
}
function testEngine (engine, rule, data, expected, matcher = 'deepStrictEqual') {
if (engine instanceof AsyncLogicEngine) {
return testEngineAsync(engine, rule, data, expected, matcher)
}
// run
if (expected === Error) {
try {
engine.run(rule, data)
throw new Error('Should have failed')
} catch (e) {}
} else {
const result = engine.run(rule, data)
assert[matcher](result, expected)
}
// build
if (expected === Error) {
try {
const built = engine.build(rule)
built(data)
throw new Error('Should have failed')
} catch (e) {}
} else {
const built = engine.build(rule)
const builtResult = built(data)
assert[matcher](builtResult, expected)
}
}
describe('Various Test Cases', () => {
it('Should fail when an unrecognized method is used.', async () => {
for (const engine of normalEngines) await testEngine(engine, { unknown: true }, {}, Error)
})
it('Should return an empty object when I pass in an empty object.', async () => {
for (const engine of normalEngines) await testEngine(engine, {}, {}, {})
for (const engine of permissiveEngines) await testEngine(engine, {}, {}, {})
})
it('Should work with preserve', async () => {
for (const engine of [...permissiveEngines, ...normalEngines]) await testEngine(engine, { preserve: { x: 5 } }, null, { x: 5 })
})
it('Should return the object when an unrecognized method is used.', async () => {
for (const engine of permissiveEngines) {
await testEngine(engine, { unknown: true }, {}, { unknown: true })
await testEngine(engine, {
if: [true, { unknown: true, unknown2: 2 }, 5]
}, {}, { unknown: true, unknown2: 2 })
const obj = { unknown: true, unknown2: 2 }
// test with deterministic function returning a passively preserved element.
await testEngine(engine, {
if: [true, obj, 5]
}, {}, obj, 'equal')
// test with a non-deterministic function returning a passively preserved element.
await testEngine(engine, {
if: [{ var: 'data' }, obj, 5]
}, {
data: true
}, obj, 'equal')
}
})
it('get operator w/ object key as string', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { get: [{ var: 'selected' }, 'b'] }, { selected: { b: 2 } }, 2)
})
it('get operator w/ object key as number', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { get: [{ var: 'selected' }, 1] }, { selected: [0, 2] }, 2)
})
it('get operator w/ object key as var', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { get: [{ var: 'selected' }, { var: 'key' }] }, { selected: { b: 2 }, key: 'b' }, 2)
})
it('is able to handle simple path escaping', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { get: [{ var: 'selected' }, 'b\\.c'] }, { selected: { 'b.c': 2 } }, 2)
})
it('is able to handle simple path escaping in a variable', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { get: [{ var: 'selected' }, { var: 'key' }] }, { selected: { 'b.c': 2 }, key: 'b\\.c' }, 2)
})
it('is able to avoid returning functions', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: 'toString' }, 'hello', null)
})
it('is able to return functions if enabled', async () => {
try {
for (const engine of [...normalEngines, ...permissiveEngines]) {
engine.allowFunctions = true
engine.addMethod('typeof', ([value]) => typeof value)
await testEngine(engine, { typeof: { var: 'toString' } }, {}, 'function')
}
} finally {
for (const engine of [...normalEngines, ...permissiveEngines]) {
engine.allowFunctions = false
delete engine.methods.typeof
}
}
})
it('is able to handle max with 1 element', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { max: 5 }, {}, 5)
})
it('is able to handle path escaping in a var call', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: 'hello\\.world' }, { 'hello.world': 2 }, 2)
})
it('is able to access empty keys', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: '.' }, { '': 2 }, 2)
})
it('is able to access dot keys', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: '\\.' }, { '.': 2 }, 2)
})
it('is able to access "/" keys from above', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { map: [[1], { '+': [{ var: '' }, { var: '../../..\\/' }] }] }, { '': { '': { '/': 3 } } }, [4])
})
it('is able to handle path escaping with multiple escapes', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: '\\foo' }, { '\\foo': 2 }, 2)
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: '\\\\foo' }, { '\\foo': 2 }, 2)
})
it('is able to access the index in the iterators', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { map: [[0, 1, 2, 3], { '+': [{ var: '' }, { var: '../index' }] }] }, {}, [0, 2, 4, 6])
})
it('is able to use pipe', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { pipe: ['Austin', { cat: ['Hello, ', { var: '' }, '!'] }] }, {}, 'Hello, Austin!')
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { pipe: [{ var: 'name' }, { cat: ['Hello, ', { var: '' }, '!'] }] }, { name: 'Austin' }, 'Hello, Austin!')
for (const engine of [normalEngines[1], normalEngines[3], permissiveEngines[1], permissiveEngines[3]]) {
engine.addMethod('as1', async ([n]) => n + 1, { async: true, deterministic: true })
await testEngine(engine, {
pipe: [
'Austin',
{ cat: ['Hello, ', { var: '' }, '!'] },
{ cat: [{ var: '' }, ' ', { as1: 5 }] }
]
}, { name: 'Austin' }, 'Hello, Austin! 6')
}
})
it('is able to reach far up in the traversal', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { map: [[1, 2, 3], { map: [[1], { var: '../../../../name' }] }] }, { name: 'Bob' }, [['Bob'], ['Bob'], ['Bob']])
})
it('correctly short circuits (or)', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) {
let count = 0
engine.addMethod('increment', {
method: () => {
count++
return true
}
})
await testEngine(engine, { or: [{ increment: true }, { increment: true }, { increment: true }] }, {}, true)
// It's called twice because it runs once for interpreted, and once for compiled.
assert.strictEqual(count, 2, 'Should have only called increment twice. (Count)')
}
})
it('correctly short circuits (and)', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) {
let count = 0
engine.addMethod('increment', {
method: () => {
count++
return false
}
})
await testEngine(engine, { and: [{ increment: true }, { increment: true }, { increment: true }] }, {}, false)
// It's called twice because it runs once for interpreted, and once for compiled.
assert.strictEqual(count, 2, 'Should have only called increment twice. (Count)')
}
})
it('does not execute any logic from and / or array if the argument is executed', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) {
let count = 0
engine.addMethod('increment', {
method: () => {
count++
return true
}
})
await testEngine(engine, { and: { preserve: [{ increment: true }, { increment: true }] } }, {}, { increment: true })
assert.strictEqual(count, 0, 'Should not have called increment on and.')
await testEngine(engine, { or: { preserve: [{ increment: true }, { increment: true }] } }, {}, { increment: true })
assert.strictEqual(count, 0, 'Should not have called increment on or.')
}
})
it('disables interpreted optimization when it realizes it will not be faster', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) {
const disableInterpretedOptimization = engine.disableInterpretedOptimization
for (let i = 0; i < 10e3; i++) await engine.run({ '+': [1, 2] }, {})
assert.strictEqual(engine.disableInterpretedOptimization, true)
// Resets back to the original value.
engine.disableInterpretedOptimization = disableInterpretedOptimization
}
})
// This is not fully supported right now.
it('is able to access the source array in the iterators', async () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { map: [[0, 1, 2, 3], { '+': [{ var: '' }, { var: '../iterator.2' }] }] }, {}, [2, 3, 4, 5])
})
it('should be able to handle various instances of "in" with good, bad and invalid data', async () => {
const rule = {
in: ['Spring', { var: 'city' }]
}
const goodData = { city: 'Springfield' }
const badData = { city: 'test' }
const invalidData = { city: null }
for (const engine of normalEngines) await testEngine(engine, rule, goodData, true)
for (const engine of normalEngines) await testEngine(engine, rule, badData, false)
for (const engine of normalEngines) await testEngine(engine, rule, invalidData, false)
})
})