Skip to content

Commit 8b41c43

Browse files
committed
Add more unit tests
1 parent 40eedaa commit 8b41c43

File tree

2 files changed

+208
-2
lines changed

2 files changed

+208
-2
lines changed

src/operations.test.ts

Lines changed: 207 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test } from '@jest/globals';
2-
import { parseExpression } from './operations';
2+
import { parseExpression, parseOp } from './operations';
33

44
describe('Test parseExpression', () => {
55
test('INT op', () => {
@@ -57,4 +57,210 @@ describe('Test parseExpression', () => {
5757
expect(parseExpression('NULL(a)', { a: {} })).toBe(false);
5858
expect(parseExpression('NULL(a)', { a: [] })).toBe(false);
5959
});
60+
61+
test('NOT_NULL op', () => {
62+
expect(parseExpression('NOT_NULL(a)', { a: null })).toBe(false);
63+
expect(parseExpression('NOT_NULL(a)', { a: undefined })).toBe(true);
64+
expect(parseExpression('NOT_NULL(a)', { a: 0 })).toBe(true);
65+
expect(parseExpression('NOT_NULL(a)', { a: '' })).toBe(true);
66+
expect(parseExpression('NOT_NULL(a)', { a: {} })).toBe(true);
67+
expect(parseExpression('NOT_NULL(a)', { a: [] })).toBe(true);
68+
});
69+
70+
test('NOT op', () => {
71+
expect(parseExpression('NOT(a)', { a: false })).toBe(true);
72+
expect(parseExpression('NOT(a)', { a: true })).toBe(false);
73+
});
74+
75+
test('STR_LEN op', () => {
76+
expect(parseExpression('STR_LEN(a)', { a: '123' })).toBe(3);
77+
expect(parseExpression('STR_LEN(a)', { a: 1 })).toBe(1);
78+
});
79+
80+
test('LOWER op', () => {
81+
expect(parseExpression('LOWER(a)', { a: 'ABCDEF' })).toBe('abcdef');
82+
});
83+
84+
test('UPPER op', () => {
85+
expect(parseExpression('UPPER(a)', { a: 'abcdef' })).toBe('ABCDEF');
86+
});
87+
88+
test('TRIM op', () => {
89+
expect(parseExpression('TRIM(a)', { a: ' abc def ' })).toBe('abc def');
90+
});
91+
92+
test('ARRAY_LEN op', () => {
93+
expect(parseExpression('ARRAY_LEN(a)', { a: [1, 2, 3] })).toBe(3);
94+
expect(parseExpression('ARRAY_LEN(a)', { a: 1 })).toBe(0);
95+
});
96+
97+
test('SUM op', () => {
98+
expect(parseExpression('SUM(a, b)', { a: 1, b: 2 })).toBe(3);
99+
});
100+
101+
test('SUBTRACT op', () => {
102+
expect(parseExpression('SUBTRACT(a, b)', { a: 5, b: 2 })).toBe(3);
103+
});
104+
105+
test('MULTIPLY op', () => {
106+
expect(parseExpression('MULTIPLY(a, b)', { a: 5, b: 2 })).toBe(10);
107+
});
108+
109+
test('DIVIDE op', () => {
110+
expect(parseExpression('DIVIDE(a, b)', { a: 5, b: 2 })).toBe(2.5);
111+
});
112+
113+
test('REMAINDER op', () => {
114+
expect(parseExpression('REMAINDER(a, b)', { a: 5, b: 2 })).toBe(1);
115+
});
116+
117+
test('ROUND op', () => {
118+
expect(parseExpression('ROUND(a, b)', { a: 5, b: 2 })).toBe('5.00');
119+
});
120+
121+
test('MAX op', () => {
122+
expect(parseExpression('MAX(a, b)', { a: 5, b: 2 })).toBe(5);
123+
});
124+
125+
test('MIN op', () => {
126+
expect(parseExpression('MIN(a, b)', { a: 5, b: 2 })).toBe(2);
127+
});
128+
129+
test('POWER op', () => {
130+
expect(parseExpression('POWER(a, b)', { a: 5, b: 2 })).toBe(25);
131+
});
132+
133+
test('CONCAT op', () => {
134+
expect(parseExpression('CONCAT(a, b)', { a: '123', b: '456' })).toBe('123456');
135+
});
136+
137+
test('LEFT op', () => {
138+
expect(parseExpression('LEFT(a, b)', { a: '123456', b: 3 })).toBe('123');
139+
});
140+
141+
test('RIGHT op', () => {
142+
expect(parseExpression('RIGHT(a, b)', { a: '123456', b: 3 })).toBe('456');
143+
});
144+
145+
test('EQUAL op', () => {
146+
expect(parseExpression('EQUAL(a, b)', { a: 1, b: 1 })).toBe(true);
147+
expect(parseExpression('EQUAL(a, b)', { a: 1, b: '1' })).toBe(false);
148+
});
149+
150+
test('NOT_EQUAL op', () => {
151+
expect(parseExpression('NOT_EQUAL(a, b)', { a: 1, b: 1 })).toBe(false);
152+
expect(parseExpression('NOT_EQUAL(a, b)', { a: 1, b: '1' })).toBe(true);
153+
});
154+
155+
test('GT op', () => {
156+
expect(parseExpression('GT(a, b)', { a: 1, b: 2 })).toBe(false);
157+
expect(parseExpression('GT(a, b)', { a: 1, b: 1 })).toBe(false);
158+
expect(parseExpression('GT(a, b)', { a: 2, b: 1 })).toBe(true);
159+
});
160+
161+
test('GTE op', () => {
162+
expect(parseExpression('GTE(a, b)', { a: 1, b: 2 })).toBe(false);
163+
expect(parseExpression('GTE(a, b)', { a: 1, b: 1 })).toBe(true);
164+
expect(parseExpression('GTE(a, b)', { a: 2, b: 1 })).toBe(true);
165+
});
166+
167+
test('LT op', () => {
168+
expect(parseExpression('LT(a, b)', { a: 1, b: 2 })).toBe(true);
169+
expect(parseExpression('LT(a, b)', { a: 1, b: 1 })).toBe(false);
170+
expect(parseExpression('LT(a, b)', { a: 2, b: 1 })).toBe(false);
171+
});
172+
173+
test('LTE op', () => {
174+
expect(parseExpression('LTE(a, b)', { a: 1, b: 2 })).toBe(true);
175+
expect(parseExpression('LTE(a, b)', { a: 1, b: 1 })).toBe(true);
176+
expect(parseExpression('LTE(a, b)', { a: 2, b: 1 })).toBe(false);
177+
});
178+
179+
test('AND op', () => {
180+
expect(parseExpression('AND(a, b)', { a: true, b: true })).toBe(true);
181+
expect(parseExpression('AND(a, b)', { a: true, b: false })).toBe(false);
182+
expect(parseExpression('AND(a, b)', { a: false, b: true })).toBe(false);
183+
expect(parseExpression('AND(a, b)', { a: false, b: false })).toBe(false);
184+
});
185+
186+
test('OR op', () => {
187+
expect(parseExpression('OR(a, b)', { a: true, b: true })).toBe(true);
188+
expect(parseExpression('OR(a, b)', { a: true, b: false })).toBe(true);
189+
expect(parseExpression('OR(a, b)', { a: false, b: true })).toBe(true);
190+
expect(parseExpression('OR(a, b)', { a: false, b: false })).toBe(false);
191+
});
192+
});
193+
194+
describe('Test parseOp', () => {
195+
test('Simple unary op', () => {
196+
expect(parseOp('OP_(var)')).toStrictEqual({
197+
op: 'OP_',
198+
a: 'var',
199+
b: null,
200+
});
201+
});
202+
203+
test('Simple binary op', () => {
204+
expect(parseOp('OP_(var1,var2)')).toStrictEqual({
205+
op: 'OP_',
206+
a: 'var1',
207+
b: 'var2',
208+
});
209+
});
210+
211+
test('Literal number', () => {
212+
expect(parseOp('1')).toStrictEqual(null);
213+
});
214+
215+
test('Field value', () => {
216+
expect(parseOp('a')).toStrictEqual(null);
217+
});
218+
219+
test('Complex op 1', () => {
220+
expect(parseOp('OP_(OP_(var1))')).toStrictEqual({
221+
op: 'OP_',
222+
a: 'OP_(var1)',
223+
b: null,
224+
});
225+
});
226+
227+
test('Complex op 2', () => {
228+
expect(parseOp('OP_(OP_(var1),var2)')).toStrictEqual({
229+
op: 'OP_',
230+
a: 'OP_(var1)',
231+
b: 'var2',
232+
});
233+
});
234+
235+
test('Complex op 3', () => {
236+
expect(parseOp('OP_(OP_(var1),OP_(var2))')).toStrictEqual({
237+
op: 'OP_',
238+
a: 'OP_(var1)',
239+
b: 'OP_(var2)',
240+
});
241+
});
242+
243+
test('Complex op 4', () => {
244+
expect(parseOp('OP_(OP_(OP_(var1), var2),OP_(var3))')).toStrictEqual({
245+
op: 'OP_',
246+
a: 'OP_(OP_(var1), var2)',
247+
b: 'OP_(var3)',
248+
});
249+
});
250+
251+
test('Complex op 5', () => {
252+
expect(parseOp('OP_(OP_(OP_(var1), var2),OP_(var3, OP_(var4, var5)))')).toStrictEqual({
253+
op: 'OP_',
254+
a: 'OP_(OP_(var1), var2)',
255+
b: 'OP_(var3, OP_(var4, var5))',
256+
});
257+
});
258+
259+
test('Complex op 5', () => {
260+
expect(parseOp('OP_(OP_(OP_(var1, OP_(var2, OP_(var3, var4))), var5))')).toStrictEqual({
261+
op: 'OP_',
262+
a: 'OP_(OP_(var1, OP_(var2, OP_(var3, var4))), var5)',
263+
b: null,
264+
});
265+
});
60266
});

src/operations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export function parseExpression(exp: string, values: Record<string, any>): any {
171171
return '';
172172
}
173173

174-
function parseOp(exp: string) {
174+
export function parseOp(exp: string) {
175175
const match = exp.match(/^([A-Z_]+)\((.+)\)$/);
176176
if (match) {
177177
const op = match[1] as string;

0 commit comments

Comments
 (0)