|
| 1 | +import { assert } from 'chai'; |
| 2 | + |
| 3 | +import { evaluate, composeRealms, JSONPointerEvaluateError } from '../../src/index.js'; |
| 4 | +import JSONEvaluationRealm from '../../src/evaluate/realms/json.js'; |
| 5 | +import MapSetEvaluationRealm from '../../src/evaluate/realms/map-set.js'; |
| 6 | + |
| 7 | +describe('evaluate', function () { |
| 8 | + context('composeRealms', function () { |
| 9 | + specify('should compose realms', function () { |
| 10 | + const compositeRealm = composeRealms(new MapSetEvaluationRealm(), new JSONEvaluationRealm()); |
| 11 | + const structure = [ |
| 12 | + { |
| 13 | + a: new Map([['b', new Set(['c', 'd'])]]), |
| 14 | + }, |
| 15 | + ]; |
| 16 | + const actual = evaluate(structure, '/0/a/b/1', { realm: compositeRealm }); |
| 17 | + const expected = 'd'; |
| 18 | + |
| 19 | + assert.strictEqual(actual, expected); |
| 20 | + }); |
| 21 | + |
| 22 | + specify('should throw on empty composition', function () { |
| 23 | + const compositeRealm = composeRealms(); |
| 24 | + const structure = [ |
| 25 | + { |
| 26 | + a: new Map([['b', new Set(['c', 'd'])]]), |
| 27 | + }, |
| 28 | + ]; |
| 29 | + |
| 30 | + assert.throws( |
| 31 | + () => evaluate(structure, '/0/a/b/1', { realm: compositeRealm }), |
| 32 | + JSONPointerEvaluateError, |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + specify('should throw on invalid realm', function () { |
| 37 | + const compositeRealm = composeRealms({}); |
| 38 | + const structure = [ |
| 39 | + { |
| 40 | + a: new Map([['b', new Set(['c', 'd'])]]), |
| 41 | + }, |
| 42 | + ]; |
| 43 | + |
| 44 | + try { |
| 45 | + evaluate(structure, '/0/a/b/1', { realm: compositeRealm }); |
| 46 | + assert.fail('Expected an error to be thrown'); |
| 47 | + } catch (error) { |
| 48 | + assert.instanceOf(error, JSONPointerEvaluateError); |
| 49 | + assert.instanceOf(error.cause, TypeError); |
| 50 | + } |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
0 commit comments