|
| 1 | +import { assert } from 'chai'; |
| 2 | + |
| 3 | +import { |
| 4 | + evaluate, |
| 5 | + JSONPointerIndexError, |
| 6 | + JSONPointerTypeError, |
| 7 | + JSONPointerKeyError, |
| 8 | + JSONPointerEvaluateError, |
| 9 | + URIFragmentIdentifier, |
| 10 | +} from '../../../src/index.js'; |
| 11 | +import MapSetEvaluationRealm from '../../../src/evaluate/realms/map-set.js'; |
| 12 | + |
| 13 | +describe('evaluate', function () { |
| 14 | + const realm = new MapSetEvaluationRealm(); |
| 15 | + const data = new Map([ |
| 16 | + ['foo', new Set(['bar', 'baz'])], |
| 17 | + ['', 0], |
| 18 | + ['a/b', 1], |
| 19 | + ['c%d', 2], |
| 20 | + ['e^f', 3], |
| 21 | + ['g|h', 4], |
| 22 | + ['i\\j', 5], |
| 23 | + ['k"l', 6], |
| 24 | + [' ', 7], |
| 25 | + ['m~n', 8], |
| 26 | + ]); |
| 27 | + |
| 28 | + context('RFC 6901 JSON String tests', function () { |
| 29 | + const jsonStringRepEntries = [ |
| 30 | + ['', data], |
| 31 | + ['/foo', new Set(['bar', 'baz'])], |
| 32 | + ['/foo/0', 'bar'], |
| 33 | + ['/', 0], |
| 34 | + ['/a~1b', 1], |
| 35 | + ['/c%d', 2], |
| 36 | + ['/e^f', 3], |
| 37 | + ['/g|h', 4], |
| 38 | + ['/i\\j', 5], |
| 39 | + ['/k"l', 6], |
| 40 | + ['/ ', 7], |
| 41 | + ['/m~0n', 8], |
| 42 | + ]; |
| 43 | + |
| 44 | + jsonStringRepEntries.forEach(([jsonString, expected]) => { |
| 45 | + specify('should correctly evaluate JSON Pointer from JSON String', function () { |
| 46 | + assert.deepEqual(evaluate(data, jsonString, { realm }), expected); |
| 47 | + }); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + context('RFC 6901 URI Fragment Identifier tests', function () { |
| 52 | + const fragmentRepEntries = [ |
| 53 | + ['#', data], |
| 54 | + ['#/foo', new Set(['bar', 'baz'])], |
| 55 | + ['#/foo/0', 'bar'], |
| 56 | + ['#/', 0], |
| 57 | + ['#/a~1b', 1], |
| 58 | + ['#/c%25d', 2], |
| 59 | + ['#/e%5Ef', 3], |
| 60 | + ['#/g%7Ch', 4], |
| 61 | + ['#/i%5Cj', 5], |
| 62 | + ['#/k%22l', 6], |
| 63 | + ['#/%20', 7], |
| 64 | + ['#/m~0n', 8], |
| 65 | + ]; |
| 66 | + |
| 67 | + fragmentRepEntries.forEach(([fragment, expected]) => { |
| 68 | + specify('should correctly evaluate JSON Pointer from URI Fragment Identifier', function () { |
| 69 | + assert.deepEqual(evaluate(data, URIFragmentIdentifier.from(fragment), { realm }), expected); |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + context('valid JSON Pointers', function () { |
| 75 | + specify('should return entire document for ""', function () { |
| 76 | + assert.deepEqual(evaluate(data, '', { realm }), data); |
| 77 | + }); |
| 78 | + |
| 79 | + specify('should return Set(["bar", "baz"]) for "/foo"', function () { |
| 80 | + assert.deepEqual(evaluate(data, '/foo', { realm }), new Set(['bar', 'baz'])); |
| 81 | + }); |
| 82 | + |
| 83 | + specify('should return "bar" for "/foo/0"', function () { |
| 84 | + assert.strictEqual(evaluate(data, '/foo/0', { realm }), 'bar'); |
| 85 | + }); |
| 86 | + |
| 87 | + specify('should return 0 for "/"', function () { |
| 88 | + assert.strictEqual(evaluate(data, '/', { realm }), 0); |
| 89 | + }); |
| 90 | + |
| 91 | + specify('should return 1 for "/a~1b"', function () { |
| 92 | + assert.strictEqual(evaluate(data, '/a~1b', { realm }), 1); |
| 93 | + }); |
| 94 | + |
| 95 | + specify('should return 2 for "/c%d"', function () { |
| 96 | + assert.strictEqual(evaluate(data, '/c%d', { realm }), 2); |
| 97 | + }); |
| 98 | + |
| 99 | + specify('should return 3 for "/e^f"', function () { |
| 100 | + assert.strictEqual(evaluate(data, '/e^f', { realm }), 3); |
| 101 | + }); |
| 102 | + |
| 103 | + specify('should return 4 for "/g|h"', function () { |
| 104 | + assert.strictEqual(evaluate(data, '/g|h', { realm }), 4); |
| 105 | + }); |
| 106 | + |
| 107 | + specify('should return 5 for "/i\\j"', function () { |
| 108 | + assert.strictEqual(evaluate(data, '/i\\j', { realm }), 5); |
| 109 | + }); |
| 110 | + |
| 111 | + specify('should return 6 for "/k\"l"', function () { |
| 112 | + assert.strictEqual(evaluate(data, '/k"l', { realm }), 6); |
| 113 | + }); |
| 114 | + |
| 115 | + specify('should return 7 for "/ "', function () { |
| 116 | + assert.strictEqual(evaluate(data, '/ ', { realm }), 7); |
| 117 | + }); |
| 118 | + |
| 119 | + specify('should return 8 for "/m~0n"', function () { |
| 120 | + assert.strictEqual(evaluate(data, '/m~0n', { realm }), 8); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + context('invalid JSON Pointers (should throw errors)', function () { |
| 125 | + specify('should throw JSONPointerEvaluateError for invalid JSON Pointer', function () { |
| 126 | + assert.throws(() => evaluate(data, 'invalid-pointer', { realm }), JSONPointerEvaluateError); |
| 127 | + }); |
| 128 | + |
| 129 | + specify( |
| 130 | + 'should throw JSONPointerTypeError for accessing property on non-object/array', |
| 131 | + function () { |
| 132 | + assert.throws(() => evaluate(data, '/foo/0/bad', { realm }), JSONPointerTypeError); |
| 133 | + }, |
| 134 | + ); |
| 135 | + |
| 136 | + specify('should throw JSONPointerKeyError for non-existing key', function () { |
| 137 | + assert.throws(() => evaluate(data, '/nonexistent', { realm }), JSONPointerKeyError); |
| 138 | + }); |
| 139 | + |
| 140 | + specify('should throw JSONPointerIndexError for non-numeric array index', function () { |
| 141 | + assert.throws(() => evaluate(data, '/foo/x', { realm }), JSONPointerIndexError); |
| 142 | + }); |
| 143 | + |
| 144 | + specify('should throw JSONPointerIndexError for out-of-bounds array index', function () { |
| 145 | + assert.throws(() => evaluate(data, '/foo/5', { realm }), JSONPointerIndexError); |
| 146 | + }); |
| 147 | + |
| 148 | + specify('should throw JSONPointerIndexError for leading zero in array index', function () { |
| 149 | + assert.throws(() => evaluate(data, '/foo/01', { realm }), JSONPointerIndexError); |
| 150 | + }); |
| 151 | + |
| 152 | + specify('should throw JSONPointerIndexError for "-" when strictArrays is true', function () { |
| 153 | + assert.throws( |
| 154 | + () => evaluate(data, '/foo/-', { strictArrays: true, realm }), |
| 155 | + JSONPointerIndexError, |
| 156 | + ); |
| 157 | + }); |
| 158 | + |
| 159 | + specify('should return undefined for "-" when strictArrays is false', function () { |
| 160 | + assert.strictEqual(evaluate(data, '/foo/-', { strictArrays: false, realm }), undefined); |
| 161 | + }); |
| 162 | + |
| 163 | + specify( |
| 164 | + 'should throw JSONPointerKeyError for accessing chain of object properties that do not exist', |
| 165 | + function () { |
| 166 | + assert.throws(() => evaluate(data, '/missing/key', { realm }), JSONPointerKeyError); |
| 167 | + }, |
| 168 | + ); |
| 169 | + |
| 170 | + specify( |
| 171 | + 'should return undefined accessing object property that does not exist when strictObject is false', |
| 172 | + function () { |
| 173 | + assert.isUndefined(evaluate(data, '/missing', { strictObjects: false, realm })); |
| 174 | + }, |
| 175 | + ); |
| 176 | + |
| 177 | + specify('should throw JSONPointerTypeError when evaluating on primitive', function () { |
| 178 | + assert.throws(() => evaluate('not-an-object', '/foo', { realm }), JSONPointerTypeError); |
| 179 | + }); |
| 180 | + |
| 181 | + specify( |
| 182 | + 'should throw JSONPointerTypeError when trying to access deep path on primitive', |
| 183 | + function () { |
| 184 | + assert.throws(() => evaluate({ foo: 42 }, '/foo/bar', { realm }), JSONPointerTypeError); |
| 185 | + }, |
| 186 | + ); |
| 187 | + }); |
| 188 | +}); |
0 commit comments