|
| 1 | + |
| 2 | +import assert from 'assert' |
| 3 | +import { LogicEngine, AsyncLogicEngine } from './index.js' |
| 4 | + |
| 5 | +const normalEngines = [ |
| 6 | + new LogicEngine(), |
| 7 | + new AsyncLogicEngine(), |
| 8 | + new LogicEngine(undefined, { yieldSupported: true }), |
| 9 | + new AsyncLogicEngine(undefined, { yieldSupported: true }) |
| 10 | +] |
| 11 | + |
| 12 | +const permissiveEngines = [ |
| 13 | + new LogicEngine(undefined, { permissive: true }), |
| 14 | + new AsyncLogicEngine(undefined, { permissive: true }), |
| 15 | + new LogicEngine(undefined, { yieldSupported: true, permissive: true }), |
| 16 | + new AsyncLogicEngine(undefined, { yieldSupported: true, permissive: true }) |
| 17 | +] |
| 18 | + |
| 19 | +async function testEngineAsync (engine, rule, data, expected, matcher = 'deepStrictEqual') { |
| 20 | + // run |
| 21 | + if (expected === Error) { |
| 22 | + try { |
| 23 | + await engine.run(rule, data) |
| 24 | + throw new Error('Should have failed') |
| 25 | + } catch (e) {} |
| 26 | + } else { |
| 27 | + const result = await engine.run(rule, data) |
| 28 | + assert[matcher](result, expected) |
| 29 | + } |
| 30 | + |
| 31 | + // build |
| 32 | + if (expected === Error) { |
| 33 | + try { |
| 34 | + const built = await engine.build(rule) |
| 35 | + await built(data) |
| 36 | + throw new Error('Should have failed') |
| 37 | + } catch (e) {} |
| 38 | + } else { |
| 39 | + const built = await engine.build(rule) |
| 40 | + const builtResult = await built(data) |
| 41 | + assert[matcher](builtResult, expected) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function testEngine (engine, rule, data, expected, matcher = 'deepStrictEqual') { |
| 46 | + if (engine instanceof AsyncLogicEngine) { |
| 47 | + return testEngineAsync(engine, rule, data, expected, matcher) |
| 48 | + } |
| 49 | + |
| 50 | + // run |
| 51 | + if (expected === Error) { |
| 52 | + try { |
| 53 | + engine.run(rule, data) |
| 54 | + throw new Error('Should have failed') |
| 55 | + } catch (e) {} |
| 56 | + } else { |
| 57 | + const result = engine.run(rule, data) |
| 58 | + assert[matcher](result, expected) |
| 59 | + } |
| 60 | + |
| 61 | + // build |
| 62 | + if (expected === Error) { |
| 63 | + try { |
| 64 | + const built = engine.build(rule) |
| 65 | + built(data) |
| 66 | + throw new Error('Should have failed') |
| 67 | + } catch (e) {} |
| 68 | + } else { |
| 69 | + const built = engine.build(rule) |
| 70 | + const builtResult = built(data) |
| 71 | + assert[matcher](builtResult, expected) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +describe('Various Test Cases', () => { |
| 76 | + it('Should fail when an unrecognized method is used.', async () => { |
| 77 | + for (const engine of normalEngines) await testEngine(engine, { unknown: true }, {}, Error) |
| 78 | + }) |
| 79 | + |
| 80 | + it('Should return the object when an unrecognized method is used.', async () => { |
| 81 | + for (const engine of permissiveEngines) { |
| 82 | + await testEngine(engine, { unknown: true }, {}, { unknown: true }) |
| 83 | + |
| 84 | + await testEngine(engine, { |
| 85 | + if: [true, { unknown: true, unknown2: 2 }, 5] |
| 86 | + }, {}, { unknown: true, unknown2: 2 }) |
| 87 | + |
| 88 | + const obj = { unknown: true, unknown2: 2 } |
| 89 | + |
| 90 | + // test with deterministic function returning a passively preserved element. |
| 91 | + await testEngine(engine, { |
| 92 | + if: [true, obj, 5] |
| 93 | + }, {}, obj, 'equal') |
| 94 | + |
| 95 | + // test with a non-deterministic function returning a passively preserved element. |
| 96 | + await testEngine(engine, { |
| 97 | + if: [{ var: 'data' }, obj, 5] |
| 98 | + }, { |
| 99 | + data: true |
| 100 | + }, obj, 'equal') |
| 101 | + } |
| 102 | + }) |
| 103 | +}) |
0 commit comments