2015-10-13 03:29:13 -04:00
|
|
|
import {ddescribe, describe, it, xit, iit, expect, beforeEach} from 'angular2/testing_internal';
|
2015-11-06 20:34:07 -05:00
|
|
|
import {isBlank, isPresent} from 'angular2/src/facade/lang';
|
2016-01-06 17:13:44 -05:00
|
|
|
import {Parser} from 'angular2/src/compiler/expression_parser/parser';
|
2015-06-04 13:06:09 -04:00
|
|
|
import {Unparser} from './unparser';
|
2016-01-06 17:13:44 -05:00
|
|
|
import {Lexer} from 'angular2/src/compiler/expression_parser/lexer';
|
|
|
|
import {BindingPipe, LiteralPrimitive, AST} from 'angular2/src/compiler/expression_parser/ast';
|
2014-10-28 12:22:38 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-01-06 17:13:44 -05:00
|
|
|
function createParser() { return new Parser(new Lexer()); }
|
2014-11-04 12:06:46 -05:00
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
function parseAction(text, location = null): any {
|
2014-12-10 22:21:15 -05:00
|
|
|
return createParser().parseAction(text, location);
|
2014-11-04 18:51:56 -05:00
|
|
|
}
|
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
function parseBinding(text, location = null): any {
|
2014-12-10 22:21:15 -05:00
|
|
|
return createParser().parseBinding(text, location);
|
2014-11-18 19:38:36 -05:00
|
|
|
}
|
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
function parseTemplateBindings(text, location = null): any {
|
2014-12-10 22:21:15 -05:00
|
|
|
return createParser().parseTemplateBindings(text, location);
|
2014-11-03 20:25:16 -05:00
|
|
|
}
|
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
function parseInterpolation(text, location = null): any {
|
2015-01-08 19:17:56 -05:00
|
|
|
return createParser().parseInterpolation(text, location);
|
|
|
|
}
|
|
|
|
|
2015-06-22 11:21:03 -04:00
|
|
|
function parseSimpleBinding(text, location = null): any {
|
|
|
|
return createParser().parseSimpleBinding(text, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
function unparse(ast: AST): string { return new Unparser().unparse(ast); }
|
|
|
|
|
2015-12-06 01:06:24 -05:00
|
|
|
function checkInterpolation(exp: string, expected?: string) {
|
|
|
|
var ast = parseInterpolation(exp);
|
|
|
|
if (isBlank(expected)) expected = exp;
|
|
|
|
expect(unparse(ast)).toEqual(expected);
|
|
|
|
}
|
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
function checkBinding(exp: string, expected?: string) {
|
|
|
|
var ast = parseBinding(exp);
|
|
|
|
if (isBlank(expected)) expected = exp;
|
|
|
|
expect(unparse(ast)).toEqual(expected);
|
2015-06-10 05:11:01 -04:00
|
|
|
}
|
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
function checkAction(exp: string, expected?: string) {
|
|
|
|
var ast = parseAction(exp);
|
|
|
|
if (isBlank(expected)) expected = exp;
|
|
|
|
expect(unparse(ast)).toEqual(expected);
|
2014-11-03 20:25:16 -05:00
|
|
|
}
|
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
function expectActionError(text) { return expect(() => parseAction(text)); }
|
2014-10-28 12:22:38 -04:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
function expectBindingError(text) { return expect(() => parseBinding(text)); }
|
2014-11-18 19:38:36 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("parser", () => {
|
|
|
|
describe("parseAction", () => {
|
|
|
|
it('should parse numbers', () => { checkAction("1"); });
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse strings', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
checkAction("'1'", '"1"');
|
2015-08-12 19:26:21 -04:00
|
|
|
checkAction('"1"');
|
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it('should parse null', () => { checkAction("null"); });
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse unary - expressions', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
checkAction("-1", "0 - 1");
|
|
|
|
checkAction("+1", "1");
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse unary ! expressions', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
checkAction("!true");
|
|
|
|
checkAction("!!true");
|
|
|
|
checkAction("!!!true");
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse multiplicative expressions',
|
2016-04-12 12:40:37 -04:00
|
|
|
() => { checkAction("3*4/2%5", "3 * 4 / 2 % 5"); });
|
2015-04-22 05:45:33 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it('should parse additive expressions', () => { checkAction("3 + 6 - 2"); });
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse relational expressions', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
checkAction("2 < 3");
|
|
|
|
checkAction("2 > 3");
|
|
|
|
checkAction("2 <= 2");
|
|
|
|
checkAction("2 >= 2");
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse equality expressions', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
checkAction("2 == 3");
|
|
|
|
checkAction("2 != 3");
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
2014-11-03 20:25:16 -05:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse strict equality expressions', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
checkAction("2 === 3");
|
|
|
|
checkAction("2 !== 3");
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
2014-11-06 15:00:09 -05:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse expressions', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
checkAction("true && true");
|
|
|
|
checkAction("true || false");
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
2014-11-06 15:00:09 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it('should parse grouped expressions', () => { checkAction("(1 + 2) * 3", "1 + 2 * 3"); });
|
2014-11-06 15:00:09 -05:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse an empty string', () => { checkAction(''); });
|
2014-11-04 19:08:01 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("literals", () => {
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse array', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
checkAction("[1][0]");
|
|
|
|
checkAction("[[1]][0][0]");
|
|
|
|
checkAction("[]");
|
|
|
|
checkAction("[].length");
|
|
|
|
checkAction("[1, 2].length");
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse map', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
checkAction("{}");
|
|
|
|
checkAction("{a: 1}[2]");
|
|
|
|
checkAction("{}[\"a\"]");
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should only allow identifier, string, or keyword as map key', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
expectActionError('{(:0}')
|
|
|
|
.toThrowError(new RegExp('expected identifier, keyword, or string'));
|
2015-08-12 19:26:21 -04:00
|
|
|
expectActionError('{1234:0}')
|
2015-05-26 20:12:38 -04:00
|
|
|
.toThrowError(new RegExp('expected identifier, keyword, or string'));
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
2014-11-03 20:25:16 -05:00
|
|
|
});
|
2014-11-04 12:06:46 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("member access", () => {
|
|
|
|
it("should parse field access", () => {
|
|
|
|
checkAction("a");
|
|
|
|
checkAction("a.a");
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should only allow identifier or keyword as member names', () => {
|
2015-08-12 19:26:21 -04:00
|
|
|
expectActionError('x.(').toThrowError(new RegExp('identifier or keyword'));
|
|
|
|
expectActionError('x. 1234').toThrowError(new RegExp('identifier or keyword'));
|
|
|
|
expectActionError('x."foo"').toThrowError(new RegExp('identifier or keyword'));
|
2015-05-26 04:19:47 -04:00
|
|
|
});
|
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse safe field access', () => {
|
|
|
|
checkAction('a?.a');
|
|
|
|
checkAction('a.a?.a');
|
2015-05-26 04:19:47 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("method calls", () => {
|
|
|
|
it("should parse method calls", () => {
|
|
|
|
checkAction("fn()");
|
|
|
|
checkAction("add(1, 2)");
|
|
|
|
checkAction("a.add(1, 2)");
|
|
|
|
checkAction("fn().add(1, 2)");
|
2015-01-19 06:58:44 -05:00
|
|
|
});
|
2014-11-04 19:08:01 -05:00
|
|
|
});
|
2014-11-04 13:19:37 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("functional calls",
|
|
|
|
() => { it("should parse function calls", () => { checkAction("fn()(1, 2)"); }); });
|
2014-11-04 13:19:37 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("conditional", () => {
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should parse ternary/conditional expressions', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
checkAction("7 == 3 + 4 ? 10 : 20");
|
|
|
|
checkAction("false ? 10 : 20");
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw on incorrect ternary operator syntax', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
expectActionError("true?1").toThrowError(new RegExp(
|
2015-05-26 20:12:38 -04:00
|
|
|
'Parser Error: Conditional expression true\\?1 requires all 3 expressions'));
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
2014-11-04 13:19:37 -05:00
|
|
|
});
|
2014-11-04 18:51:56 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("assignment", () => {
|
|
|
|
it("should support field assignments", () => {
|
|
|
|
checkAction("a = 12");
|
|
|
|
checkAction("a.a.a = 123");
|
|
|
|
checkAction("a = 123; b = 234;");
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
2014-11-04 12:21:28 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should throw on safe field assignments", () => {
|
|
|
|
expectActionError("a?.a = 123")
|
2015-08-12 19:26:21 -04:00
|
|
|
.toThrowError(new RegExp('cannot be used in the assignment'));
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should support array updates", () => { checkAction("a[0] = 200"); });
|
2014-11-05 18:38:44 -05:00
|
|
|
});
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should error when using pipes",
|
2015-08-12 19:26:21 -04:00
|
|
|
() => { expectActionError('x|blah').toThrowError(new RegExp('Cannot have a pipe')); });
|
2014-11-18 19:38:36 -05:00
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
it('should store the source in the result',
|
|
|
|
() => { expect(parseAction('someExpr').source).toBe('someExpr'); });
|
2014-12-10 22:21:15 -05:00
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
it('should store the passed-in location',
|
|
|
|
() => { expect(parseAction('someExpr', 'location').location).toBe('location'); });
|
2015-08-20 10:34:47 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should throw when encountering interpolation", () => {
|
|
|
|
expectActionError("{{a()}}")
|
|
|
|
.toThrowErrorWith('Got interpolation ({{}}) where expression was expected');
|
2015-08-20 10:34:47 -04:00
|
|
|
});
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
2014-11-04 12:21:28 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("general error handling", () => {
|
|
|
|
it("should throw on an unexpected token", () => {
|
|
|
|
expectActionError("[1,2] trac").toThrowError(new RegExp('Unexpected token \'trac\''));
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw a reasonable error for unconsumed tokens', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
expectActionError(")")
|
|
|
|
.toThrowError(new RegExp("Unexpected token \\) at column 1 in \\[\\)\\]"));
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw on missing expected token', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
expectActionError("a(b").toThrowError(
|
|
|
|
new RegExp("Missing expected \\) at the end of the expression \\[a\\(b\\]"));
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("parseBinding", () => {
|
|
|
|
describe("pipes", () => {
|
|
|
|
it("should parse pipes", () => {
|
2015-08-12 19:26:21 -04:00
|
|
|
checkBinding('a(b | c)', 'a((b | c))');
|
|
|
|
checkBinding('a.b(c.d(e) | f)', 'a.b((c.d(e) | f))');
|
|
|
|
checkBinding('[1, 2, 3] | a', '([1, 2, 3] | a)');
|
|
|
|
checkBinding('{a: 1} | b', '({a: 1} | b)');
|
|
|
|
checkBinding('a[b] | c', '(a[b] | c)');
|
|
|
|
checkBinding('a?.b | c', '(a?.b | c)');
|
|
|
|
checkBinding('true | a', '(true | a)');
|
2015-10-13 21:55:46 -04:00
|
|
|
checkBinding('a | b:c | d', '((a | b:c) | d)');
|
|
|
|
checkBinding('a | b:(c | d)', '(a | b:(c | d))');
|
2014-11-05 13:00:19 -05:00
|
|
|
});
|
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should only allow identifier or keyword as formatter names', () => {
|
2015-08-12 19:26:21 -04:00
|
|
|
expectBindingError('"Foo"|(').toThrowError(new RegExp('identifier or keyword'));
|
|
|
|
expectBindingError('"Foo"|1234').toThrowError(new RegExp('identifier or keyword'));
|
|
|
|
expectBindingError('"Foo"|"uppercase"').toThrowError(new RegExp('identifier or keyword'));
|
2015-06-04 13:06:09 -04:00
|
|
|
});
|
2015-11-23 20:58:12 -05:00
|
|
|
|
|
|
|
it('should parse quoted expressions', () => { checkBinding('a:b', 'a:b'); });
|
|
|
|
|
2015-11-25 18:28:42 -05:00
|
|
|
it('should not crash when prefix part is not tokenizable',
|
|
|
|
() => { checkBinding('"a:b"', '"a:b"'); });
|
|
|
|
|
2015-11-23 20:58:12 -05:00
|
|
|
it('should ignore whitespace around quote prefix', () => { checkBinding(' a :b', 'a:b'); });
|
|
|
|
|
|
|
|
it('should refuse prefixes that are not single identifiers', () => {
|
|
|
|
expectBindingError('a + b:c').toThrowError();
|
|
|
|
expectBindingError('1:c').toThrowError();
|
|
|
|
});
|
2014-11-18 19:38:36 -05:00
|
|
|
});
|
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
it('should store the source in the result',
|
|
|
|
() => { expect(parseBinding('someExpr').source).toBe('someExpr'); });
|
2014-12-10 22:21:15 -05:00
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
it('should store the passed-in location',
|
|
|
|
() => { expect(parseBinding('someExpr', 'location').location).toBe('location'); });
|
2014-11-06 15:00:09 -05:00
|
|
|
|
|
|
|
it('should throw on chain expressions', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
expect(() => parseBinding("1;2")).toThrowError(new RegExp("contain chained expression"));
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
|
|
|
|
2015-06-04 13:06:09 -04:00
|
|
|
it('should throw on assignment', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
expect(() => parseBinding("a=2")).toThrowError(new RegExp("contain assignments"));
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
2015-08-20 10:34:47 -04:00
|
|
|
|
|
|
|
it('should throw when encountering interpolation', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
expectBindingError("{{a.b}}")
|
|
|
|
.toThrowErrorWith('Got interpolation ({{}}) where expression was expected');
|
2015-08-20 10:34:47 -04:00
|
|
|
});
|
2015-12-06 01:06:24 -05:00
|
|
|
|
|
|
|
it('should parse conditional expression', () => { checkBinding('a < b ? a : b'); });
|
2014-10-28 12:22:38 -04:00
|
|
|
});
|
2014-11-18 19:38:36 -05:00
|
|
|
|
|
|
|
describe('parseTemplateBindings', () => {
|
|
|
|
|
2015-10-06 22:05:09 -04:00
|
|
|
function keys(templateBindings: any[]) {
|
|
|
|
return templateBindings.map(binding => binding.key);
|
|
|
|
}
|
2014-11-18 19:38:36 -05:00
|
|
|
|
2015-10-06 22:05:09 -04:00
|
|
|
function keyValues(templateBindings: any[]) {
|
2015-10-06 21:00:42 -04:00
|
|
|
return templateBindings.map(binding => {
|
2015-01-28 01:34:25 -05:00
|
|
|
if (binding.keyIsVar) {
|
2015-05-21 15:17:46 -04:00
|
|
|
return '#' + binding.key + (isBlank(binding.name) ? '=null' : '=' + binding.name);
|
2015-01-28 01:34:25 -05:00
|
|
|
} else {
|
2015-05-26 20:12:38 -04:00
|
|
|
return binding.key + (isBlank(binding.expression) ? '' : `=${binding.expression}`)
|
2015-01-28 01:34:25 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-10-06 22:05:09 -04:00
|
|
|
function exprSources(templateBindings: any[]) {
|
2015-10-06 21:00:42 -04:00
|
|
|
return templateBindings.map(
|
|
|
|
binding => isPresent(binding.expression) ? binding.expression.source : null);
|
2014-11-18 19:38:36 -05:00
|
|
|
}
|
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse an empty string', () => { expect(parseTemplateBindings('')).toEqual([]); });
|
2014-11-18 19:38:36 -05:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse a string without a value',
|
|
|
|
() => { expect(keys(parseTemplateBindings('a'))).toEqual(['a']); });
|
2014-12-01 19:05:44 -05:00
|
|
|
|
|
|
|
it('should only allow identifier, string, or keyword including dashes as keys', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var bindings = parseTemplateBindings("a:'b'");
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
bindings = parseTemplateBindings("'a':'b'");
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
bindings = parseTemplateBindings("\"a\":'b'");
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
bindings = parseTemplateBindings("a-b:'c'");
|
2014-12-01 19:05:44 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a-b']);
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
expect(() => { parseTemplateBindings('(:0'); })
|
|
|
|
.toThrowError(new RegExp('expected identifier, keyword, or string'));
|
2014-11-18 19:38:36 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
expect(() => { parseTemplateBindings('1234:0'); })
|
|
|
|
.toThrowError(new RegExp('expected identifier, keyword, or string'));
|
2014-11-18 19:38:36 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect expressions as value', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var bindings = parseTemplateBindings("a:b");
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(exprSources(bindings)).toEqual(['b']);
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
bindings = parseTemplateBindings("a:1+1");
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(exprSources(bindings)).toEqual(['1+1']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect names as value', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var bindings = parseTemplateBindings("a:#b");
|
2015-05-21 15:17:46 -04:00
|
|
|
expect(keyValues(bindings)).toEqual(['a', '#b=\$implicit']);
|
2014-11-18 19:38:36 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow space and colon as separators', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var bindings = parseTemplateBindings("a:b");
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
expect(exprSources(bindings)).toEqual(['b']);
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
bindings = parseTemplateBindings("a b");
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
expect(exprSources(bindings)).toEqual(['b']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow multiple pairs', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var bindings = parseTemplateBindings("a 1 b 2");
|
2015-11-23 19:02:19 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a', 'aB']);
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(exprSources(bindings)).toEqual(['1 ', '2']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should store the sources in the result', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var bindings = parseTemplateBindings("a 1,b 2");
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(bindings[0].expression.source).toEqual('1');
|
|
|
|
expect(bindings[1].expression.source).toEqual('2');
|
|
|
|
});
|
2014-12-10 22:21:15 -05:00
|
|
|
|
|
|
|
it('should store the passed-in location', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var bindings = parseTemplateBindings("a 1,b 2", 'location');
|
2014-12-10 22:21:15 -05:00
|
|
|
expect(bindings[0].expression.location).toEqual('location');
|
|
|
|
});
|
2015-01-28 01:34:25 -05:00
|
|
|
|
|
|
|
it('should support var/# notation', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var bindings = parseTemplateBindings("var i");
|
2015-05-21 15:17:46 -04:00
|
|
|
expect(keyValues(bindings)).toEqual(['#i=\$implicit']);
|
2015-01-28 01:34:25 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
bindings = parseTemplateBindings("#i");
|
2015-05-21 15:17:46 -04:00
|
|
|
expect(keyValues(bindings)).toEqual(['#i=\$implicit']);
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
bindings = parseTemplateBindings("var a; var b");
|
2015-05-21 15:17:46 -04:00
|
|
|
expect(keyValues(bindings)).toEqual(['#a=\$implicit', '#b=\$implicit']);
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
bindings = parseTemplateBindings("#a; #b;");
|
2015-05-21 15:17:46 -04:00
|
|
|
expect(keyValues(bindings)).toEqual(['#a=\$implicit', '#b=\$implicit']);
|
2015-01-28 01:34:25 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
bindings = parseTemplateBindings("var i-a = k-a");
|
2015-01-28 01:34:25 -05:00
|
|
|
expect(keyValues(bindings)).toEqual(['#i-a=k-a']);
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
bindings = parseTemplateBindings("keyword var item; var i = k");
|
2015-01-28 01:34:25 -05:00
|
|
|
expect(keyValues(bindings)).toEqual(['keyword', '#item=\$implicit', '#i=k']);
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
bindings = parseTemplateBindings("keyword: #item; #i = k");
|
2015-01-28 01:34:25 -05:00
|
|
|
expect(keyValues(bindings)).toEqual(['keyword', '#item=\$implicit', '#i=k']);
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
bindings = parseTemplateBindings("directive: var item in expr; var a = b", 'location');
|
|
|
|
expect(keyValues(bindings))
|
|
|
|
.toEqual(['directive', '#item=\$implicit', 'directiveIn=expr in location', '#a=b']);
|
2015-01-28 01:34:25 -05:00
|
|
|
});
|
2015-02-23 16:12:49 -05:00
|
|
|
|
|
|
|
it('should parse pipes', () => {
|
|
|
|
var bindings = parseTemplateBindings('key value|pipe');
|
2015-05-26 20:12:38 -04:00
|
|
|
var ast = bindings[0].expression.ast;
|
2015-06-18 18:40:12 -04:00
|
|
|
expect(ast).toBeAnInstanceOf(BindingPipe);
|
2015-02-23 16:12:49 -05:00
|
|
|
});
|
2014-11-18 19:38:36 -05:00
|
|
|
});
|
2015-01-08 19:17:56 -05:00
|
|
|
|
|
|
|
describe('parseInterpolation', () => {
|
2015-05-26 20:12:38 -04:00
|
|
|
it('should return null if no interpolation',
|
|
|
|
() => { expect(parseInterpolation('nothing')).toBe(null); });
|
2015-01-08 19:17:56 -05:00
|
|
|
|
|
|
|
it('should parse no prefix/suffix interpolation', () => {
|
|
|
|
var ast = parseInterpolation('{{a}}').ast;
|
|
|
|
expect(ast.strings).toEqual(['', '']);
|
|
|
|
expect(ast.expressions.length).toEqual(1);
|
|
|
|
expect(ast.expressions[0].name).toEqual('a');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse prefix/suffix with multiple interpolation', () => {
|
2015-06-04 13:06:09 -04:00
|
|
|
var originalExp = 'before {{ a }} middle {{ b }} after';
|
|
|
|
var ast = parseInterpolation(originalExp).ast;
|
|
|
|
expect(new Unparser().unparse(ast)).toEqual(originalExp);
|
2015-01-08 19:17:56 -05:00
|
|
|
});
|
2015-08-03 06:05:45 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should throw on empty interpolation expressions", () => {
|
|
|
|
expect(() => parseInterpolation("{{}}"))
|
2015-08-20 10:34:47 -04:00
|
|
|
.toThrowErrorWith(
|
2016-04-12 12:40:37 -04:00
|
|
|
"Parser Error: Blank expressions are not allowed in interpolated strings");
|
2015-08-03 06:05:45 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
expect(() => parseInterpolation("foo {{ }}"))
|
2015-08-20 10:34:47 -04:00
|
|
|
.toThrowErrorWith(
|
2016-04-12 12:40:37 -04:00
|
|
|
"Parser Error: Blank expressions are not allowed in interpolated strings");
|
2015-08-03 06:05:45 -04:00
|
|
|
});
|
2015-12-06 01:06:24 -05:00
|
|
|
|
|
|
|
it('should parse conditional expression',
|
|
|
|
() => { checkInterpolation('{{ a < b ? a : b }}'); });
|
2015-12-21 11:32:23 -05:00
|
|
|
|
|
|
|
it('should parse expression with newline characters', () => {
|
|
|
|
checkInterpolation(`{{ 'foo' +\n 'bar' +\r 'baz' }}`, `{{ "foo" + "bar" + "baz" }}`);
|
|
|
|
});
|
2015-01-08 19:17:56 -05:00
|
|
|
});
|
2015-02-05 14:13:32 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("parseSimpleBinding", () => {
|
|
|
|
it("should parse a field access", () => {
|
|
|
|
var p = parseSimpleBinding("name");
|
|
|
|
expect(unparse(p)).toEqual("name");
|
2015-06-22 11:21:03 -04:00
|
|
|
});
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should parse a constant", () => {
|
|
|
|
var p = parseSimpleBinding("[1, 2]");
|
|
|
|
expect(unparse(p)).toEqual("[1, 2]");
|
2015-06-22 11:21:03 -04:00
|
|
|
});
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should throw when the given expression is not just a field name", () => {
|
|
|
|
expect(() => parseSimpleBinding("name + 1"))
|
2015-08-20 10:34:47 -04:00
|
|
|
.toThrowErrorWith(
|
2015-11-23 20:58:12 -05:00
|
|
|
'Host binding expression can only contain field access and constants');
|
2015-08-20 10:34:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when encountering interpolation', () => {
|
|
|
|
expect(() => parseSimpleBinding('{{exp}}'))
|
|
|
|
.toThrowErrorWith('Got interpolation ({{}}) where expression was expected');
|
2015-06-22 11:21:03 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-05 14:13:32 -05:00
|
|
|
describe('wrapLiteralPrimitive', () => {
|
|
|
|
it('should wrap a literal primitive', () => {
|
2016-04-12 12:40:37 -04:00
|
|
|
expect(unparse(createParser().wrapLiteralPrimitive("foo", null))).toEqual('"foo"');
|
2015-02-05 14:13:32 -05:00
|
|
|
});
|
|
|
|
});
|
2014-10-28 12:22:38 -04:00
|
|
|
});
|
2015-04-22 05:45:33 -04:00
|
|
|
}
|