-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap-set.js
More file actions
188 lines (158 loc) · 5.96 KB
/
map-set.js
File metadata and controls
188 lines (158 loc) · 5.96 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { assert } from 'chai';
import {
evaluate,
JSONPointerIndexError,
JSONPointerTypeError,
JSONPointerKeyError,
JSONPointerEvaluateError,
URIFragmentIdentifier,
} from '../../../src/index.js';
import MapSetEvaluationRealm from '../../../src/evaluate/realms/map-set.js';
describe('evaluate', function () {
const realm = new MapSetEvaluationRealm();
const data = new Map([
['foo', new Set(['bar', 'baz'])],
['', 0],
['a/b', 1],
['c%d', 2],
['e^f', 3],
['g|h', 4],
['i\\j', 5],
['k"l', 6],
[' ', 7],
['m~n', 8],
]);
context('RCC 6901 JSON String tests', function () {
const jsonStringRepEntries = [
['', data],
['/foo', new Set(['bar', 'baz'])],
['/foo/0', 'bar'],
['/', 0],
['/a~1b', 1],
['/c%d', 2],
['/e^f', 3],
['/g|h', 4],
['/i\\j', 5],
['/k"l', 6],
['/ ', 7],
['/m~0n', 8],
];
jsonStringRepEntries.forEach(([jsonString, expected]) => {
specify('should correctly evaluate JSON Pointer from JSON String', function () {
assert.deepEqual(evaluate(data, jsonString, { realm }), expected);
});
});
});
context('RCC 6901 JSON String tests', function () {
const fragmentRepEntries = [
['#', data],
['#/foo', new Set(['bar', 'baz'])],
['#/foo/0', 'bar'],
['#/', 0],
['#/a~1b', 1],
['#/c%25d', 2],
['#/e%5Ef', 3],
['#/g%7Ch', 4],
['#/i%5Cj', 5],
['#/k%22l', 6],
['#/%20', 7],
['#/m~0n', 8],
];
fragmentRepEntries.forEach(([fragment, expected]) => {
specify('should correctly evaluate JSON Pointer from URI Fragment Identifier', function () {
assert.deepEqual(evaluate(data, URIFragmentIdentifier.from(fragment), { realm }), expected);
});
});
});
context('valid JSON Pointers', function () {
specify('should return entire document for ""', function () {
assert.deepEqual(evaluate(data, '', { realm }), data);
});
specify('should return array ["bar", "baz"] for "/foo"', function () {
assert.deepEqual(evaluate(data, '/foo', { realm }), new Set(['bar', 'baz']));
});
specify('should return "bar" for "/foo/0"', function () {
assert.strictEqual(evaluate(data, '/foo/0', { realm }), 'bar');
});
specify('should return 0 for "/"', function () {
assert.strictEqual(evaluate(data, '/', { realm }), 0);
});
specify('should return 1 for "/a~1b"', function () {
assert.strictEqual(evaluate(data, '/a~1b', { realm }), 1);
});
specify('should return 2 for "/c%d"', function () {
assert.strictEqual(evaluate(data, '/c%d', { realm }), 2);
});
specify('should return 3 for "/e^f"', function () {
assert.strictEqual(evaluate(data, '/e^f', { realm }), 3);
});
specify('should return 4 for "/g|h"', function () {
assert.strictEqual(evaluate(data, '/g|h', { realm }), 4);
});
specify('should return 5 for "/i\\j"', function () {
assert.strictEqual(evaluate(data, '/i\\j', { realm }), 5);
});
specify('should return 6 for "/k\"l"', function () {
assert.strictEqual(evaluate(data, '/k"l', { realm }), 6);
});
specify('should return 7 for "/ "', function () {
assert.strictEqual(evaluate(data, '/ ', { realm }), 7);
});
specify('should return 8 for "/m~0n"', function () {
assert.strictEqual(evaluate(data, '/m~0n', { realm }), 8);
});
});
context('invalid JSON Pointers (should throw errors)', function () {
specify('should throw JSONPointerEvaluateError for invalid JSON Pointer', function () {
assert.throws(() => evaluate(data, 'invalid-pointer', { realm }), JSONPointerEvaluateError);
});
specify(
'should throw JSONPointerTypeError for accessing property on non-object/array',
function () {
assert.throws(() => evaluate(data, '/foo/0/bad', { realm }), JSONPointerTypeError);
},
);
specify('should throw JSONPointerKeyError for non-existing key', function () {
assert.throws(() => evaluate(data, '/nonexistent', { realm }), JSONPointerKeyError);
});
specify('should throw JSONPointerIndexError for non-numeric array index', function () {
assert.throws(() => evaluate(data, '/foo/x', { realm }), JSONPointerIndexError);
});
specify('should throw JSONPointerIndexError for out-of-bounds array index', function () {
assert.throws(() => evaluate(data, '/foo/5', { realm }), JSONPointerIndexError);
});
specify('should throw JSONPointerIndexError for leading zero in array index', function () {
assert.throws(() => evaluate(data, '/foo/01', { realm }), JSONPointerIndexError);
});
specify('should throw JSONPointerIndexError for "-" when strictArrays is true', function () {
assert.throws(
() => evaluate(data, '/foo/-', { strictArrays: true, realm }),
JSONPointerIndexError,
);
});
specify('should return undefined for "-" when strictArrays is false', function () {
assert.strictEqual(evaluate(data, '/foo/-', { strictArrays: false, realm }), undefined);
});
specify(
'should throw JSONPointerKeyError for accessing chain of object properties that do not exist',
function () {
assert.throws(() => evaluate(data, '/missing/key', { realm }), JSONPointerKeyError);
},
);
specify(
'should return undefined accessing object property that does not exist when strictObject is false',
function () {
assert.isUndefined(evaluate(data, '/missing', { strictObjects: false, realm }));
},
);
specify('should throw JSONPointerTypeError when evaluating on primitive', function () {
assert.throws(() => evaluate('not-an-object', '/foo', { realm }), JSONPointerTypeError);
});
specify(
'should throw JSONPointerTypeError when trying to access deep path on primitive',
function () {
assert.throws(() => evaluate({ foo: 42 }, '/foo/bar', { realm }), JSONPointerTypeError);
},
);
});
});