feat(Parser): support if statements in actions

fixes #2022
This commit is contained in:
Victor Berchet
2015-06-10 11:11:01 +02:00
parent d64cc8d87d
commit 7d32879929
7 changed files with 146 additions and 9 deletions

View File

@ -43,10 +43,14 @@ export function main() {
function emptyLocals() { return new Locals(null, MapWrapper.create()); }
function expectEval(text, passedInContext = null, passedInLocals = null) {
function evalAction(text, passedInContext = null, passedInLocals = null) {
var c = isBlank(passedInContext) ? td() : passedInContext;
var l = isBlank(passedInLocals) ? emptyLocals() : passedInLocals;
return expect(parseAction(text).eval(c, l));
return parseAction(text).eval(c, l);
}
function expectEval(text, passedInContext = null, passedInLocals = null) {
return expect(evalAction(text, passedInContext, passedInLocals));
}
function expectEvalError(text, passedInContext = null, passedInLocals = null) {
@ -280,6 +284,28 @@ export function main() {
});
});
describe("if", () => {
it('should parse if statements', () => {
var fixtures = [
['if (true) a = 0', 0, null],
['if (false) a = 0', null, null],
['if (a == null) b = 0', null, 0],
['if (true) { a = 0; b = 0 }', 0, 0],
['if (true) { a = 0; b = 0 } else { a = 1; b = 1; }', 0, 0],
['if (false) { a = 0; b = 0 } else { a = 1; b = 1; }', 1, 1],
['if (false) { } else { a = 1; b = 1; }', 1, 1],
];
fixtures.forEach(fix => {
var testData = td(null, null);
evalAction(fix[0], testData);
expect(testData.a).toEqual(fix[1]);
expect(testData.b).toEqual(fix[2]);
});
});
});
describe("assignment", () => {
it("should support field assignments", () => {
var context = td();