-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathtest.eval.js
More file actions
227 lines (216 loc) · 8.05 KB
/
Copy pathtest.eval.js
File metadata and controls
227 lines (216 loc) · 8.05 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
import {checkBuiltInVMAndNodeVM} from '../test-helpers/checkVM.js';
checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
describe(`JSONPath - Eval (${vmType} - native)`, function () {
before(setBuiltInState);
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('fail if eval is unsupported', () => {
expect(() => {
jsonpath({json, path: "$..[?(@.category === category)]", eval: 'wrong-eval'});
}).to.throw(
TypeError,
'Unknown "eval" property "wrong-eval"'
);
});
it('multi statement eval', () => {
const expected = [json.store.books[0]];
const selector = '$..[?(' +
'var sum = @.price && @.price[0][email protected][1];' +
'sum > 20;)]';
const result = jsonpath({json, path: selector, wrap: false, eval: 'native'});
assert.deepEqual(result, expected);
});
it('multi statement eval (with use strict)', () => {
const expected = [json.store.books[0]];
const selector = '$..[?(' +
'"use strict";' +
'var sum = @.price && @.price[0][email protected][1];' +
'sum > 20;)]';
const result = jsonpath({json, path: selector, wrap: false,
eval: 'native'});
assert.deepEqual(result, expected);
});
it('accessing current path', () => {
const expected = [json.store.books[1]];
const result = jsonpath({json,
path: "$..[?(@path==\"$['store']['books'][1]\")]",
wrap: false,
eval: 'native'
});
assert.deepEqual(result, expected);
});
it('sandbox', () => {
const expected = [json.store.book];
const result = jsonpath({
json,
sandbox: {category: 'reference'},
path: "$..[?(@.category === category)]", wrap: false,
eval: 'native'
});
assert.deepEqual(result, expected);
});
it('sandbox (with `arguments`)', () => {
const expected = [json.store.book];
const selector = "$..[?(@.category === arguments)]";
const result = jsonpath({
json,
path: selector,
sandbox: {
arguments: 'reference'
},
wrap: false,
eval: 'native'
});
assert.deepEqual(result, expected);
});
it('sandbox with function without "function" in string', () => {
const expected = [json.store.book];
const result = jsonpath({
json,
sandbox: {
category () {
return 'reference';
}
},
path: "$..[?(@.category === category())]", wrap: false,
eval: 'native'
});
assert.deepEqual(result, expected);
});
it('sandbox with function with "function" in string', () => {
const expected = [json.store.book];
const result = jsonpath({
json,
sandbox: {
category () {
return 'reference';
}
},
path: "$..[?(@.category === category())]", wrap: false,
eval: 'native'
});
assert.deepEqual(result, expected);
});
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,
eval: 'native'
});
assert.deepEqual(result, expected);
});
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,
eval: 'native'
});
assert.deepEqual(result, expected);
});
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,
eval: 'native'
});
assert.deepEqual(result, expected);
});
});
});
describe(`JSONPath - Eval (${vmType} - custom)`, function () {
before(setBuiltInState);
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('eval as callback function', () => {
const evalCb = (code, ctxt) => {
const script = new jsonpath.prototype.safeVm.Script(code);
return script.runInNewContext(ctxt);
};
const expected = [json.store.book];
const result = jsonpath({
json,
path: '$..[?(@.category === "reference")]',
eval: evalCb
});
assert.deepEqual(result, expected);
});
it('eval as class', () => {
const expected = [json.store.book];
const result = jsonpath({
json,
path: '$..[?(@.category === "reference")]',
eval: jsonpath.prototype.safeVm.Script
});
assert.deepEqual(result, expected);
});
it('treat error as mismatch in eval', () => {
const expected = [json.store.book];
const result = jsonpath({
json,
path: '$..[?(@.category.toLowerCase() === "reference")]',
eval: jsonpath.prototype.safeVm.Script,
ignoreEvalErrors: true
});
assert.deepEqual(result, expected);
});
});
});