2014-10-31 02:47:22 -04:00
|
|
|
import {ddescribe, describe, it, iit, expect, beforeEach} from 'test_lib/test_lib';
|
2014-11-04 18:51:56 -05:00
|
|
|
import {BaseException, isBlank} from 'facade/lang';
|
2014-10-28 12:22:38 -04:00
|
|
|
import {Parser} from 'change_detection/parser/parser';
|
|
|
|
import {Lexer} from 'change_detection/parser/lexer';
|
2014-11-04 18:51:56 -05:00
|
|
|
import {Formatter, LiteralPrimitive} from 'change_detection/parser/ast';
|
2014-10-28 12:22:38 -04:00
|
|
|
import {ClosureMap} from 'change_detection/parser/closure_map';
|
|
|
|
|
|
|
|
class TestData {
|
2014-10-31 02:47:22 -04:00
|
|
|
constructor(a, b) {
|
2014-10-28 12:22:38 -04:00
|
|
|
this.a = a;
|
2014-10-31 02:47:22 -04:00
|
|
|
this.b = b;
|
2014-10-28 12:22:38 -04:00
|
|
|
}
|
2014-11-04 18:51:56 -05:00
|
|
|
|
|
|
|
constant() {
|
|
|
|
return "constant";
|
|
|
|
}
|
|
|
|
|
|
|
|
add(a, b) {
|
|
|
|
return a + b;
|
|
|
|
}
|
2014-10-28 12:22:38 -04:00
|
|
|
}
|
|
|
|
|
2014-11-04 12:21:28 -05:00
|
|
|
class ContextWithErrors {
|
|
|
|
get boo() {
|
2014-11-04 13:19:37 -05:00
|
|
|
throw new BaseException("boo to you");
|
2014-11-04 12:21:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 12:22:38 -04:00
|
|
|
export function main() {
|
2014-10-31 02:47:22 -04:00
|
|
|
function td(a = 0, b = 0) {
|
|
|
|
return new TestData(a, b);
|
|
|
|
}
|
|
|
|
|
2014-11-04 12:06:46 -05:00
|
|
|
function createParser() {
|
|
|
|
return new Parser(new Lexer(), new ClosureMap());
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:51:56 -05:00
|
|
|
function parseAction(text) {
|
|
|
|
return createParser().parseAction(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseBinding(text) {
|
|
|
|
return createParser().parseBinding(text);
|
2014-11-03 20:25:16 -05:00
|
|
|
}
|
|
|
|
|
2014-11-04 18:51:56 -05:00
|
|
|
function expectEval(text, passedInContext = null) {
|
|
|
|
var c = isBlank(passedInContext) ? td() : passedInContext;
|
|
|
|
return expect(parseAction(text).eval(c));
|
2014-11-03 20:25:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function expectEvalError(text) {
|
2014-11-04 18:51:56 -05:00
|
|
|
return expect(() => parseAction(text).eval(td()));
|
2014-10-28 12:22:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
describe("parser", () => {
|
2014-11-04 18:51:56 -05:00
|
|
|
describe("parseAction", () => {
|
|
|
|
it("should parse field access", () => {
|
|
|
|
expectEval("a", td(999)).toEqual(999);
|
|
|
|
expectEval("a.a", td(td(999))).toEqual(999);
|
2014-10-28 12:22:38 -04:00
|
|
|
});
|
|
|
|
|
2014-10-31 02:47:22 -04:00
|
|
|
it('should parse numerical expressions', () => {
|
2014-11-03 20:25:16 -05:00
|
|
|
expectEval("1").toEqual(1);
|
2014-10-31 02:47:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse unary - expressions', () => {
|
2014-11-03 20:25:16 -05:00
|
|
|
expectEval("-1").toEqual(-1);
|
|
|
|
expectEval("+1").toEqual(1);
|
2014-10-31 02:47:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse unary ! expressions', () => {
|
2014-11-03 20:25:16 -05:00
|
|
|
expectEval("!true").toEqual(!true);
|
2014-10-31 02:47:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse multiplicative expressions', () => {
|
2014-11-04 18:51:56 -05:00
|
|
|
expectEval("3*4/2%5").toEqual(3 * 4 / 2 % 5);
|
2014-10-31 02:47:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse additive expressions', () => {
|
2014-11-04 18:51:56 -05:00
|
|
|
expectEval("3+6-2").toEqual(3 + 6 - 2);
|
2014-10-31 02:47:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse relational expressions', () => {
|
2014-11-04 18:51:56 -05:00
|
|
|
expectEval("2<3").toEqual(2 < 3);
|
|
|
|
expectEval("2>3").toEqual(2 > 3);
|
|
|
|
expectEval("2<=2").toEqual(2 <= 2);
|
|
|
|
expectEval("2>=2").toEqual(2 >= 2);
|
2014-10-31 02:47:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse equality expressions', () => {
|
2014-11-04 18:51:56 -05:00
|
|
|
expectEval("2==3").toEqual(2 == 3);
|
|
|
|
expectEval("2!=3").toEqual(2 != 3);
|
2014-10-31 02:47:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse logicalAND expressions', () => {
|
2014-11-04 18:51:56 -05:00
|
|
|
expectEval("true&&true").toEqual(true && true);
|
|
|
|
expectEval("true&&false").toEqual(true && false);
|
2014-10-31 02:47:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse logicalOR expressions', () => {
|
2014-11-04 18:51:56 -05:00
|
|
|
expectEval("false||true").toEqual(false || true);
|
|
|
|
expectEval("false||false").toEqual(false || false);
|
2014-11-03 20:25:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse ternary/conditional expressions', () => {
|
|
|
|
expectEval("7==3+4?10:20").toEqual(10);
|
|
|
|
expectEval("false?10:20").toEqual(20);
|
2014-10-31 02:47:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should auto convert ints to strings', () => {
|
2014-11-03 20:25:16 -05:00
|
|
|
expectEval("'str ' + 4").toEqual("str 4");
|
|
|
|
expectEval("4 + ' str'").toEqual("4 str");
|
|
|
|
expectEval("4 + 4").toEqual(8);
|
|
|
|
expectEval("4 + 4 + ' str'").toEqual("8 str");
|
|
|
|
expectEval("'str ' + 4 + 4").toEqual("str 44");
|
|
|
|
});
|
2014-11-04 12:06:46 -05:00
|
|
|
|
|
|
|
it('should behave gracefully with a null scope', () => {
|
2014-11-04 18:51:56 -05:00
|
|
|
var exp = createParser().parseAction("null");
|
|
|
|
expect(exp.eval(null)).toEqual(null);
|
2014-11-04 12:06:46 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should eval binary operators with null as null', () => {
|
2014-11-04 18:51:56 -05:00
|
|
|
expectEvalError("null < 0").toThrowError();
|
|
|
|
expectEvalError("null * 3").toThrowError();
|
|
|
|
expectEvalError("null + 6").toThrowError();
|
|
|
|
expectEvalError("5 + null").toThrowError();
|
|
|
|
expectEvalError("null - 4").toThrowError();
|
|
|
|
expectEvalError("3 - null").toThrowError();
|
|
|
|
expectEvalError("null + null").toThrowError();
|
|
|
|
expectEvalError("null - null").toThrowError();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("error handling", () => {
|
|
|
|
it('should throw on incorrect ternary operator syntax', () => {
|
|
|
|
expectEvalError("true?1").
|
|
|
|
toThrowError(new RegExp('Parser Error: Conditional expression true\\?1 requires all 3 expressions'));
|
|
|
|
});
|
2014-11-04 13:19:37 -05:00
|
|
|
|
2014-11-04 18:51:56 -05:00
|
|
|
it('should pass exceptions', () => {
|
|
|
|
expect(() => {
|
|
|
|
createParser().parseAction('boo').eval(new ContextWithErrors());
|
|
|
|
}).toThrowError('boo to you');
|
|
|
|
});
|
2014-11-04 13:19:37 -05:00
|
|
|
|
2014-11-04 18:51:56 -05:00
|
|
|
it('should only allow identifier or keyword as member names', () => {
|
|
|
|
expect(() => parseAction("x.(")).toThrowError(new RegExp('identifier or keyword'));
|
|
|
|
expect(() => parseAction('x. 1234')).toThrowError(new RegExp('identifier or keyword'));
|
|
|
|
expect(() => parseAction('x."foo"')).toThrowError(new RegExp('identifier or keyword'));
|
|
|
|
});
|
2014-11-04 13:19:37 -05:00
|
|
|
|
2014-11-04 18:51:56 -05:00
|
|
|
it("should error when using formatters", () => {
|
|
|
|
expectEvalError('x|blah').toThrowError(new RegExp('Cannot have a formatter'));
|
|
|
|
});
|
2014-11-04 13:19:37 -05:00
|
|
|
});
|
|
|
|
});
|
2014-11-04 18:51:56 -05:00
|
|
|
|
|
|
|
describe("parseBinding", () => {
|
|
|
|
it("should parse formatters", function () {
|
|
|
|
var exp = parseBinding("'Foo'|uppercase");
|
|
|
|
expect(exp).toBeAnInstanceOf(Formatter);
|
|
|
|
expect(exp.name).toEqual("uppercase");
|
2014-10-28 12:22:38 -04:00
|
|
|
});
|
2014-11-04 12:21:28 -05:00
|
|
|
|
2014-11-04 18:51:56 -05:00
|
|
|
it("should parse formatters with args", function () {
|
|
|
|
var exp = parseBinding("1|increment:2");
|
|
|
|
expect(exp).toBeAnInstanceOf(Formatter);
|
|
|
|
expect(exp.name).toEqual("increment");
|
|
|
|
expect(exp.args[0]).toBeAnInstanceOf(LiteralPrimitive);
|
2014-11-04 12:21:28 -05:00
|
|
|
});
|
|
|
|
|
2014-11-04 18:51:56 -05:00
|
|
|
it('should throw on chain expressions', () => {
|
|
|
|
expect(() => parseBinding("1;2")).toThrowError(new RegExp("contain chained expression"));
|
2014-11-04 12:21:28 -05:00
|
|
|
});
|
2014-10-28 12:22:38 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
}
|