2014-11-06 15:00:09 -05:00
|
|
|
import {ddescribe, describe, it, xit, iit, expect, beforeEach} from 'test_lib/test_lib';
|
2014-11-18 19:38:36 -05:00
|
|
|
import {BaseException, isBlank, isPresent} from 'facade/lang';
|
2014-11-20 15:07:48 -05:00
|
|
|
import {reflector} from 'reflection/reflection';
|
2014-11-18 19:38:36 -05:00
|
|
|
import {MapWrapper, ListWrapper} from 'facade/collection';
|
2014-10-28 12:22:38 -04:00
|
|
|
import {Parser} from 'change_detection/parser/parser';
|
|
|
|
import {Lexer} from 'change_detection/parser/lexer';
|
2014-11-26 12:44:31 -05:00
|
|
|
import {ContextWithVariableBindings} from 'change_detection/parser/context_with_variable_bindings';
|
2014-11-04 18:51:56 -05:00
|
|
|
import {Formatter, LiteralPrimitive} from 'change_detection/parser/ast';
|
2014-10-28 12:22:38 -04:00
|
|
|
|
|
|
|
class TestData {
|
2014-11-22 00:19:23 -05:00
|
|
|
a;
|
|
|
|
b;
|
|
|
|
fnReturnValue;
|
2014-11-05 18:38:44 -05:00
|
|
|
constructor(a, b, fnReturnValue) {
|
2014-10-28 12:22:38 -04:00
|
|
|
this.a = a;
|
2014-10-31 02:47:22 -04:00
|
|
|
this.b = b;
|
2014-11-05 18:38:44 -05:00
|
|
|
this.fnReturnValue = fnReturnValue;
|
2014-10-28 12:22:38 -04:00
|
|
|
}
|
2014-11-04 18:51:56 -05:00
|
|
|
|
2014-11-05 18:38:44 -05:00
|
|
|
fn() {
|
|
|
|
return this.fnReturnValue;
|
2014-11-04 18:51:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
add(a, b) {
|
|
|
|
return a + b;
|
|
|
|
}
|
2014-10-28 12:22:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function main() {
|
2014-11-05 18:38:44 -05:00
|
|
|
function td(a = 0, b = 0, fnReturnValue = "constant") {
|
|
|
|
return new TestData(a, b, fnReturnValue);
|
2014-10-31 02:47:22 -04:00
|
|
|
}
|
|
|
|
|
2014-11-04 12:06:46 -05:00
|
|
|
function createParser() {
|
2014-11-20 15:07:48 -05:00
|
|
|
return new Parser(new Lexer(), reflector);
|
2014-11-04 12:06:46 -05:00
|
|
|
}
|
|
|
|
|
2014-11-04 18:51:56 -05:00
|
|
|
function parseAction(text) {
|
2014-11-18 19:38:36 -05:00
|
|
|
return createParser().parseAction(text).ast;
|
2014-11-04 18:51:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseBinding(text) {
|
2014-11-18 19:38:36 -05:00
|
|
|
return createParser().parseBinding(text).ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseTemplateBindings(text) {
|
|
|
|
return createParser().parseTemplateBindings(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
|
|
|
}
|
|
|
|
|
2014-11-26 12:44:31 -05:00
|
|
|
function expectEvalError(text, passedInContext = null) {
|
|
|
|
var c = isBlank(passedInContext) ? td() : passedInContext;
|
|
|
|
return expect(() => parseAction(text).eval(c));
|
2014-10-28 12:22:38 -04:00
|
|
|
}
|
|
|
|
|
2014-11-18 19:38:36 -05:00
|
|
|
function evalAsts(asts, passedInContext = null) {
|
|
|
|
var c = isBlank(passedInContext) ? td() : passedInContext;
|
|
|
|
var res = [];
|
|
|
|
for (var i=0; i<asts.length; i++) {
|
|
|
|
ListWrapper.push(res, asts[i].eval(c));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-10-28 12:22:38 -04:00
|
|
|
describe("parser", () => {
|
2014-11-04 18:51:56 -05:00
|
|
|
describe("parseAction", () => {
|
2014-11-06 15:00:09 -05:00
|
|
|
describe("basic expressions", () => {
|
|
|
|
it('should parse numerical expressions', () => {
|
|
|
|
expectEval("1").toEqual(1);
|
|
|
|
});
|
2014-10-28 12:22:38 -04:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should parse strings', () => {
|
|
|
|
expectEval("'1'").toEqual('1');
|
|
|
|
expectEval('"1"').toEqual('1');
|
|
|
|
});
|
2014-11-04 19:08:01 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should parse null', () => {
|
|
|
|
expectEval("null").toBe(null);
|
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should parse unary - expressions', () => {
|
|
|
|
expectEval("-1").toEqual(-1);
|
|
|
|
expectEval("+1").toEqual(1);
|
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should parse unary ! expressions', () => {
|
|
|
|
expectEval("!true").toEqual(!true);
|
|
|
|
expectEval("!!true").toEqual(!!true);
|
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should parse multiplicative expressions', () => {
|
|
|
|
expectEval("3*4/2%5").toEqual(3 * 4 / 2 % 5);
|
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should parse additive expressions', () => {
|
|
|
|
expectEval("3+6-2").toEqual(3 + 6 - 2);
|
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should parse relational expressions', () => {
|
|
|
|
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
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should parse equality expressions', () => {
|
|
|
|
expectEval("2==3").toEqual(2 == 3);
|
|
|
|
expectEval("2!=3").toEqual(2 != 3);
|
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should parse logicalAND expressions', () => {
|
|
|
|
expectEval("true&&true").toEqual(true && true);
|
|
|
|
expectEval("true&&false").toEqual(true && false);
|
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should parse logicalOR expressions', () => {
|
|
|
|
expectEval("false||true").toEqual(false || true);
|
|
|
|
expectEval("false||false").toEqual(false || false);
|
|
|
|
});
|
2014-11-03 20:25:16 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should short-circuit AND operator', () => {
|
|
|
|
expectEval('false && a()', td(() => {throw "BOOM"})).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should short-circuit OR operator', () => {
|
|
|
|
expectEval('true || a()', td(() => {throw "BOOM"})).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should evaluate grouped expressions', () => {
|
|
|
|
expectEval("(1+2)*3").toEqual((1+2)*3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should auto convert ints to strings', () => {
|
|
|
|
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-10-31 02:47:22 -04:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should throw when one of the operands is null', () => {
|
|
|
|
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();
|
|
|
|
});
|
2014-11-25 21:38:28 -05:00
|
|
|
|
|
|
|
it('should parse an empty string', () => {
|
|
|
|
expectEval('').toBeNull();
|
|
|
|
});
|
2014-11-04 19:08:01 -05:00
|
|
|
});
|
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
describe("literals", () => {
|
|
|
|
it('should evaluate array', () => {
|
|
|
|
expectEval("[1][0]").toEqual(1);
|
|
|
|
expectEval("[[1]][0][0]").toEqual(1);
|
|
|
|
expectEval("[]").toEqual([]);
|
|
|
|
expectEval("[].length").toEqual(0);
|
|
|
|
expectEval("[1, 2].length").toEqual(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should evaluate map', () => {
|
|
|
|
expectEval("{}").toEqual(MapWrapper.create());
|
|
|
|
expectEval("{a:'b'}['a']").toEqual('b');
|
|
|
|
expectEval("{'a':'b'}['a']").toEqual('b');
|
|
|
|
expectEval("{\"a\":'b'}['a']").toEqual('b');
|
|
|
|
expectEval("{\"a\":'b'}['a']").toEqual("b");
|
|
|
|
expectEval("{}['a']").not.toBeDefined();
|
|
|
|
expectEval("{\"a\":'b'}['invalid']").not.toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should only allow identifier, string, or keyword as map key', () => {
|
|
|
|
expectEvalError('{(:0}').toThrowError(new RegExp('expected identifier, keyword, or string'));
|
|
|
|
expectEvalError('{1234:0}').toThrowError(new RegExp('expected identifier, keyword, or string'));
|
|
|
|
});
|
2014-11-03 20:25:16 -05:00
|
|
|
});
|
2014-11-04 12:06:46 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
describe("member access", () => {
|
|
|
|
it("should parse field access", () => {
|
|
|
|
expectEval("a", td(999)).toEqual(999);
|
|
|
|
expectEval("a.a", td(td(999))).toEqual(999);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when accessing a field on null', () => {
|
|
|
|
expectEvalError("a.a.a").toThrowError();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should only allow identifier or keyword as member names', () => {
|
|
|
|
expectEvalError('x.(').toThrowError(new RegExp('identifier or keyword'));
|
|
|
|
expectEvalError('x. 1234').toThrowError(new RegExp('identifier or keyword'));
|
|
|
|
expectEvalError('x."foo"').toThrowError(new RegExp('identifier or keyword'));
|
|
|
|
});
|
2014-11-26 12:44:31 -05:00
|
|
|
|
|
|
|
it("should read a field from ContextWithVariableBindings", () => {
|
|
|
|
var locals = new ContextWithVariableBindings(null,
|
|
|
|
MapWrapper.createFromPairs([["key", "value"]]));
|
|
|
|
expectEval("key", locals).toEqual("value");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle nested ContextWithVariableBindings", () => {
|
|
|
|
var nested = new ContextWithVariableBindings(null,
|
|
|
|
MapWrapper.createFromPairs([["key", "value"]]));
|
|
|
|
var locals = new ContextWithVariableBindings(nested, MapWrapper.create());
|
|
|
|
expectEval("key", locals).toEqual("value");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fall back to a regular field read when ContextWithVariableBindings "+
|
|
|
|
"does not have the requested field", () => {
|
|
|
|
var locals = new ContextWithVariableBindings(td(999), MapWrapper.create());
|
|
|
|
expectEval("a", locals).toEqual(999);
|
|
|
|
});
|
2014-11-04 18:51:56 -05:00
|
|
|
});
|
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
describe("method calls", () => {
|
|
|
|
it("should evaluate method calls", () => {
|
|
|
|
expectEval("fn()", td(0, 0, "constant")).toEqual("constant");
|
|
|
|
expectEval("add(1,2)").toEqual(3);
|
|
|
|
expectEval("a.add(1,2)", td(td())).toEqual(3);
|
|
|
|
expectEval("fn().add(1,2)", td(0, 0, td())).toEqual(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when no method', () => {
|
|
|
|
expectEvalError("blah()").toThrowError();
|
|
|
|
});
|
2014-11-04 19:08:01 -05:00
|
|
|
});
|
2014-11-04 13:19:37 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
describe("functional calls", () => {
|
|
|
|
it("should evaluate function calls", () => {
|
|
|
|
expectEval("fn()(1,2)", td(0, 0, (a, b) => a + b)).toEqual(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw on non-function function calls', () => {
|
|
|
|
expectEvalError("4()").toThrowError(new RegExp('4 is not a function'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse functions for object indices', () => {
|
|
|
|
expectEval('a[b()]()', td([()=>6], () => 0)).toEqual(6);
|
|
|
|
});
|
2014-11-04 19:08:01 -05:00
|
|
|
});
|
2014-11-04 13:19:37 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
describe("conditional", () => {
|
|
|
|
it('should parse ternary/conditional expressions', () => {
|
|
|
|
expectEval("7==3+4?10:20").toEqual(10);
|
|
|
|
expectEval("false?10:20").toEqual(20);
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
describe("assignment", () => {
|
|
|
|
it("should support field assignments", () => {
|
|
|
|
var context = td();
|
|
|
|
expectEval("a=12", context).toEqual(12);
|
|
|
|
expect(context.a).toEqual(12);
|
|
|
|
});
|
2014-11-04 12:21:28 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it("should support nested field assignments", () => {
|
|
|
|
var context = td(td(td()));
|
|
|
|
expectEval("a.a.a=123;", context).toEqual(123);
|
|
|
|
expect(context.a.a.a).toEqual(123);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should support multiple assignments", () => {
|
|
|
|
var context = td();
|
|
|
|
expectEval("a=123; b=234", context).toEqual(234);
|
|
|
|
expect(context.a).toEqual(123);
|
|
|
|
expect(context.b).toEqual(234);
|
|
|
|
});
|
2014-11-05 13:00:19 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it("should support array updates", () => {
|
|
|
|
var context = td([100]);
|
|
|
|
expectEval('a[0] = 200', context).toEqual(200);
|
|
|
|
expect(context.a[0]).toEqual(200);
|
|
|
|
});
|
2014-11-05 16:48:36 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it("should support map updates", () => {
|
|
|
|
var context = td(MapWrapper.createFromPairs([["key", 100]]));
|
|
|
|
expectEval('a["key"] = 200', context).toEqual(200);
|
|
|
|
expect(MapWrapper.get(context.a, "key")).toEqual(200);
|
|
|
|
});
|
2014-11-18 19:38:36 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it("should support array/map updates", () => {
|
|
|
|
var context = td([MapWrapper.createFromPairs([["key", 100]])]);
|
|
|
|
expectEval('a[0]["key"] = 200', context).toEqual(200);
|
|
|
|
expect(MapWrapper.get(context.a[0], "key")).toEqual(200);
|
|
|
|
});
|
2014-11-05 16:48:36 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should allow assignment after array dereference', () => {
|
|
|
|
var context = td([td()]);
|
|
|
|
expectEval('a[0].a = 200', context).toEqual(200);
|
|
|
|
expect(context.a[0].a).toEqual(200);
|
|
|
|
});
|
2014-11-05 16:48:36 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should throw on bad assignment', () => {
|
|
|
|
expectEvalError("5=4").toThrowError(new RegExp("Expression 5 is not assignable"));
|
|
|
|
});
|
2014-11-26 12:44:31 -05:00
|
|
|
|
|
|
|
it('should reassign when no variable binding with the given name', () => {
|
|
|
|
var context = td();
|
|
|
|
var locals = new ContextWithVariableBindings(context, MapWrapper.create());
|
|
|
|
expectEval('a = 200', locals).toEqual(200);
|
|
|
|
expect(context.a).toEqual(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when reassigning a variable binding', () => {
|
|
|
|
var locals = new ContextWithVariableBindings(null, MapWrapper.createFromPairs([["key", "value"]]));
|
|
|
|
expectEvalError('key = 200', locals).toThrowError(new RegExp("Cannot reassign a variable binding"));
|
|
|
|
});
|
2014-11-05 16:48:36 -05:00
|
|
|
});
|
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
describe("general error handling", () => {
|
|
|
|
it("should throw on an unexpected token", () => {
|
|
|
|
expectEvalError("[1,2] trac")
|
|
|
|
.toThrowError(new RegExp('Unexpected token \'trac\''));
|
|
|
|
});
|
2014-11-05 16:53:45 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should throw a reasonable error for unconsumed tokens', () => {
|
|
|
|
expectEvalError(")").toThrowError(new RegExp("Unexpected token \\) at column 1 in \\[\\)\\]"));
|
|
|
|
});
|
2014-11-05 18:38:44 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should throw on missing expected token', () => {
|
|
|
|
expectEvalError("a(b").toThrowError(new RegExp("Missing expected \\) at the end of the expression \\[a\\(b\\]"));
|
|
|
|
});
|
2014-11-05 18:38:44 -05:00
|
|
|
});
|
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it("should error when using formatters", () => {
|
|
|
|
expectEvalError('x|blah').toThrowError(new RegExp('Cannot have a formatter'));
|
2014-11-05 16:48:36 -05:00
|
|
|
});
|
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should pass exceptions', () => {
|
|
|
|
expect(() => {
|
2014-11-18 19:38:36 -05:00
|
|
|
createParser().parseAction('a()').ast.eval(td(() => {throw new BaseException("boo to you")}));
|
2014-11-06 15:00:09 -05:00
|
|
|
}).toThrowError('boo to you');
|
2014-11-05 16:48:36 -05:00
|
|
|
});
|
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
describe("multiple statements", () => {
|
|
|
|
it("should return the last non-blank value", () => {
|
|
|
|
expectEval("a=1;b=3;a+b").toEqual(4);
|
|
|
|
expectEval("1;;").toEqual(1);
|
|
|
|
});
|
2014-11-04 12:21:28 -05:00
|
|
|
});
|
2014-11-18 19:38:36 -05:00
|
|
|
|
|
|
|
it('should store the source in the result', () => {
|
|
|
|
expect(createParser().parseAction('someExpr').source).toBe('someExpr');
|
|
|
|
});
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
2014-11-04 12:21:28 -05:00
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
describe("parseBinding", () => {
|
|
|
|
describe("formatters", () => {
|
|
|
|
it("should parse formatters", () => {
|
2014-11-05 13:00:19 -05:00
|
|
|
var exp = parseBinding("'Foo'|uppercase");
|
|
|
|
expect(exp).toBeAnInstanceOf(Formatter);
|
|
|
|
expect(exp.name).toEqual("uppercase");
|
|
|
|
});
|
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it("should parse formatters with args", () => {
|
2014-11-05 13:00:19 -05:00
|
|
|
var exp = parseBinding("1|increment:2");
|
|
|
|
expect(exp).toBeAnInstanceOf(Formatter);
|
|
|
|
expect(exp.name).toEqual("increment");
|
|
|
|
expect(exp.args[0]).toBeAnInstanceOf(LiteralPrimitive);
|
|
|
|
});
|
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should only allow identifier or keyword as formatter names', () => {
|
|
|
|
expect(() => parseBinding('"Foo"|(')).toThrowError(new RegExp('identifier or keyword'));
|
|
|
|
expect(() => parseBinding('"Foo"|1234')).toThrowError(new RegExp('identifier or keyword'));
|
|
|
|
expect(() => parseBinding('"Foo"|"uppercase"')).toThrowError(new RegExp('identifier or keyword'));
|
2014-11-05 13:00:19 -05:00
|
|
|
});
|
2014-11-18 19:38:36 -05:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should store the source in the result', () => {
|
|
|
|
expect(createParser().parseBinding('someExpr').source).toBe('someExpr');
|
2014-11-04 12:21:28 -05:00
|
|
|
});
|
2014-11-06 15:00:09 -05:00
|
|
|
|
|
|
|
it('should throw on chain expressions', () => {
|
|
|
|
expect(() => parseBinding("1;2")).toThrowError(new RegExp("contain chained expression"));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw on assignmnt', () => {
|
|
|
|
expect(() => parseBinding("1;2")).toThrowError(new RegExp("contain chained expression"));
|
|
|
|
});
|
2014-10-28 12:22:38 -04:00
|
|
|
});
|
2014-11-18 19:38:36 -05:00
|
|
|
|
|
|
|
describe('parseTemplateBindings', () => {
|
|
|
|
|
|
|
|
function keys(templateBindings) {
|
|
|
|
return ListWrapper.map(templateBindings, (binding) => binding.key );
|
|
|
|
}
|
|
|
|
|
|
|
|
function names(templateBindings) {
|
|
|
|
return ListWrapper.map(templateBindings, (binding) => binding.name );
|
|
|
|
}
|
|
|
|
|
|
|
|
function exprSources(templateBindings) {
|
|
|
|
return ListWrapper.map(templateBindings,
|
|
|
|
(binding) => isPresent(binding.expression) ? binding.expression.source : null );
|
|
|
|
}
|
|
|
|
|
|
|
|
function exprAsts(templateBindings) {
|
|
|
|
return ListWrapper.map(templateBindings,
|
|
|
|
(binding) => isPresent(binding.expression) ? binding.expression.ast : null );
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should parse an empty string', () => {
|
2014-11-25 21:38:28 -05:00
|
|
|
var bindings = parseTemplateBindings('');
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(bindings).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2014-12-01 19:05:44 -05:00
|
|
|
it('should parse a string without a value', () => {
|
|
|
|
var bindings = parseTemplateBindings('a');
|
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should only allow identifier, string, or keyword including dashes as keys', () => {
|
2014-11-18 19:38:36 -05:00
|
|
|
var bindings = parseTemplateBindings("a:'b'");
|
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
|
|
|
|
bindings = parseTemplateBindings("'a':'b'");
|
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
|
|
|
|
bindings = parseTemplateBindings("\"a\":'b'");
|
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
|
2014-12-01 19:05:44 -05:00
|
|
|
bindings = parseTemplateBindings("a-b:'c'");
|
|
|
|
expect(keys(bindings)).toEqual(['a-b']);
|
|
|
|
|
2014-11-18 19:38:36 -05:00
|
|
|
expect( () => {
|
|
|
|
parseTemplateBindings('(:0');
|
|
|
|
}).toThrowError(new RegExp('expected identifier, keyword, or string'));
|
|
|
|
|
|
|
|
expect( () => {
|
|
|
|
parseTemplateBindings('1234:0');
|
|
|
|
}).toThrowError(new RegExp('expected identifier, keyword, or string'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect expressions as value', () => {
|
|
|
|
var bindings = parseTemplateBindings("a:b");
|
|
|
|
expect(exprSources(bindings)).toEqual(['b']);
|
|
|
|
expect(evalAsts(exprAsts(bindings), td(0, 23))).toEqual([23]);
|
|
|
|
|
|
|
|
bindings = parseTemplateBindings("a:1+1");
|
|
|
|
expect(exprSources(bindings)).toEqual(['1+1']);
|
|
|
|
expect(evalAsts(exprAsts(bindings))).toEqual([2]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect names as value', () => {
|
|
|
|
var bindings = parseTemplateBindings("a:#b");
|
|
|
|
expect(names(bindings)).toEqual(['b']);
|
|
|
|
expect(exprSources(bindings)).toEqual([null]);
|
|
|
|
expect(exprAsts(bindings)).toEqual([null]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow space and colon as separators', () => {
|
|
|
|
var bindings = parseTemplateBindings("a:b");
|
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
expect(exprSources(bindings)).toEqual(['b']);
|
|
|
|
|
|
|
|
bindings = parseTemplateBindings("a b");
|
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
expect(exprSources(bindings)).toEqual(['b']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow multiple pairs', () => {
|
|
|
|
var bindings = parseTemplateBindings("a 1 b 2");
|
|
|
|
expect(keys(bindings)).toEqual(['a', 'b']);
|
|
|
|
expect(exprSources(bindings)).toEqual(['1 ', '2']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should store the sources in the result', () => {
|
|
|
|
var bindings = parseTemplateBindings("a 1,b 2");
|
|
|
|
expect(bindings[0].expression.source).toEqual('1');
|
|
|
|
expect(bindings[1].expression.source).toEqual('2');
|
|
|
|
});
|
|
|
|
});
|
2014-10-28 12:22:38 -04:00
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
}
|
2014-11-05 13:00:19 -05:00
|
|
|
|