This repository was archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathparser.specs.js
More file actions
208 lines (138 loc) · 6.57 KB
/
parser.specs.js
File metadata and controls
208 lines (138 loc) · 6.57 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
var assert = require('assert');
var parser = require("../lib");
describe('odata.parser grammar', function () {
it('should parse $top and return the value', function () {
var ast = parser.parse('$top=40');
assert.equal(ast.$top, 40);
});
it('should parse two params', function () {
var ast = parser.parse('$top=4&$skip=5');
assert.equal(ast.$top, 4);
assert.equal(ast.$skip, 5);
});
it('should parse three params', function () {
var ast = parser.parse('$top=4&$skip=5&$select=Rating');
assert.equal(ast.$top, 4);
assert.equal(ast.$skip, 5);
assert.equal(ast.$select[0], "Rating");
});
it('should parse string params', function () {
var ast = parser.parse('$select=Rating');
assert.equal(ast.$select[0], 'Rating');
});
it('should accept * in $select', function () {
var ast = parser.parse('$select=*');
assert.equal(ast.$select[0], '*');
});
it('should accept * and , and / in $select', function () {
var ast = parser.parse('$select=*,Category/Name');
assert.equal(ast.$select[0], '*');
assert.equal(ast.$select[1], 'Category/Name');
});
it('should accept more than two fields', function () {
var ast = parser.parse('$select=Rating, Name,LastName');
assert.equal(ast.$select[0], 'Rating');
assert.equal(ast.$select[1], 'Name');
assert.equal(ast.$select[2], 'LastName');
});
// This select parameter is not currently supported.
it('should accept * after . in $select', function () {
var ast = parser.parse('$select=DemoService.*');
assert.equal(ast.$select[0], 'DemoService.*');
});
it('should parse order by', function () {
var ast = parser.parse('$orderby=ReleaseDate desc, Rating');
assert.equal(ast.$orderby[0].ReleaseDate, 'desc');
assert.equal(ast.$orderby[1].Rating, 'asc');
});
it('should parse $filter', function () {
var ast = parser.parse("$filter=Name eq 'Jef'");
assert.equal(ast.$filter.type, "eq");
assert.equal(ast.$filter.left.type, "property");
assert.equal(ast.$filter.left.name, "Name");
assert.equal(ast.$filter.right.type, "literal");
assert.equal(ast.$filter.right.value, "Jef");
});
it('should parse multiple conditions in a $filter', function () {
var ast = parser.parse("$filter=Name eq 'John' and LastName lt 'Doe'");
assert.equal(ast.$filter.type, "and");
assert.equal(ast.$filter.left.type, "eq");
assert.equal(ast.$filter.left.left.type, "property");
assert.equal(ast.$filter.left.left.name, "Name");
assert.equal(ast.$filter.left.right.type, "literal");
assert.equal(ast.$filter.left.right.value, "John");
assert.equal(ast.$filter.right.type, "lt");
assert.equal(ast.$filter.right.left.type, "property");
assert.equal(ast.$filter.right.left.name, "LastName");
assert.equal(ast.$filter.right.right.type, "literal");
assert.equal(ast.$filter.right.right.value, "Doe");
});
it('should parse substringof $filter', function () {
var ast = parser.parse("$filter=substringof('nginx', Data)");
assert.equal(ast.$filter.type, "functioncall");
assert.equal(ast.$filter.func, "substringof");
assert.equal(ast.$filter.args[0].type, "literal");
assert.equal(ast.$filter.args[0].value, "nginx");
assert.equal(ast.$filter.args[1].type, "property");
assert.equal(ast.$filter.args[1].name, "Data");
});
it('should parse substringof $filter with empty string', function () {
var ast = parser.parse("$filter=substringof('', Data)");
assert.equal(ast.$filter.args[0].type, "literal");
assert.equal(ast.$filter.args[0].value, "");
});
it('should parse substringof eq true in $filter', function () {
var ast = parser.parse("$filter=substringof('nginx', Data) eq true");
assert.equal(ast.$filter.type, "eq");
assert.equal(ast.$filter.left.type, "functioncall");
assert.equal(ast.$filter.left.func, "substringof");
assert.equal(ast.$filter.left.args[0].type, "literal");
assert.equal(ast.$filter.left.args[0].value, "nginx");
assert.equal(ast.$filter.left.args[1].type, "property");
assert.equal(ast.$filter.left.args[1].name, "Data");
assert.equal(ast.$filter.right.type, "literal");
assert.equal(ast.$filter.right.value, true);
});
it('should parse startswith $filter', function () {
var ast = parser.parse("$filter=startswith('nginx', Data)");
assert.equal(ast.$filter.type, "functioncall");
assert.equal(ast.$filter.func, "startswith");
assert.equal(ast.$filter.args[0].type, "literal");
assert.equal(ast.$filter.args[0].value, "nginx");
assert.equal(ast.$filter.args[1].type, "property");
assert.equal(ast.$filter.args[1].name, "Data");
});
it('should return an error if invalid value', function() {
var ast = parser.parse("$top=foo");
assert.equal(ast.error, "invalid $top parameter");
});
it('should convert dates to javascript Date', function () {
var ast = parser.parse("$top=2&$filter=Date gt datetime'2012-09-27T21:12:59'");
assert.ok(ast.$filter.right.value instanceof Date);
});
it('should parse numbers okay', function(){
var ast = parser.parse('$filter=status eq 35');
assert.equal(ast.$filter.right.value, 35);
});
it('should parse larger digit numbers okay', function(){
var ast = parser.parse('$filter=status eq 67823456');
assert.equal(ast.$filter.right.value, 67823456);
});
it('should parse rational numbers okay', function(){
var ast = parser.parse('$filter=status gt 43.5');
assert.equal(ast.$filter.right.value, 43.5, 0.001);
});
it('should parse negative numbers okay', function(){
var ast = parser.parse('$filter=status eq -3');
assert.equal(ast.$filter.right.value, -3);
});
it('should parse $expand and return an array of identifier paths', function () {
var ast = parser.parse('$expand=Category,Products/Suppliers');
assert.equal(ast.$expand[0], 'Category');
assert.equal(ast.$expand[1], 'Products/Suppliers');
});
// it('xxxxx', function () {
// var ast = parser.parse("$top=2&$filter=Date gt datetime'2012-09-27T21:12:59'");
// console.log(JSON.stringify(ast, 0, 2));
// });
});