@@ -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();
|
||||
|
||||
@@ -6,6 +6,8 @@ import {
|
||||
Binary,
|
||||
Chain,
|
||||
Conditional,
|
||||
EmptyExpr,
|
||||
If,
|
||||
Pipe,
|
||||
FunctionCall,
|
||||
ImplicitReceiver,
|
||||
@@ -21,7 +23,7 @@ import {
|
||||
} from 'angular2/src/change_detection/parser/ast';
|
||||
|
||||
|
||||
import {StringWrapper, RegExpWrapper} from 'angular2/src/facade/lang';
|
||||
import {StringWrapper, RegExpWrapper, isPresent} from 'angular2/src/facade/lang';
|
||||
|
||||
var quoteRegExp = RegExpWrapper.create('"');
|
||||
|
||||
@@ -53,10 +55,11 @@ export class Unparser implements AstVisitor {
|
||||
}
|
||||
|
||||
visitChain(ast: Chain) {
|
||||
ast.expressions.forEach(expression => {
|
||||
this._visit(expression);
|
||||
this._expression += ';'
|
||||
});
|
||||
var len = ast.expressions.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
this._visit(ast.expressions[i]);
|
||||
this._expression += i == len - 1 ? ';' : '; ';
|
||||
}
|
||||
}
|
||||
|
||||
visitConditional(ast: Conditional) {
|
||||
@@ -67,6 +70,17 @@ export class Unparser implements AstVisitor {
|
||||
this._visit(ast.falseExp);
|
||||
}
|
||||
|
||||
visitIf(ast: If) {
|
||||
this._expression += 'if (';
|
||||
this._visit(ast.condition);
|
||||
this._expression += ') ';
|
||||
this._visitExpOrBlock(ast.trueExp);
|
||||
if (isPresent(ast.falseExp)) {
|
||||
this._expression += ' else ';
|
||||
this._visitExpOrBlock(ast.falseExp);
|
||||
}
|
||||
}
|
||||
|
||||
visitPipe(ast: Pipe) {
|
||||
this._expression += '(';
|
||||
this._visit(ast.exp);
|
||||
@@ -179,4 +193,11 @@ export class Unparser implements AstVisitor {
|
||||
}
|
||||
|
||||
private _visit(ast: AST) { ast.visit(this); }
|
||||
|
||||
private _visitExpOrBlock(ast: AST) {
|
||||
var isBlock = ast instanceof Chain || ast instanceof EmptyExpr;
|
||||
if (isBlock) this._expression += '{ ';
|
||||
this._visit(ast);
|
||||
if (isBlock) this._expression += ' }';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import {
|
||||
Binary,
|
||||
Chain,
|
||||
Conditional,
|
||||
EmptyExpr,
|
||||
If,
|
||||
Pipe,
|
||||
ImplicitReceiver,
|
||||
Interpolation,
|
||||
@@ -59,7 +61,7 @@ export function main() {
|
||||
|
||||
it('should support Binary', () => { check('a && b', Binary); });
|
||||
|
||||
it('should support Chain', () => { check('a;b;', Chain); });
|
||||
it('should support Chain', () => { check('a; b;', Chain); });
|
||||
|
||||
it('should support Conditional', () => { check('a ? b : c', Conditional); });
|
||||
|
||||
@@ -93,6 +95,17 @@ export function main() {
|
||||
|
||||
it('should support SafeMethodCall', () => { check('a?.b(c, d)', SafeMethodCall); });
|
||||
|
||||
it('should support if statements', () => {
|
||||
var ifs = [
|
||||
'if (true) a()',
|
||||
'if (true) a() else b()',
|
||||
'if (a()) { b = 1; c = 2; }',
|
||||
'if (a()) b = 1 else { c = 2; d = e(); }'
|
||||
];
|
||||
|
||||
ifs.forEach(ifStmt => check(ifStmt, If));
|
||||
});
|
||||
|
||||
it('should support complex expression', () => {
|
||||
var originalExp = 'a + 3 * fn([(c + d | e).f], {a: 3})[g].h && i';
|
||||
var ast = parseBinding(originalExp).ast;
|
||||
@@ -108,5 +121,12 @@ export function main() {
|
||||
expect(ast).toBeAnInstanceOf(Interpolation);
|
||||
expect(unparser.unparse(ast)).toEqual('a {{ b }} c');
|
||||
});
|
||||
|
||||
it('should support EmptyExpr', () => {
|
||||
var ast = parser.parseAction('if (true) { }', null).ast;
|
||||
expect(ast).toBeAnInstanceOf(If);
|
||||
expect((<If>ast).trueExp).toBeAnInstanceOf(EmptyExpr);
|
||||
expect(unparser.unparse(ast)).toEqual('if (true) { }');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user