|
| 1 | +import { assert } from 'chai'; |
| 2 | + |
| 3 | +import { |
| 4 | + evaluate, |
| 5 | + JSONPointerIndexError, |
| 6 | + JSONPointerTypeError, |
| 7 | + JSONPointerKeyError, |
| 8 | + JSONPointerEvaluateError, |
| 9 | + referenceTokenListEvaluator, |
| 10 | +} from '../src/index.js'; |
| 11 | +import unescape from '../src/unescape.js'; |
| 12 | + |
| 13 | +describe('evaluate', function () { |
| 14 | + const data = { |
| 15 | + foo: ['bar', 'baz'], |
| 16 | + '': 0, |
| 17 | + 'a/b': 1, |
| 18 | + 'c%d': 2, |
| 19 | + 'e^f': 3, |
| 20 | + 'g|h': 4, |
| 21 | + 'i\\j': 5, |
| 22 | + 'k"l': 6, |
| 23 | + ' ': 7, |
| 24 | + 'm~n': 8, |
| 25 | + }; |
| 26 | + |
| 27 | + context('valid JSON Pointers', function () { |
| 28 | + specify('should return entire document for ""', function () { |
| 29 | + assert.deepEqual(evaluate(data, ''), data); |
| 30 | + }); |
| 31 | + |
| 32 | + specify('should return array ["bar", "baz"] for "/foo"', function () { |
| 33 | + assert.deepEqual(evaluate(data, '/foo'), ['bar', 'baz']); |
| 34 | + }); |
| 35 | + |
| 36 | + specify('should return "bar" for "/foo/0"', function () { |
| 37 | + assert.strictEqual(evaluate(data, '/foo/0'), 'bar'); |
| 38 | + }); |
| 39 | + |
| 40 | + specify('should return 0 for "/"', function () { |
| 41 | + assert.strictEqual(evaluate(data, '/'), 0); |
| 42 | + }); |
| 43 | + |
| 44 | + specify('should return 1 for "/a~1b"', function () { |
| 45 | + assert.strictEqual(evaluate(data, '/a~1b'), 1); |
| 46 | + }); |
| 47 | + |
| 48 | + specify('should return 2 for "/c%d"', function () { |
| 49 | + assert.strictEqual(evaluate(data, '/c%d'), 2); |
| 50 | + }); |
| 51 | + |
| 52 | + specify('should return 3 for "/e^f"', function () { |
| 53 | + assert.strictEqual(evaluate(data, '/e^f'), 3); |
| 54 | + }); |
| 55 | + |
| 56 | + specify('should return 4 for "/g|h"', function () { |
| 57 | + assert.strictEqual(evaluate(data, '/g|h'), 4); |
| 58 | + }); |
| 59 | + |
| 60 | + specify('should return 5 for "/i\\j"', function () { |
| 61 | + assert.strictEqual(evaluate(data, '/i\\j'), 5); |
| 62 | + }); |
| 63 | + |
| 64 | + specify('should return 6 for "/k\"l"', function () { |
| 65 | + assert.strictEqual(evaluate(data, '/k"l'), 6); |
| 66 | + }); |
| 67 | + |
| 68 | + specify('should return 7 for "/ "', function () { |
| 69 | + assert.strictEqual(evaluate(data, '/ '), 7); |
| 70 | + }); |
| 71 | + |
| 72 | + specify('should return 8 for "/m~0n"', function () { |
| 73 | + assert.strictEqual(evaluate(data, '/m~0n'), 8); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + context('custom evaluator option', function () { |
| 78 | + specify('should correctly use a default evaluator', function () { |
| 79 | + const result = evaluate(data, '/a~1b', { evaluator: referenceTokenListEvaluator }); |
| 80 | + |
| 81 | + assert.deepEqual(result, 1); // Evaluator should return unescaped reference token list |
| 82 | + }); |
| 83 | + |
| 84 | + specify('should use a custom evaluator', function () { |
| 85 | + const evaluator = (ast) => { |
| 86 | + const parts = []; |
| 87 | + |
| 88 | + ast.translate(parts); |
| 89 | + |
| 90 | + return parts.filter(([type]) => type === 'reference-token').map(([, value]) => value); |
| 91 | + }; |
| 92 | + |
| 93 | + assert.throws(() => evaluate(data, '/a~1b', { evaluator }), JSONPointerKeyError); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + context('invalid JSON Pointers (should throw errors)', function () { |
| 98 | + specify('should throw JSONPointerEvaluateError for invalid JSON Pointer', function () { |
| 99 | + assert.throws(() => evaluate(data, 'invalid-pointer'), JSONPointerEvaluateError); |
| 100 | + }); |
| 101 | + |
| 102 | + specify( |
| 103 | + 'should throw JSONPointerTypeError for accessing property on non-object/array', |
| 104 | + function () { |
| 105 | + assert.throws(() => evaluate(data, '/foo/0/bad'), JSONPointerTypeError); |
| 106 | + }, |
| 107 | + ); |
| 108 | + |
| 109 | + specify('should throw JSONPointerKeyError for non-existing key', function () { |
| 110 | + assert.throws(() => evaluate(data, '/nonexistent'), JSONPointerKeyError); |
| 111 | + }); |
| 112 | + |
| 113 | + specify('should throw JSONPointerIndexError for non-numeric array index', function () { |
| 114 | + assert.throws(() => evaluate(data, '/foo/x'), JSONPointerIndexError); |
| 115 | + }); |
| 116 | + |
| 117 | + specify('should throw JSONPointerIndexError for out-of-bounds array index', function () { |
| 118 | + assert.throws(() => evaluate(data, '/foo/5'), JSONPointerIndexError); |
| 119 | + }); |
| 120 | + |
| 121 | + specify('should throw JSONPointerIndexError for leading zero in array index', function () { |
| 122 | + assert.throws(() => evaluate(data, '/foo/01'), JSONPointerIndexError); |
| 123 | + }); |
| 124 | + |
| 125 | + specify('should throw JSONPointerIndexError for "-" when strictArrays is true', function () { |
| 126 | + assert.throws(() => evaluate(data, '/foo/-', { strictArrays: true }), JSONPointerIndexError); |
| 127 | + }); |
| 128 | + |
| 129 | + specify('should return undefined for "-" when strictArrays is false', function () { |
| 130 | + assert.strictEqual(evaluate(data, '/foo/-', { strictArrays: false }), undefined); |
| 131 | + }); |
| 132 | + |
| 133 | + specify( |
| 134 | + 'should throw JSONPointerKeyError for accessing object property that does not exist', |
| 135 | + function () { |
| 136 | + assert.throws(() => evaluate(data, '/missing/key'), JSONPointerKeyError); |
| 137 | + }, |
| 138 | + ); |
| 139 | + |
| 140 | + specify('should throw JSONPointerTypeError when evaluating on primitive', function () { |
| 141 | + assert.throws(() => evaluate('not-an-object', '/foo'), JSONPointerTypeError); |
| 142 | + }); |
| 143 | + |
| 144 | + specify( |
| 145 | + 'should throw JSONPointerTypeError when trying to access deep path on primitive', |
| 146 | + function () { |
| 147 | + assert.throws(() => evaluate({ foo: 42 }, '/foo/bar'), JSONPointerTypeError); |
| 148 | + }, |
| 149 | + ); |
| 150 | + }); |
| 151 | +}); |
0 commit comments