jmespath.js
8.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
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
var assert = require('assert');
var jmespath = require('../jmespath');
var tokenize = jmespath.tokenize;
var compile = jmespath.compile;
var strictDeepEqual = jmespath.strictDeepEqual;
describe('tokenize', function() {
it('should tokenize unquoted identifier', function() {
assert.deepEqual(tokenize('foo'),
[{type: "UnquotedIdentifier",
value: "foo",
start: 0}]);
});
it('should tokenize unquoted identifier with underscore', function() {
assert.deepEqual(tokenize('_underscore'),
[{type: "UnquotedIdentifier",
value: "_underscore",
start: 0}]);
});
it('should tokenize unquoted identifier with numbers', function() {
assert.deepEqual(tokenize('foo123'),
[{type: "UnquotedIdentifier",
value: "foo123",
start: 0}]);
});
it('should tokenize dotted lookups', function() {
assert.deepEqual(
tokenize('foo.bar'),
[{type: "UnquotedIdentifier", value: "foo", start: 0},
{type: "Dot", value: ".", start: 3},
{type: "UnquotedIdentifier", value: "bar", start: 4},
]);
});
it('should tokenize numbers', function() {
assert.deepEqual(
tokenize('foo[0]'),
[{type: "UnquotedIdentifier", value: "foo", start: 0},
{type: "Lbracket", value: "[", start: 3},
{type: "Number", value: 0, start: 4},
{type: "Rbracket", value: "]", start: 5},
]);
});
it('should tokenize numbers with multiple digits', function() {
assert.deepEqual(
tokenize("12345"),
[{type: "Number", value: 12345, start: 0}]);
});
it('should tokenize negative numbers', function() {
assert.deepEqual(
tokenize("-12345"),
[{type: "Number", value: -12345, start: 0}]);
});
it('should tokenize quoted identifier', function() {
assert.deepEqual(tokenize('"foo"'),
[{type: "QuotedIdentifier",
value: "foo",
start: 0}]);
});
it('should tokenize quoted identifier with unicode escape', function() {
assert.deepEqual(tokenize('"\\u2713"'),
[{type: "QuotedIdentifier",
value: "✓",
start: 0}]);
});
it('should tokenize literal lists', function() {
assert.deepEqual(tokenize("`[0, 1]`"),
[{type: "Literal",
value: [0, 1],
start: 0}]);
});
it('should tokenize literal dict', function() {
assert.deepEqual(tokenize("`{\"foo\": \"bar\"}`"),
[{type: "Literal",
value: {"foo": "bar"},
start: 0}]);
});
it('should tokenize literal strings', function() {
assert.deepEqual(tokenize("`\"foo\"`"),
[{type: "Literal",
value: "foo",
start: 0}]);
});
it('should tokenize json literals', function() {
assert.deepEqual(tokenize("`true`"),
[{type: "Literal",
value: true,
start: 0}]);
});
it('should not requiring surrounding quotes for strings', function() {
assert.deepEqual(tokenize("`foo`"),
[{type: "Literal",
value: "foo",
start: 0}]);
});
it('should not requiring surrounding quotes for numbers', function() {
assert.deepEqual(tokenize("`20`"),
[{type: "Literal",
value: 20,
start: 0}]);
});
it('should tokenize literal lists with chars afterwards', function() {
assert.deepEqual(
tokenize("`[0, 1]`[0]"), [
{type: "Literal", value: [0, 1], start: 0},
{type: "Lbracket", value: "[", start: 8},
{type: "Number", value: 0, start: 9},
{type: "Rbracket", value: "]", start: 10}
]);
});
it('should tokenize two char tokens with shared prefix', function() {
assert.deepEqual(
tokenize("[?foo]"),
[{type: "Filter", value: "[?", start: 0},
{type: "UnquotedIdentifier", value: "foo", start: 2},
{type: "Rbracket", value: "]", start: 5}]
);
});
it('should tokenize flatten operator', function() {
assert.deepEqual(
tokenize("[]"),
[{type: "Flatten", value: "[]", start: 0}]);
});
it('should tokenize comparators', function() {
assert.deepEqual(tokenize("<"),
[{type: "LT",
value: "<",
start: 0}]);
});
it('should tokenize two char tokens without shared prefix', function() {
assert.deepEqual(
tokenize("=="),
[{type: "EQ", value: "==", start: 0}]
);
});
it('should tokenize not equals', function() {
assert.deepEqual(
tokenize("!="),
[{type: "NE", value: "!=", start: 0}]
);
});
it('should tokenize the OR token', function() {
assert.deepEqual(
tokenize("a||b"),
[
{type: "UnquotedIdentifier", value: "a", start: 0},
{type: "Or", value: "||", start: 1},
{type: "UnquotedIdentifier", value: "b", start: 3}
]
);
});
it('should tokenize function calls', function() {
assert.deepEqual(
tokenize("abs(@)"),
[
{type: "UnquotedIdentifier", value: "abs", start: 0},
{type: "Lparen", value: "(", start: 3},
{type: "Current", value: "@", start: 4},
{type: "Rparen", value: ")", start: 5}
]
);
});
});
describe('parsing', function() {
it('should parse field node', function() {
assert.deepEqual(compile('foo'),
{type: 'Field', name: 'foo'});
});
});
describe('strictDeepEqual', function() {
it('should compare scalars', function() {
assert.strictEqual(strictDeepEqual('a', 'a'), true);
});
it('should be false for different types', function() {
assert.strictEqual(strictDeepEqual('a', 2), false);
});
it('should be false for arrays of different lengths', function() {
assert.strictEqual(strictDeepEqual([0, 1], [1, 2, 3]), false);
});
it('should be true for identical arrays', function() {
assert.strictEqual(strictDeepEqual([0, 1], [0, 1]), true);
});
it('should be true for nested arrays', function() {
assert.strictEqual(
strictDeepEqual([[0, 1], [2, 3]], [[0, 1], [2, 3]]), true);
});
it('should be true for nested arrays of strings', function() {
assert.strictEqual(
strictDeepEqual([["a", "b"], ["c", "d"]],
[["a", "b"], ["c", "d"]]), true);
});
it('should be false for different arrays of the same length', function() {
assert.strictEqual(strictDeepEqual([0, 1], [1, 2]), false);
});
it('should handle object literals', function() {
assert.strictEqual(strictDeepEqual({a: 1, b: 2}, {a: 1, b: 2}), true);
});
it('should handle keys in first not in second', function() {
assert.strictEqual(strictDeepEqual({a: 1, b: 2}, {a: 1}), false);
});
it('should handle keys in second not in first', function() {
assert.strictEqual(strictDeepEqual({a: 1}, {a: 1, b: 2}), false);
});
it('should handle nested objects', function() {
assert.strictEqual(
strictDeepEqual({a: {b: [1, 2]}},
{a: {b: [1, 2]}}), true);
});
it('should handle nested objects that are not equal', function() {
assert.strictEqual(
strictDeepEqual({a: {b: [1, 2]}},
{a: {b: [1, 4]}}), false);
});
});