Navigation Menu

Skip to content

Commit

Permalink
fix(Binary): handle adding undefined
Browse files Browse the repository at this point in the history
fixes #337
  • Loading branch information
jdanyow committed Apr 15, 2016
1 parent d344831 commit d2a88dd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/ast.js
Expand Up @@ -459,15 +459,15 @@ export class Binary extends Expression {
}

// Null check for the operations.
if (left === null || right === null) {
if (left === null || right === null || left === undefined || right === undefined) {
switch (this.operation) { // eslint-disable-line
case '+':
if (left !== null) return left;
if (right !== null) return right;
if (left !== null && left !== undefined) return left;
if (right !== null && right !== undefined) return right;
return 0;
case '-':
if (left !== null) return left;
if (right !== null) return 0 - right;
if (left !== null && left !== undefined) return left;
if (right !== null && right !== undefined) return 0 - right;
return 0;
}

Expand Down
48 changes: 48 additions & 0 deletions test/ast/binary.spec.js
@@ -0,0 +1,48 @@
import {Binary, LiteralString, LiteralPrimitive} from '../../src/ast';
import {createScopeForTest} from '../../src/scope';

describe('Binary', () => {
it('concats strings', () => {
let expression = new Binary('+', new LiteralString('a'), new LiteralString('b'));
let scope = createScopeForTest({});
expect(expression.evaluate(scope, null)).toBe('ab');

expression = new Binary('+', new LiteralString('a'), new LiteralPrimitive(null));
scope = createScopeForTest({});
expect(expression.evaluate(scope, null)).toBe('a');

expression = new Binary('+', new LiteralPrimitive(null), new LiteralString('b'));
scope = createScopeForTest({});
expect(expression.evaluate(scope, null)).toBe('b');

expression = new Binary('+', new LiteralString('a'), new LiteralPrimitive(undefined));
scope = createScopeForTest({});
expect(expression.evaluate(scope, null)).toBe('a');

expression = new Binary('+', new LiteralPrimitive(undefined), new LiteralString('b'));
scope = createScopeForTest({});
expect(expression.evaluate(scope, null)).toBe('b');
});

it('adds numbers', () => {
let expression = new Binary('+', new LiteralPrimitive(1), new LiteralPrimitive(2));
let scope = createScopeForTest({});
expect(expression.evaluate(scope, null)).toBe(3);

expression = new Binary('+', new LiteralPrimitive(1), new LiteralPrimitive(null));
scope = createScopeForTest({});
expect(expression.evaluate(scope, null)).toBe(1);

expression = new Binary('+', new LiteralPrimitive(null), new LiteralPrimitive(2));
scope = createScopeForTest({});
expect(expression.evaluate(scope, null)).toBe(2);

expression = new Binary('+', new LiteralPrimitive(1), new LiteralPrimitive(undefined));
scope = createScopeForTest({});
expect(expression.evaluate(scope, null)).toBe(1);

expression = new Binary('+', new LiteralPrimitive(undefined), new LiteralPrimitive(2));
scope = createScopeForTest({});
expect(expression.evaluate(scope, null)).toBe(2);
});
});

0 comments on commit d2a88dd

Please sign in to comment.