forked from JSONPath-Plus/JSONPath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.errors.js
More file actions
155 lines (137 loc) · 5.69 KB
/
Copy pathtest.errors.js
File metadata and controls
155 lines (137 loc) · 5.69 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
import {checkBuiltInVMAndNodeVM} from '../test-helpers/checkVM.js';
checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
describe(`JSONPath - Error (${vmType})`, function () {
before(setBuiltInState);
it('should throw with missing `path`', function () {
assert.throws(() => {
jsonpath({json: []});
}, TypeError, 'You must supply a "path" property when providing an object ' +
'argument to JSONPath.evaluate().');
});
it('should throw with missing `json`', function () {
assert.throws(() => {
jsonpath({path: '$'});
}, TypeError, 'You must supply a "json" property when providing an object ' +
'argument to JSONPath.evaluate().');
});
it('should throw with a bad filter', () => {
expect(() => {
jsonpath({json: {book: []}, path: '$..[?(@.category === category)]'});
}).to.throw(Error, 'jsonPath: category is not defined: @.category === category');
});
it('should throw with a bad result type', () => {
expect(() => {
jsonpath({
json: {children: [5]},
path: '$..children',
resultType: 'badType'
});
}).to.throw(TypeError, 'Unknown result type');
});
it('should throw with `eval: false` and [?()] filtering expression', () => {
expect(() => {
const json = {
datafield: [
{"tag": "035", "subfield": {"@code": "a", "#text": "1879"}},
{"@tag": "042", "subfield": {"@code": "a", "#text": "5555"}},
{"@tag": "045", "045": "secret"}
]
};
jsonpath({
json,
path: "$.datafield[?(@.tag=='035')]",
eval: false
});
}).to.throw(Error, 'Eval [?(expr)] prevented in JSONPath expression.');
});
it('should throw with `eval: false` and [?()] filtering expression (@.length)', () => {
expect(() => {
const json = {
datafield: [
{"tag": "035", "subfield": {"@code": "a", "#text": "1879"}},
{"@tag": "042", "subfield": {"@code": "a", "#text": "5555"}},
{"@tag": "045", "045": "secret"}
]
};
jsonpath({
json,
path: '$..datafield[(@.length-1)]',
eval: false
});
}).to.throw(Error, 'Eval [(expr)] prevented in JSONPath expression.');
});
it('Syntax error in safe mode script', () => {
expect(() => {
const json = {tag: 10};
jsonpath({
json,
path: '$..[?(this)]',
wrap: false,
eval: 'safe'
});
}).to.throw(Error, 'jsonPath: Unexpected expression: this');
});
it('Invalid assignment in safe mode script', () => {
expect(() => {
const json = {tag: 10};
jsonpath({
json,
path: '$..[?(2 = 8)]',
wrap: false,
eval: 'safe'
});
}).to.throw(Error, 'jsonPath: Invalid left-hand side in assignment: 2 = 8');
});
// Parser syntax error tests
it('should throw with unbalanced brackets', () => {
expect(() => {
jsonpath.toPathParts('$.store[?(');
}).to.throw(SyntaxError, 'Unbalanced');
});
it('should throw with unexpected end after dot', () => {
expect(() => {
jsonpath.toPathParts('$.store.');
}).to.throw(SyntaxError, 'Unexpected end after dot');
});
it('should throw with unclosed filter', () => {
expect(() => {
jsonpath.toPathParts('$.store[?(@.price < 10');
}).to.throw(SyntaxError);
});
it('should throw with unclosed wildcard bracket', () => {
expect(() => {
jsonpath.toPathParts('$.store[*');
}).to.throw(SyntaxError, 'Expected ] after *');
});
it('should throw with unexpected character after backtick', () => {
expect(() => {
jsonpath.toPathParts('$.`');
}).to.throw(SyntaxError, 'Unexpected end after backtick');
});
it('should throw with unexpected end after opening bracket', () => {
expect(() => {
jsonpath.toPathParts('$[');
}).to.throw(SyntaxError, 'Unexpected end after [');
});
it('should throw with missing ( after ? in filter', () => {
expect(() => {
jsonpath.toPathParts('$[?');
}).to.throw(SyntaxError, 'Expected ( after ?');
});
it('should throw with missing ] after filter expression', () => {
expect(() => {
jsonpath.toPathParts('$[?(@.price < 10)');
}).to.throw(SyntaxError, 'Expected ] after filter');
});
it('should throw with missing ] after dynamic expression', () => {
expect(() => {
jsonpath.toPathParts('$[(@ + 1)');
}).to.throw(SyntaxError, 'Expected ] after dynamic expression');
});
it('should throw with malformed multi-property', () => {
expect(() => {
jsonpath.toPathParts("$['a','");
}).to.throw(SyntaxError);
});
});
});