-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathtest.parent-selector.js
More file actions
99 lines (90 loc) · 4.16 KB
/
Copy pathtest.parent-selector.js
File metadata and controls
99 lines (90 loc) · 4.16 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
import {checkBuiltInVMAndNodeVM} from '../test-helpers/checkVM.js';
checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
describe(`JSONPath - Parent selector (${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', () => {
const result = jsonpath({json, path: '$.children[0]^', flatten: true});
assert.deepEqual(result, json.children);
});
it('parent selection with multiple matches', () => {
const expected = [json.children, json.children];
const result = jsonpath({json, path: '$.children[1:3]^'});
assert.deepEqual(result, expected);
});
it('select sibling via parent', () => {
const expected = [{"name": "child3_2"}];
const result = jsonpath({json, path: '$..[?(@.name && @.name.match(/3_1$/))]^[?(@.name.match(/_2$/))]'});
assert.deepEqual(result, expected);
});
it('parent parent parent', () => {
const expected = json.children[0].children;
const result = jsonpath({json, path: '$..[?(@.name && @.name.match(/1_1$/))].name^^', flatten: true});
assert.deepEqual(result, expected);
});
// Todo: Handle "this._trace(...).filter is not a function" error`
/*
it.only('parent root', () => {
const jsonSimple = {
children: null
};
const expected = jsonSimple.children;
const result = jsonpath({json: jsonSimple, path: '$^', flatten: true});
console.log('result', result);
assert.deepEqual(result, expected);
});
*/
it('empty string key (parent of)', () => {
const jsonSimple = {
'': null
};
const expected = jsonSimple;
const result = jsonpath({json: jsonSimple, path: '^', wrap: false});
assert.deepEqual(result, expected);
});
it('no such parent', () => {
const result = jsonpath({json, path: 'name^^'});
assert.deepEqual(result, []);
});
it('select sibling via parent (with non-match present)', () => {
const jsonMultipleChildren = {
"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"}]},
{"name": "child4", "children": [{"name": "child4_1"}, {"name": "child3_1"}]}
]
};
const expected = [{"name": "child3_2"}];
const result = jsonpath({
json: jsonMultipleChildren,
path: '$..[?(@.name && @.name.match(/3_1$/))]^[?(@.name.match(/_2$/))]'
});
assert.deepEqual(result, expected);
});
it('select sibling via parent (with multiple results)', () => {
const jsonMultipleChildren = {
"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"}, {"name": "child3_2", second: true}]}
]
};
const expected = [{"name": "child3_2"}, {"name": "child3_2", second: true}];
const result = jsonpath({
json: jsonMultipleChildren,
path: '$..[?(@.name && @.name.match(/3_1$/))]^[?(@.name.match(/_2$/))]'
});
assert.deepEqual(result, expected);
});
});
});