feat(parser): change Parser to return null when one of the operands is null

This commit is contained in:
vsavkin 2014-11-04 09:06:46 -08:00
parent a7fe25d93f
commit c41f59c794
2 changed files with 22 additions and 14 deletions

View File

@ -86,19 +86,7 @@ export class Binary extends AST {
var right = this.right.eval(context, formatters); var right = this.right.eval(context, formatters);
// Null check for the operations. // Null check for the operations.
if (left == null || right == null) { if (left == null || right == null) return null;
switch (this.operation) {
case '+':
if (left != null) return left;
if (right != null) return right;
return 0;
case '-':
if (left != null) return left;
if (right != null) return 0 - right;
return 0;
}
return null;
}
switch (this.operation) { switch (this.operation) {
case '+' : return autoConvertAdd(left, right); case '+' : return autoConvertAdd(left, right);

View File

@ -18,6 +18,10 @@ export function main() {
var context = td(); var context = td();
var formatters; var formatters;
function createParser() {
return new Parser(new Lexer(), new ClosureMap());
}
function _eval(text) { function _eval(text) {
return new Parser(new Lexer(), new ClosureMap()).parse(text) return new Parser(new Lexer(), new ClosureMap()).parse(text)
.eval(context, formatters); .eval(context, formatters);
@ -36,7 +40,7 @@ export function main() {
var parser; var parser;
beforeEach(() => { beforeEach(() => {
parser = new Parser(new Lexer(), new ClosureMap()); parser = createParser();
}); });
it("should parse field access",() => { it("should parse field access",() => {
@ -119,6 +123,22 @@ export function main() {
expectEval("4 + 4 + ' str'").toEqual("8 str"); expectEval("4 + 4 + ' str'").toEqual("8 str");
expectEval("'str ' + 4 + 4").toEqual("str 44"); expectEval("'str ' + 4 + 4").toEqual("str 44");
}); });
it('should behave gracefully with a null scope', () => {
var exp = createParser().parse("null");
expect(exp.eval(null, null)).toEqual(null);
});
it('should eval binary operators with null as null', () => {
expectEval("null < 0").toBeNull();
expectEval("null * 3").toBeNull();
expectEval("null + 6").toBeNull();
expectEval("5 + null").toBeNull();
expectEval("null - 4").toBeNull();
expectEval("3 - null").toBeNull();
expectEval("null + null").toBeNull();
expectEval("null - null").toBeNull();
});
}); });
describe("error handling", () => { describe("error handling", () => {