-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathtest.all.js
More file actions
52 lines (45 loc) · 2.58 KB
/
test.all.js
File metadata and controls
52 lines (45 loc) · 2.58 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
import {checkBuiltInVMAndNodeVM} from '../test-helpers/checkVM.js';
checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
describe(`JSONPath - All (${vmType})`, function () {
before(setBuiltInState);
const json = {
"name": "root",
"children": [
{"name": "child1", "children": [{"name": "child1_1"}, {"name": "child1_2"}]},
{"name": "child2", "children": [{"name": "child2_1"}]},
{"name": "child3", "children": [{"name": "child3_1"}, {"name": "child3_2"}]}
]
};
it('simple parent selection, return both path and value', () => {
const result = jsonpath({json, path: '$.children[0]^', resultType: 'all'});
assert.deepEqual(
result,
[{
path: "$['children']", value: json.children,
parent: json, parentProperty: 'children',
pointer: '/children', hasArrExpr: undefined
}]
);
});
it('parent selection with multiple matches, return both path and value', () => {
const expectedOne = {path: "$['children']", value: json.children, parent: json, parentProperty: 'children', pointer: '/children', hasArrExpr: true};
const expected = [expectedOne, expectedOne];
const result = jsonpath({json, path: '$.children[1:3]^', resultType: 'all'});
assert.deepEqual(result, expected);
});
it('select sibling via parent, return both path and value', () => {
const expected = [{path: "$['children'][2]['children'][1]", value: {name: 'child3_2'}, parent: json.children[2].children, parentProperty: 1, pointer: '/children/2/children/1', hasArrExpr: true}];
const result = jsonpath({json, path: '$..[?(@.name && @.name.match(/3_1$/))]^[?(@.name.match(/_2$/))]', resultType: 'all'});
assert.deepEqual(result, expected);
});
it('parent parent parent, return both path and value', () => {
const expected = [{path: "$['children'][0]['children']", value: json.children[0].children, parent: json.children[0], parentProperty: 'children', pointer: '/children/0/children', hasArrExpr: true}];
const result = jsonpath({json, path: '$..[?(@.name && @.name.match(/1_1$/))].name^^', resultType: 'all'});
assert.deepEqual(result, expected);
});
it('no such parent', () => {
const result = jsonpath({json, path: 'name^^', resultType: 'all'});
assert.deepEqual(result, []);
});
});
});