This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
infix-operators.spec.js
247 lines (229 loc) · 7.29 KB
/
infix-operators.spec.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const _ = require('lodash');
const {assertBoolean, assertString, assertNumberValue, initDoc} = require('./helpers');
describe('infix operators', () => {
describe('math operators', () => {
describe('with numbers', () => {
_.forEach({
'1 + 1' : 2,
'1 - 1' : 0,
'1 * 1' : 1,
'1 div 1' : 1,
'1 mod 1' : 0,
'2 + 1' : 3,
'2 - 1' : 1,
'2 * 1' : 2,
'2 div 1' : 2,
'2 mod 1' : 0,
'1 + 2' : 3,
'1 - 2' : -1,
'1 * 2' : 2,
'1 div 2' : 0.5,
'1 mod 2' : 1,
}, (expected, expr) => {
it('should evaluate "' + expr + '" as ' + expected, () => {
assertString(expr, expected);
});
const spaceless = expr.replace(/\s/g, '');
it('should evaluate "' + spaceless + '" as ' + expected, () => {
assertString(spaceless, expected);
});
});
});
});
describe('boolean operators', () => {
describe('with numbers', () => {
_.forEach({
'1 = 1' : true,
'1 != 1' : false,
'1 = 2' : false,
'1 != 2' : true,
'1 < 2' : true,
'1 > 2' : false,
'2 < 1' : false,
'2 > 1' : true,
'1 <= 2' : true,
'1 >= 2' : false,
'2 <= 1' : false,
'2 >= 1' : true,
'1 <= 1' : true,
'1 >= 1' : true,
/* weird spacing */
'1=1' : true,
'1= 1' : true,
'1 =1' : true,
'2=1' : false,
'2= 1' : false,
'2 =1' : false,
'1!=1' : false,
'1!= 1' : false,
'1 !=1' : false,
'2!=1' : true,
'2!= 1' : true,
'2 !=1' : true,
'1<1' : false,
'1< 1' : false,
'1 <1' : false,
'2<1' : false,
'2< 1' : false,
'2 <1' : false,
'1>1' : false,
'1> 1' : false,
'1 >1' : false,
'2>1' : true,
'2> 1' : true,
'2 >1' : true,
'1<=1' : true,
'1<= 1' : true,
'1 <=1' : true,
'2<=1' : false,
'2<= 1' : false,
'2 <=1' : false,
'1>=1' : true,
'1>= 1' : true,
'1 >=1' : true,
'2>=1' : true,
'2>= 1' : true,
'2 >=1' : true,
}, (expectedBoolean, expr) => {
it('should evaluate "' + expr + '" as ' + expectedBoolean.toString().toUpperCase(), () => {
assertBoolean(expr, expectedBoolean);
});
});
});
describe('with strings', () => {
_.forEach({
'"1" = "1"' : true,
'"1" = "2"' : false,
'"1" != "1"' : false,
'"1" != "2"' : true,
// > When neither object to be compared is a node-set and the operator
// > is <=, <, >= or >, then the objects are compared by converting both
// > objects to numbers and comparing the numbers according to IEEE 754.
// - https://www.w3.org/TR/1999/REC-xpath-19991116/#booleans
'"1" < "2"' : true,
'"1" > "2"' : false,
'"2" < "1"' : false,
'"2" > "1"' : true,
'"1" <= "2"' : true,
'"1" >= "2"' : false,
'"2" <= "1"' : false,
'"2" >= "1"' : true,
'"1" <= "1"' : true,
'"1" >= "1"' : true,
'"aardvark" < "aligator"' : false,
'"aardvark" <= "aligator"' : false,
'"aligator" < "aardvark"' : false,
'"aligator" <= "aardvark"' : false,
'"possum" > "aligator"' : false,
'"possum" >= "aligator"' : false,
'"aligator" > "possum"' : false,
'"aligator" >= "possum"' : false,
}, (expectedBoolean, expr) => {
it('should evaluate "' + expr + '" as ' + expectedBoolean.toString().toUpperCase(), () => {
assertBoolean(expr, expectedBoolean);
});
});
});
describe('with booleans', () => {
_.forEach({
'true() and true()': true,
'false() and true()': false,
'true() and false()': false,
'false() and false()': false,
'true() or true()': true,
'false() or true()': true,
'true() or false()': true,
'false() or false()': false,
}, (expectedBoolean, expr) => {
it('should evaluate "' + expr + '" as ' + expectedBoolean.toString().toUpperCase(), () => {
assertBoolean(expr, expectedBoolean);
});
});
});
});
describe('with nodes', () =>{
const doc = initDoc(`
<data>
<a>1</a>
<b>2</b>
</data>`);
_.forEach({
'/data/a!= /data/b': true,
'/data/a!=/data/b': true,
'/data/a!= "1"': false,
'/data/a!="1"': false,
'"1" != /data/a': false,
'"1"!= /data/a': false,
'"1"!=/data/a': false,
'/data/a<= /data/b': true,
'/data/a<=/data/b': true,
'/data/a<= "1"': true,
'/data/a<="1"': true,
'"1" <= /data/a': true,
'"1"<= /data/a': true,
'"1"<=/data/a': true,
'/data/a>= /data/b': false,
'/data/a>=/data/b': false,
'/data/a>= "1"': true,
'/data/a>="1"': true,
'"1" >= /data/a': true,
'"1">= /data/a': true,
'"1">=/data/a': true,
}, (expectedBoolean, expr) => {
it('should evaluate "' + expr + '" as ' + expectedBoolean.toString().toUpperCase(), () => {
assertBoolean(doc, null, expr, expectedBoolean);
});
});
});
describe('number operations', () => {
it( '*,+,-,mod,div precedence rules are applied correctly', () => {
[
[ "1+2*3", 7 ],
[ "2*3+1", 7 ],
[ "1-10 mod 3 div 3", 0.6666666666666667 ],
[ "4-3*4+5-1", -4 ],
[ "(4-3)*4+5-1", 8 ],
[ "8 div 2 + 4", 8 ]
].forEach(([expr, expected]) => {
assertNumberValue(expr, expected);
});
assertNumberValue('1-1', 0);
assertNumberValue('1 - 1', 0);
});
describe('node-based calculatations with strings', () => {
let doc;
beforeEach(() => {
doc = initDoc(`<data><number id="num">4</number></data>`);
});
it('should support simple addition', () => {
// It doesn't matter whether a string or number is requested, an infix operator should ensure that both
// left and right operands are converted to numbers during evaluation.
// If multiple nodes are returned, the value of the first node will be used.
assertString('/data/number + 1', '5');
});
it('should support addition without spaces around the operator', () => {
// expect
assertString('/data/number+1', '5');
});
it('should support relative nodesets with strings', () => {
// expect
assertString(doc.getElementById('num'), null, '../number + 1', '5');
});
it('should support relative nodesets with strings without spaces around the operator', () => {
// expect
assertString(doc.getElementById('num'), null, '../number+1', '5');
});
});
it('calculation with multiple nodes operand returned as string', () => {
initDoc(`
<data>
<number>4</number>
<number>10</number>
</data>`);
// It doesn't matter whether a string or number is requested, an infix operator should ensure that both
// left and right operands are converted to numbers during evaluation.
// If multiple nodes are returned, the value of the first node will be used.
assertString('/data/number + 1', '5');
});
});
});