forked from JSONPath-Plus/JSONPath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.eval.js
More file actions
94 lines (88 loc) · 3.01 KB
/
test.eval.js
File metadata and controls
94 lines (88 loc) · 3.01 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
'use strict';
describe('JSONPath - Eval', function () {
const json = {
"store": {
"book": {
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": [8.95, 8.94]
},
"books": [{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": [10.99, 12.29]
}, {
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": [8.99, 6.95]
}]
}
};
it('multi statement eval', () => {
const expected = [json.store.books[0]];
const selector = '$..[?(' +
'var sum = @.price && @.price[0]+@.price[1];' +
'sum > 20;)]';
const result = jsonpath({json, path: selector, wrap: false});
assert.deepEqual(expected, result);
});
it('accessing current path', () => {
const expected = [json.store.books[1]];
const result = jsonpath({json, path: "$..[?(@path==\"$['store']['books'][1]\")]", wrap: false});
assert.deepEqual(expected, result);
});
it('sandbox', () => {
const expected = [json.store.book];
const result = jsonpath({
json,
sandbox: {category: 'reference'},
path: "$..[?(@.category === category)]", wrap: false
});
assert.deepEqual(expected, result);
});
it('sandbox (with parsing function)', () => {
const expected = [json.store.book];
const result = jsonpath({
json,
sandbox: {
filter (arg) {
return arg.category === 'reference';
}
},
path: "$..[?(filter(@))]", wrap: false
});
assert.deepEqual(expected, result);
});
describe('cyclic object', () => {
// This is not an eval test, but we put it here for parity with item below
it('cyclic object without a sandbox', () => {
const circular = {a: {b: {c: 5}}};
circular.a.x = circular;
const expected = circular.a.b;
const result = jsonpath({
json: circular,
path: '$.a.b',
wrap: false
});
assert.deepEqual(expected, result);
});
it('cyclic object in a sandbox', () => {
const circular = {category: 'fiction'};
circular.recurse = circular;
const expected = json.store.books;
const result = jsonpath({
json,
path: '$..[?(@.category === aCircularReference.category)]',
sandbox: {
aCircularReference: circular
},
wrap: false
});
assert.deepEqual(expected, result);
});
});
});