forked from msironi/expr-eval
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoperators-logical.ts
More file actions
278 lines (217 loc) · 7.95 KB
/
operators-logical.ts
File metadata and controls
278 lines (217 loc) · 7.95 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { expect, describe, it } from 'vitest';
import { Parser } from '../../index.js';
import spy from '../lib/spy.js';
// Helper functions for spy tests
function returnTrue() {
return true;
}
function returnFalse() {
return false;
}
// Logical Operators Tests - Converted from operators.js
// Tests for and, or, not, in, not in operators and conditional operator
describe('Logical Operators TypeScript Test', () => {
describe('and operator', () => {
it('1 and 0', () => {
expect(Parser.evaluate('1 and 0')).toBe(false);
});
it('1 and 1', () => {
expect(Parser.evaluate('1 and 1')).toBe(true);
});
it('0 and 0', () => {
expect(Parser.evaluate('0 and 0')).toBe(false);
});
it('0 and 1', () => {
expect(Parser.evaluate('0 and 1')).toBe(false);
});
it('0 and 1 and 0', () => {
expect(Parser.evaluate('0 and 1 and 0')).toBe(false);
});
it('1 and 1 and 0', () => {
expect(Parser.evaluate('1 and 1 and 0')).toBe(false);
});
it('skips rhs when lhs is false', () => {
const notCalled = spy(returnFalse);
// Security: Functions must be registered in parser.functions before use
const parser = new Parser();
parser.functions.notCalled = notCalled;
expect(parser.evaluate('false and notCalled()')).toBe(false);
expect(notCalled.called).toBe(false);
});
it('evaluates rhs when lhs is true', () => {
const called = spy(returnFalse);
// Security: Functions must be registered in parser.functions before use
const parser = new Parser();
parser.functions.called = called;
expect(parser.evaluate('true and called()')).toBe(false);
expect(called.called).toBe(true);
});
});
describe('or operator', () => {
it('1 or 0', () => {
expect(Parser.evaluate('1 or 0')).toBe(true);
});
it('1 or 1', () => {
expect(Parser.evaluate('1 or 1')).toBe(true);
});
it('0 or 0', () => {
expect(Parser.evaluate('0 or 0')).toBe(false);
});
it('0 or 1', () => {
expect(Parser.evaluate('0 or 1')).toBe(true);
});
it('0 or 1 or 0', () => {
expect(Parser.evaluate('0 or 1 or 0')).toBe(true);
});
it('1 or 1 or 0', () => {
expect(Parser.evaluate('1 or 1 or 0')).toBe(true);
});
it('skips rhs when lhs is true', () => {
const notCalled = spy(returnFalse);
// Security: Functions must be registered in parser.functions before use
const parser = new Parser();
parser.functions.notCalled = notCalled;
expect(parser.evaluate('true or notCalled()')).toBe(true);
expect(notCalled.called).toBe(false);
});
it('evaluates rhs when lhs is false', () => {
const called = spy(returnTrue);
// Security: Functions must be registered in parser.functions before use
const parser = new Parser();
parser.functions.called = called;
expect(parser.evaluate('false or called()')).toBe(true);
expect(called.called).toBe(true);
});
});
describe('in operator', () => {
const parser = new Parser();
it('"a" in ["a", "b"]', () => {
expect(parser.evaluate('"a" in toto', { toto: ['a', 'b'] })).toBe(true);
});
it('"a" in ["b", "a"]', () => {
expect(parser.evaluate('"a" in toto', { toto: ['b', 'a'] })).toBe(true);
});
it('3 in [4, 3]', () => {
expect(parser.evaluate('3 in toto', { toto: [4, 3] })).toBe(true);
});
it('"c" in ["a", "b"]', () => {
expect(parser.evaluate('"c" in toto', { toto: ['a', 'b'] })).toBe(false);
});
it('"c" in ["b", "a"]', () => {
expect(parser.evaluate('"c" in toto', { toto: ['b', 'a'] })).toBe(false);
});
it('3 in [1, 2]', () => {
expect(parser.evaluate('3 in toto', { toto: [1, 2] })).toBe(false);
});
});
describe('not in operator', () => {
const parser = new Parser();
it('"c" not in ["a", "b"]', () => {
expect(parser.evaluate('"c" not in toto', { toto: ['a', 'b'] })).toBe(true);
});
it('"a" in ["a", "b"]', () => {
expect(parser.evaluate('"a" not in toto', { toto: ['a', 'b'] })).toBe(false);
});
it('"a" in ["b", "a"]', () => {
expect(parser.evaluate('"a" not in toto', { toto: ['b', 'a'] })).toBe(false);
});
it('3 in [4, 3]', () => {
expect(parser.evaluate('3 not in toto', { toto: [4, 3] })).toBe(false);
});
it('"c" in ["a", "b"]', () => {
expect(parser.evaluate('"c" not in toto', { toto: ['a', 'b'] })).toBe(true);
});
it('"c" in ["b", "a"]', () => {
expect(parser.evaluate('"c" not in toto', { toto: ['b', 'a'] })).toBe(true);
});
it('3 in [1, 2]', () => {
expect(parser.evaluate('3 not in toto', { toto: [1, 2] })).toBe(true);
});
});
describe('not operator', () => {
it('not 1', () => {
expect(Parser.evaluate('not 1')).toBe(false);
});
it('not true', () => {
expect(Parser.evaluate('not true')).toBe(false);
});
it('not 0', () => {
expect(Parser.evaluate('not 0')).toBe(true);
});
it('not false', () => {
expect(Parser.evaluate('not false')).toBe(true);
});
it('not 4', () => {
expect(Parser.evaluate('not 4')).toBe(false);
});
it('1 and not 0', () => {
expect(Parser.evaluate('1 and not 0')).toBe(true);
});
it('not \'0\'', () => {
expect(Parser.evaluate('not \'0\'')).toBe(false);
});
it('not \'\'', () => {
expect(Parser.evaluate('not \'\'')).toBe(true);
});
});
describe('conditional operator', () => {
const parser = new Parser();
it('1 ? 2 : 0 ? 3 : 4', () => {
expect(parser.evaluate('1 ? 2 : 0 ? 3 : 4')).toBe(2);
});
it('(1 ? 2 : 0) ? 3 : 4', () => {
expect(parser.evaluate('(1 ? 2 : 0) ? 3 : 4')).toBe(3);
});
it('0 ? 2 : 0 ? 3 : 4', () => {
expect(parser.evaluate('0 ? 2 : 0 ? 3 : 4')).toBe(4);
});
it('(0 ? 2 : 0) ? 3 : 4', () => {
expect(parser.evaluate('0 ? 2 : 0 ? 3 : 4')).toBe(4);
});
it('(0 ? 0 : 2) ? 3 : 4', () => {
expect(parser.evaluate('(1 ? 2 : 0) ? 3 : 4')).toBe(3);
});
it('min(1 ? 3 : 10, 0 ? 11 : 2)', () => {
expect(parser.evaluate('min(1 ? 3 : 10, 0 ? 11 : 2)')).toBe(2);
});
it('a == 1 ? b == 2 ? 3 : 4 : 5', () => {
expect(parser.evaluate('a == 1 ? b == 2 ? 3 : 4 : 5', { a: 1, b: 2 })).toBe(3);
expect(parser.evaluate('a == 1 ? b == 2 ? 3 : 4 : 5', { a: 1, b: 9 })).toBe(4);
expect(parser.evaluate('a == 1 ? b == 2 ? 3 : 4 : 5', { a: 9, b: 2 })).toBe(5);
expect(parser.evaluate('a == 1 ? b == 2 ? 3 : 4 : 5', { a: 9, b: 9 })).toBe(5);
});
it('should only evaluate one branch', () => {
expect(parser.evaluate('1 ? 42 : fail')).toBe(42);
expect(parser.evaluate('0 ? fail : 99')).toBe(99);
});
});
describe('length operator', () => {
const parser = new Parser();
it('should return 0 for empty strings', () => {
expect(parser.evaluate('length ""')).toBe(0);
});
it('should return the length of a string', () => {
expect(parser.evaluate('length "a"')).toBe(1);
expect(parser.evaluate('length "as"')).toBe(2);
expect(parser.evaluate('length "asd"')).toBe(3);
expect(parser.evaluate('length "asdf"')).toBe(4);
});
it('should convert numbers to strings', () => {
expect(parser.evaluate('length 0')).toBe(1);
expect(parser.evaluate('length 12')).toBe(2);
expect(parser.evaluate('length 999')).toBe(3);
expect(parser.evaluate('length 1000')).toBe(4);
expect(parser.evaluate('length -1')).toBe(2);
expect(parser.evaluate('length -999')).toBe(4);
});
it('should return 0 for empty arrays', () => {
expect(parser.evaluate('length []')).toBe(0);
});
it('should return the length of an array', () => {
expect(parser.evaluate('length [123]')).toBe(1);
expect(parser.evaluate('length [123, 456]')).toBe(2);
expect(parser.evaluate('length [12, 34, 56]')).toBe(3);
expect(parser.evaluate('length [1, 2, 3, 4]')).toBe(4);
});
});
});