-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathtest.api.js
More file actions
325 lines (301 loc) · 10.6 KB
/
Copy pathtest.api.js
File metadata and controls
325 lines (301 loc) · 10.6 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* @import {JSONPathClass} from '../src/jsonpath.js';
*/
describe('JSONPath - API', function () {
// tests based on examples at https://goessner.net/articles/jsonpath/
const json = {
"store": {
"book": [{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}],
"bicycle": {
"color": "red",
"price": 19.95
}
}
};
it('should test non-object argument of constructor', () => {
const books = json.store.book;
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
let result = jsonpath('$.store.book[*].author', json);
assert.deepEqual(result, expected);
result = jsonpath({json, path: 'store.book[*].author'});
assert.deepEqual(result, expected);
});
it('should test array path of constructor', () => {
const books = json.store.book;
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
let result = jsonpath({path: ['$', 'store', 'book', '*', 'author'], json});
assert.deepEqual(result, expected);
result = jsonpath({json, path: 'store.book[*].author'});
assert.deepEqual(result, expected);
});
it('should test defaults on manual `evaluate` with `autostart: false`', () => {
const books = json.store.book;
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
let jp = jsonpath({
path: '$.store.book[*].author',
json,
autostart: false
});
let result = jp.evaluate();
assert.deepEqual(result, expected);
jp = jsonpath({
json,
path: 'store.book[*].author',
autostart: false
});
result = jp.evaluate();
assert.deepEqual(result, expected);
});
it('should test defaults with `evaluate` object and `autostart: false`', () => {
const books = json.store.book;
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
const jp = jsonpath({
autostart: false
});
const result = jp.evaluate({
json,
path: '$.store.book[*].author',
sandbox: {category: 'reference'},
eval: false,
flatten: true,
wrap: false,
resultType: 'value',
callback () { /* */ },
parent: null,
parentProperty: null,
otherTypeCallback () {
return true;
}
});
assert.deepEqual(result, expected);
});
it('should handle _handleCallback with undefined callback', () => {
const jp = jsonpath({
autostart: false
});
// Test the defensive check in _handleCallback when callback is undefined
const retObj = {
path: "$['store']['book'][0]",
value: json.store.book[0],
parent: json.store.book,
parentProperty: 0
};
// This should not throw and should return early
assert.doesNotThrow(() => {
jp._handleCallback(retObj, undefined, 'value');
});
});
it('should handle _getPreferredOutput with string path and all resultType', () => {
const jp = jsonpath({
autostart: false
});
jp.currResultType = 'all';
const retObj = {
path: "$['store']['book'][0]",
value: json.store.book[0],
parent: json.store.book,
parentProperty: 0
};
const result = /** @type {{path: string, pointer: string}} */ (
jp._getPreferredOutput(retObj)
);
assert.deepEqual(result.path, "$['store']['book'][0]");
assert.deepEqual(result.pointer, '/store/book/0');
});
it('should return string path directly in _getPreferredOutput for path resultType', () => {
const jp = jsonpath({
autostart: false
});
jp.currResultType = 'path';
const retObj = {
path: "$['store']['book'][0]",
value: json.store.book[0],
parent: json.store.book,
parentProperty: 0
};
const result = /** @type {string} */ (jp._getPreferredOutput(retObj));
assert.deepEqual(result, "$['store']['book'][0]");
});
it('should convert string path to array for pointer resultType', () => {
const jp = jsonpath({
autostart: false
});
jp.currResultType = 'pointer';
const retObj = {
path: "$['store']['book'][0]",
value: json.store.book[0],
parent: json.store.book,
parentProperty: 0
};
const result = /** @type {string} */ (jp._getPreferredOutput(retObj));
assert.deepEqual(result, '/store/book/0');
});
it('should handle flatten: null in evaluate options', () => {
const jp = jsonpath({
autostart: false
});
const result = jp.evaluate({
json,
path: '$.store.book[*].author',
flatten: undefined
});
const books = json.store.book;
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
assert.deepEqual(result, expected);
});
it('should preserve explicit null overrides in evaluate options', () => {
const jp = jsonpath({
autostart: false,
parent: {sentinel: true},
parentProperty: 'sentinel'
});
const result = /** @type {{parent: unknown, parentProperty: unknown, value: unknown}} */ (
jp.evaluate({
json,
path: '$',
parent: null,
parentProperty: null,
wrap: false,
resultType: 'all'
})
);
assert.deepEqual(result.parent, null);
assert.deepEqual(result.parentProperty, null);
assert.deepEqual(result.value, json);
});
it('should handle nested filter with single result', () => {
const testJson = {
items: [
{id: 1, nested: {value: 'a'}},
{id: 2, nested: {value: 'b'}}
]
};
const result = jsonpath({
json: testJson,
path: '$.items[?(@.nested)]'
});
assert.deepEqual(result, [testJson.items[0], testJson.items[1]]);
});
it('should handle slice with single element', () => {
const testJson = {items: [1]};
const result = jsonpath({
json: testJson,
path: '$.items[0:1]'
});
assert.deepEqual(result, [1]);
});
it('should handle simple root path returning single object', () => {
const testJson = {value: 42};
const result = jsonpath({
json: testJson,
path: '$',
wrap: false,
resultType: 'all'
});
assert.deepEqual(result, {
path: '$',
value: testJson,
parent: null,
parentProperty: null,
hasArrExpr: undefined,
pointer: ''
});
});
it('should handle otherTypeCallback returning null for @other()', () => {
const testJson = {
values: [1, 'text', true, null, {obj: 'value'}]
};
const result = jsonpath({
json: testJson,
path: '$.values[*]@other()',
otherTypeCallback (val) {
// Return null for objects to test the ?? false fallback
if (typeof val === 'object' && val !== null) {
return null;
}
return false;
}
});
assert.deepEqual(/** @type {unknown} */ (result), []);
});
it('should handle nested filter with property access', () => {
const testJson = {
items: [
{id: 1, subs: [{val: 1}, {val: 2}]},
{id: 2, subs: [{val: 3}]},
{id: 3, subs: []}
]
};
// Nested filter: select items that have subs with val > 1
const result = jsonpath({
json: testJson,
path: '$.items[?(@.subs[?(@.val > 1)])]'
});
// Items 0 and 1 both have subs with val > 1
assert.deepEqual(result, [testJson.items[0], testJson.items[1]]);
});
it('should handle slice operation with multiple elements', () => {
const testJson = {items: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]};
const result = jsonpath({
json: testJson,
path: '$.items[2:8:2]'
});
assert.deepEqual(result, [3, 5, 7]);
});
it('should handle parenthesis expression with undefined result', () => {
const testJson = {a: {x: 1}, b: {y: 2}, c: {z: undefined}};
const result = jsonpath({
json: testJson,
path: '$[(undefined)]'
});
assert.deepEqual(result, []);
});
// Behavior change: an `undefined` eval result no longer falls through to
// the literal `"undefined"` property (which previously also threw for
// some inputs); it now matches nothing.
it('should not match the literal "undefined" key for an undefined parenthesis expression', () => {
const testJson = {undefined: 'X', a: 'A'};
assert.deepEqual(jsonpath({
json: testJson,
path: '$[(undefined)]'
}), []);
assert.deepEqual(jsonpath({
json: testJson,
path: "$['undefined']"
}), ['X']);
});
it('should handle @path expression without predefined sandbox', () => {
const testJson = {a: 1, b: 2};
// Don't provide sandbox, let it be created on demand
const result = jsonpath({
json: testJson,
path: '$[?(@path == "$[\'a\']")]'
});
assert.deepEqual(result, [1]);
});
});