Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions test/functions/functions-binary-ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,52 @@ describe('Binary Operators TypeScript Test', function () {
assert.strictEqual(parser.evaluate('2 + undefined'), undefined);
assert.strictEqual(parser.evaluate('undefined + 2'), undefined);
});
it('should concatenate non-numeric strings', function () {
const parser = new Parser();
assert.strictEqual(parser.evaluate('"hello" + "world"'), 'helloworld');
assert.strictEqual(parser.evaluate('"foo" + "bar"'), 'foobar');
assert.strictEqual(parser.evaluate('"test" + "123"'), 'test123');
});
it('should add numeric strings as numbers', function () {
const parser = new Parser();
assert.strictEqual(parser.evaluate('"5" + "3"'), 8);
assert.strictEqual(parser.evaluate('"10" + "20"'), 30);
assert.strictEqual(parser.evaluate('"0" + "5"'), 5);
});
it('should concatenate arrays', function () {
const parser = new Parser();
assert.deepStrictEqual(parser.evaluate('[1, 2] + [3, 4]'), [1, 2, 3, 4]);
assert.deepStrictEqual(parser.evaluate('[1] + [2, 3]'), [1, 2, 3]);
assert.deepStrictEqual(parser.evaluate('[] + [1, 2]'), [1, 2]);
});
it('should merge objects', function () {
const parser = new Parser();
assert.deepStrictEqual(parser.evaluate('{a: 1} + {b: 2}'), { a: 1, b: 2 });
assert.deepStrictEqual(parser.evaluate('{x: 10} + {y: 20}'), { x: 10, y: 20 });
assert.deepStrictEqual(parser.evaluate('{a: 1, b: 2} + {c: 3}'), { a: 1, b: 2, c: 3 });
});
it('should handle object merging with overlapping keys', function () {
const parser = new Parser();
assert.deepStrictEqual(parser.evaluate('{a: 1} + {a: 2}'), { a: 2 });
assert.deepStrictEqual(parser.evaluate('{x: 10, y: 20} + {y: 30}'), { x: 10, y: 30 });
});
it('should handle null values correctly', function () {
const parser = new Parser();
assert.deepStrictEqual(parser.evaluate('null + null'), {});
});
it('should convert numeric values to numbers before adding', function () {
const parser = new Parser();
assert.strictEqual(parser.evaluate('true + 1'), 2);
assert.strictEqual(parser.evaluate('false + 5'), 5);
assert.strictEqual(parser.evaluate('1 + true'), 2);
});
it('should throw error for incompatible types', function () {
const parser = new Parser();
assert.throws(() => parser.evaluate('5 + [1, 2]'), /Cannot add values of incompatible types/);
assert.throws(() => parser.evaluate('"text" + {a: 1}'), /Cannot add values of incompatible types/);
assert.throws(() => parser.evaluate('[1, 2] + {a: 1}'), /Cannot add values of incompatible types/);
assert.throws(() => parser.evaluate('5 + {x: 1}'), /Cannot add values of incompatible types/);
});
});

describe('- (subtraction)', function () {
Expand Down
Loading