-
Notifications
You must be signed in to change notification settings - Fork 9
/
general.test.js
151 lines (126 loc) · 5.38 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
import assert from 'assert'
import { LogicEngine, AsyncLogicEngine } from './index.js'
const normalEngines = [
new LogicEngine(),
new AsyncLogicEngine(),
new LogicEngine(undefined, { yieldSupported: true }),
new AsyncLogicEngine(undefined, { yieldSupported: true })
]
const permissiveEngines = [
new LogicEngine(undefined, { permissive: true }),
new AsyncLogicEngine(undefined, { permissive: true }),
new LogicEngine(undefined, { yieldSupported: true, permissive: true }),
new AsyncLogicEngine(undefined, { yieldSupported: 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 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 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 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('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)
})
})