| 
									
										
										
										
											2015-05-26 10:19:47 +02:00
										 |  |  | import {ddescribe, describe, it, xit, iit, expect, beforeEach, IS_DARTIUM} from 'angular2/test_lib'; | 
					
						
							| 
									
										
										
										
											2015-02-05 13:08:05 -08:00
										 |  |  | import {BaseException, isBlank, isPresent} from 'angular2/src/facade/lang'; | 
					
						
							|  |  |  | import {reflector} from 'angular2/src/reflection/reflection'; | 
					
						
							|  |  |  | import {MapWrapper, ListWrapper} from 'angular2/src/facade/collection'; | 
					
						
							|  |  |  | import {Parser} from 'angular2/src/change_detection/parser/parser'; | 
					
						
							| 
									
										
										
										
											2015-06-04 19:06:09 +02:00
										 |  |  | import {Unparser} from './unparser'; | 
					
						
							| 
									
										
										
										
											2015-02-05 13:08:05 -08:00
										 |  |  | import {Lexer} from 'angular2/src/change_detection/parser/lexer'; | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  | import {Locals} from 'angular2/src/change_detection/parser/locals'; | 
					
						
							| 
									
										
										
										
											2015-06-04 19:06:09 +02:00
										 |  |  | import {Pipe, LiteralPrimitive} from 'angular2/src/change_detection/parser/ast'; | 
					
						
							| 
									
										
										
										
											2014-10-28 12:22:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class TestData { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |   constructor(public a?: any, public b?: any, public fnReturnValue?: any) {} | 
					
						
							| 
									
										
										
										
											2014-11-04 15:51:56 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |   fn() { return this.fnReturnValue; } | 
					
						
							| 
									
										
										
										
											2014-11-04 15:51:56 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |   add(a, b) { return a + b; } | 
					
						
							| 
									
										
										
										
											2014-10-28 12:22:38 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |   function td(a: any = 0, b: any = 0, fnReturnValue: any = "constant") { | 
					
						
							| 
									
										
										
										
											2014-11-05 15:38:44 -08:00
										 |  |  |     return new TestData(a, b, fnReturnValue); | 
					
						
							| 
									
										
										
										
											2014-10-30 23:47:22 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |   function createParser() { return new Parser(new Lexer(), reflector); } | 
					
						
							| 
									
										
										
										
											2014-11-04 09:06:46 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |   function parseAction(text, location = null): any { | 
					
						
							| 
									
										
										
										
											2014-12-10 19:21:15 -08:00
										 |  |  |     return createParser().parseAction(text, location); | 
					
						
							| 
									
										
										
										
											2014-11-04 15:51:56 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |   function parseBinding(text, location = null): any { | 
					
						
							| 
									
										
										
										
											2014-12-10 19:21:15 -08:00
										 |  |  |     return createParser().parseBinding(text, location); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |   function parseTemplateBindings(text, location = null): any { | 
					
						
							| 
									
										
										
										
											2014-12-10 19:21:15 -08:00
										 |  |  |     return createParser().parseTemplateBindings(text, location); | 
					
						
							| 
									
										
										
										
											2014-11-03 17:25:16 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |   function parseInterpolation(text, location = null): any { | 
					
						
							| 
									
										
										
										
											2015-01-08 16:17:56 -08:00
										 |  |  |     return createParser().parseInterpolation(text, location); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |   function addPipes(ast, pipes): any { return createParser().addPipes(ast, pipes); } | 
					
						
							| 
									
										
										
										
											2015-02-19 17:47:25 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |   function emptyLocals() { return new Locals(null, MapWrapper.create()); } | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function expectEval(text, passedInContext = null, passedInLocals = null) { | 
					
						
							| 
									
										
										
										
											2014-11-04 15:51:56 -08:00
										 |  |  |     var c = isBlank(passedInContext) ? td() : passedInContext; | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |     var l = isBlank(passedInLocals) ? emptyLocals() : passedInLocals; | 
					
						
							|  |  |  |     return expect(parseAction(text).eval(c, l)); | 
					
						
							| 
									
										
										
										
											2014-11-03 17:25:16 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |   function expectEvalError(text, passedInContext = null, passedInLocals = null) { | 
					
						
							| 
									
										
										
										
											2014-11-26 09:44:31 -08:00
										 |  |  |     var c = isBlank(passedInContext) ? td() : passedInContext; | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |     var l = isBlank(passedInLocals) ? emptyLocals() : passedInLocals; | 
					
						
							|  |  |  |     return expect(() => parseAction(text).eval(c, l)); | 
					
						
							| 
									
										
										
										
											2014-10-28 12:22:38 -04:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  |   function evalAsts(asts, passedInContext = null) { | 
					
						
							|  |  |  |     var c = isBlank(passedInContext) ? td() : passedInContext; | 
					
						
							|  |  |  |     var res = []; | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |     for (var i = 0; i < asts.length; i++) { | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |       ListWrapper.push(res, asts[i].eval(c, emptyLocals())); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return res; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 12:22:38 -04:00
										 |  |  |   describe("parser", () => { | 
					
						
							| 
									
										
										
										
											2014-11-04 15:51:56 -08:00
										 |  |  |     describe("parseAction", () => { | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |       describe("basic expressions", () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it('should parse numerical expressions', () => { expectEval("1").toEqual(1); }); | 
					
						
							| 
									
										
										
										
											2014-10-28 12:22:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         it('should parse strings', () => { | 
					
						
							|  |  |  |           expectEval("'1'").toEqual('1'); | 
					
						
							|  |  |  |           expectEval('"1"').toEqual('1'); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-11-04 16:08:01 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it('should parse null', () => { expectEval("null").toBe(null); }); | 
					
						
							| 
									
										
										
										
											2014-10-30 23:47:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         it('should parse unary - expressions', () => { | 
					
						
							|  |  |  |           expectEval("-1").toEqual(-1); | 
					
						
							|  |  |  |           expectEval("+1").toEqual(1); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-10-30 23:47:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         it('should parse unary ! expressions', () => { | 
					
						
							|  |  |  |           expectEval("!true").toEqual(!true); | 
					
						
							|  |  |  |           expectEval("!!true").toEqual(!!true); | 
					
						
							| 
									
										
										
										
											2015-04-22 11:45:33 +02:00
										 |  |  |           expectEval("!!!true").toEqual(!!!true); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-10-30 23:47:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it('should parse multiplicative expressions', | 
					
						
							|  |  |  |            () => { expectEval("3*4/2%5").toEqual(3 * 4 / 2 % 5); }); | 
					
						
							| 
									
										
										
										
											2014-10-30 23:47:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it('should parse additive expressions', () => { expectEval("3+6-2").toEqual(3 + 6 - 2); }); | 
					
						
							| 
									
										
										
										
											2014-10-30 23:47:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08: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-30 23:47:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         it('should parse equality expressions', () => { | 
					
						
							|  |  |  |           expectEval("2==3").toEqual(2 == 3); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           expectEval("2=='2'").toEqual(2 == <any>'2'); | 
					
						
							|  |  |  |           expectEval("2=='3'").toEqual(2 == <any>'3'); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |           expectEval("2!=3").toEqual(2 != 3); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           expectEval("2!='3'").toEqual(2 != <any>'3'); | 
					
						
							|  |  |  |           expectEval("2!='2'").toEqual(2 != <any>'2'); | 
					
						
							|  |  |  |           expectEval("2!=!false").toEqual(2 != <any>!false); | 
					
						
							| 
									
										
										
										
											2015-04-22 11:45:33 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should parse strict equality expressions', () => { | 
					
						
							|  |  |  |           expectEval("2===3").toEqual(2 === 3); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           expectEval("2==='3'").toEqual(2 === <any>'3'); | 
					
						
							|  |  |  |           expectEval("2==='2'").toEqual(2 === <any>'2'); | 
					
						
							| 
									
										
										
										
											2015-04-22 11:45:33 +02:00
										 |  |  |           expectEval("2!==3").toEqual(2 !== 3); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           expectEval("2!=='3'").toEqual(2 !== <any>'3'); | 
					
						
							|  |  |  |           expectEval("2!=='2'").toEqual(2 !== <any>'2'); | 
					
						
							|  |  |  |           expectEval("false===!true").toEqual(false === !true); | 
					
						
							|  |  |  |           expectEval("false!==!!true").toEqual(false !== !!true); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-10-30 23:47:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         it('should parse logicalAND expressions', () => { | 
					
						
							|  |  |  |           expectEval("true&&true").toEqual(true && true); | 
					
						
							|  |  |  |           expectEval("true&&false").toEqual(true && false); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-10-30 23:47:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         it('should parse logicalOR expressions', () => { | 
					
						
							|  |  |  |           expectEval("false||true").toEqual(false || true); | 
					
						
							|  |  |  |           expectEval("false||false").toEqual(false || false); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-11-03 17:25:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it('should short-circuit AND operator', | 
					
						
							|  |  |  |            () => { expectEval('false && a()', td(() => {throw "BOOM"})).toBe(false); }); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it('should short-circuit OR operator', | 
					
						
							|  |  |  |            () => { expectEval('true || a()', td(() => {throw "BOOM"})).toBe(true); }); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it('should evaluate grouped expressions', | 
					
						
							|  |  |  |            () => { expectEval("(1+2)*3").toEqual((1 + 2) * 3); }); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it('should parse an empty string', () => { expectEval('').toBeNull(); }); | 
					
						
							| 
									
										
										
										
											2014-11-04 16:08:01 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08: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', () => { | 
					
						
							| 
									
										
										
										
											2014-12-16 10:10:32 -08:00
										 |  |  |           expectEval("{}").toEqual({}); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |           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', () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           expectEvalError('{(:0}') | 
					
						
							|  |  |  |               .toThrowError(new RegExp('expected identifier, keyword, or string')); | 
					
						
							|  |  |  |           expectEvalError('{1234:0}') | 
					
						
							|  |  |  |               .toThrowError(new RegExp('expected identifier, keyword, or string')); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-11-03 17:25:16 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-11-04 09:06:46 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |       describe("member access", () => { | 
					
						
							|  |  |  |         it("should parse field access", () => { | 
					
						
							|  |  |  |           expectEval("a", td(999)).toEqual(999); | 
					
						
							|  |  |  |           expectEval("a.a", td(td(999))).toEqual(999); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it('should throw when accessing a field on null', | 
					
						
							|  |  |  |            () => { expectEvalError("a.a.a").toThrowError(); }); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         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 09:44:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |         it("should read a field from Locals", () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           var locals = new Locals(null, MapWrapper.createFromPairs([["key", "value"]])); | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |           expectEval("key", null, locals).toEqual("value"); | 
					
						
							| 
									
										
										
										
											2014-11-26 09:44:31 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |         it("should handle nested Locals", () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           var nested = new Locals(null, MapWrapper.createFromPairs([["key", "value"]])); | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |           var locals = new Locals(nested, MapWrapper.create()); | 
					
						
							|  |  |  |           expectEval("key", null, locals).toEqual("value"); | 
					
						
							| 
									
										
										
										
											2014-11-26 09:44:31 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it("should fall back to a regular field read when Locals " + | 
					
						
							|  |  |  |                "does not have the requested field", | 
					
						
							|  |  |  |            () => { | 
					
						
							|  |  |  |              var locals = new Locals(null, MapWrapper.create()); | 
					
						
							|  |  |  |              expectEval("a", td(999), locals).toEqual(999); | 
					
						
							|  |  |  |            }); | 
					
						
							| 
									
										
										
										
											2014-11-04 15:51:56 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 10:19:47 +02:00
										 |  |  |       describe('safe navigation operator', () => { | 
					
						
							|  |  |  |         it('should parse field access', () => { | 
					
						
							|  |  |  |           expectEval('a?.a', td(td(999))).toEqual(999); | 
					
						
							|  |  |  |           expectEval('a.a?.a', td(td(td(999)))).toEqual(999); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should return null when accessing a field on null', | 
					
						
							|  |  |  |            () => { expect(() => { expectEval('null?.a', td()).toEqual(null); }).not.toThrow(); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should have the same priority as .', () => { | 
					
						
							|  |  |  |           expect(() => { expectEval('null?.a.a', td()).toEqual(null); }).toThrowError(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!IS_DARTIUM) { | 
					
						
							|  |  |  |           it('should return null when accessing a field on undefined', () => { | 
					
						
							|  |  |  |             expect(() => { expectEval('_undefined?.a', td()).toEqual(null); }).not.toThrow(); | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should evaluate method calls', | 
					
						
							|  |  |  |            () => { expectEval('a?.add(1,2)', td(td())).toEqual(3); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should return null when accessing a method on null', () => { | 
					
						
							|  |  |  |           expect(() => { expectEval('null?.add(1, 2)', td()).toEqual(null); }).not.toThrow(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08: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); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |         it('should throw when more than 10 arguments', () => { | 
					
						
							|  |  |  |           expectEvalError("fn(1,2,3,4,5,6,7,8,9,10,11)").toThrowError(new RegExp('more than')); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it('should throw when no method', () => { expectEvalError("blah()").toThrowError(); }); | 
					
						
							| 
									
										
										
										
											2015-01-19 12:58:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |         it('should evaluate a method from Locals', () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           var locals = new Locals(null, MapWrapper.createFromPairs([['fn', () => 'child']])); | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |           expectEval("fn()", td(0, 0, 'parent'), locals).toEqual('child'); | 
					
						
							| 
									
										
										
										
											2015-01-19 12:58:44 +01:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |         it('should fall back to the parent context when Locals does not ' + | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |                'have the requested method', | 
					
						
							|  |  |  |            () => { | 
					
						
							|  |  |  |              var locals = new Locals(null, MapWrapper.create()); | 
					
						
							|  |  |  |              expectEval("fn()", td(0, 0, 'parent'), locals).toEqual('parent'); | 
					
						
							|  |  |  |            }); | 
					
						
							| 
									
										
										
										
											2014-11-04 16:08:01 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-11-04 10:19:37 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |       describe("functional calls", () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it("should evaluate function calls", | 
					
						
							|  |  |  |            () => { expectEval("fn()(1,2)", td(0, 0, (a, b) => a + b)).toEqual(3); }); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it('should throw on non-function function calls', | 
					
						
							|  |  |  |            () => { expectEvalError("4()").toThrowError(new RegExp('4 is not a function')); }); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         it('should parse functions for object indices', | 
					
						
							|  |  |  |            () => { expectEval('a[b()]()', td([() => 6], () => 0)).toEqual(6); }); | 
					
						
							| 
									
										
										
										
											2014-11-04 16:08:01 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-11-04 10:19:37 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08: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', () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           expectEvalError("true?1").toThrowError(new RegExp( | 
					
						
							|  |  |  |               'Parser Error: Conditional expression true\\?1 requires all 3 expressions')); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-11-04 10:19:37 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-11-04 15:51:56 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08: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 09:21:28 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08: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 10:00:19 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08: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 13:48:36 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         it("should support map updates", () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           var context = td({"key": 100}); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |           expectEval('a["key"] = 200', context).toEqual(200); | 
					
						
							| 
									
										
										
										
											2014-12-16 10:10:32 -08:00
										 |  |  |           expect(context.a["key"]).toEqual(200); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         it("should support array/map updates", () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           var context = td([{"key": 100}]); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |           expectEval('a[0]["key"] = 200', context).toEqual(200); | 
					
						
							| 
									
										
										
										
											2014-12-16 10:10:32 -08:00
										 |  |  |           expect(context.a[0]["key"]).toEqual(200); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-11-05 13:48:36 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08: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 13:48:36 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         it('should throw on bad assignment', () => { | 
					
						
							|  |  |  |           expectEvalError("5=4").toThrowError(new RegExp("Expression 5 is not assignable")); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-11-26 09:44:31 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         it('should reassign when no variable binding with the given name', () => { | 
					
						
							|  |  |  |           var context = td(); | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |           var locals = new Locals(null, MapWrapper.create()); | 
					
						
							|  |  |  |           expectEval('a = 200', context, locals).toEqual(200); | 
					
						
							| 
									
										
										
										
											2014-11-26 09:44:31 -08:00
										 |  |  |           expect(context.a).toEqual(200); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should throw when reassigning a variable binding', () => { | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |           var locals = new Locals(null, MapWrapper.createFromPairs([["key", "value"]])); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           expectEvalError('key = 200', null, locals) | 
					
						
							|  |  |  |               .toThrowError(new RegExp("Cannot reassign a variable binding")); | 
					
						
							| 
									
										
										
										
											2014-11-26 09:44:31 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-11-05 13:48:36 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |       describe("general error handling", () => { | 
					
						
							|  |  |  |         it("should throw on an unexpected token", () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           expectEvalError("[1,2] trac").toThrowError(new RegExp('Unexpected token \'trac\'')); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-11-05 13:53:45 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         it('should throw a reasonable error for unconsumed tokens', () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           expectEvalError(")") | 
					
						
							|  |  |  |               .toThrowError(new RegExp("Unexpected token \\) at column 1 in \\[\\)\\]")); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-11-05 15:38:44 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         it('should throw on missing expected token', () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           expectEvalError("a(b").toThrowError( | 
					
						
							|  |  |  |               new RegExp("Missing expected \\) at the end of the expression \\[a\\(b\\]")); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-11-05 15:38:44 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |       it("should error when using pipes", | 
					
						
							|  |  |  |          () => { expectEvalError('x|blah').toThrowError(new RegExp('Cannot have a pipe')); }); | 
					
						
							| 
									
										
										
										
											2014-11-05 13:48:36 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |       it('should pass exceptions', () => { | 
					
						
							|  |  |  |         expect(() => { | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |           parseAction('a()').eval(td(() => {throw new BaseException("boo to you")}), emptyLocals()); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         }).toThrowError('boo to you'); | 
					
						
							| 
									
										
										
										
											2014-11-05 13:48:36 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08: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 09:21:28 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |       it('should store the source in the result', | 
					
						
							|  |  |  |          () => { expect(parseAction('someExpr').source).toBe('someExpr'); }); | 
					
						
							| 
									
										
										
										
											2014-12-10 19:21:15 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |       it('should store the passed-in location', | 
					
						
							|  |  |  |          () => { expect(parseAction('someExpr', 'location').location).toBe('location'); }); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2014-11-04 09:21:28 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |     describe("parseBinding", () => { | 
					
						
							| 
									
										
										
										
											2015-02-20 10:59:14 -08:00
										 |  |  |       describe("pipes", () => { | 
					
						
							|  |  |  |         it("should parse pipes", () => { | 
					
						
							| 
									
										
										
										
											2015-06-04 19:06:09 +02:00
										 |  |  |           var originalExp = '"Foo" | uppercase'; | 
					
						
							|  |  |  |           var ast = parseBinding(originalExp).ast; | 
					
						
							|  |  |  |           expect(ast).toBeAnInstanceOf(Pipe); | 
					
						
							|  |  |  |           expect(new Unparser().unparse(ast)).toEqual(`(${originalExp})`); | 
					
						
							| 
									
										
										
										
											2014-11-05 10:00:19 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-15 13:34:59 -07:00
										 |  |  |         it("should parse pipes in the middle of a binding", () => { | 
					
						
							| 
									
										
										
										
											2015-06-04 19:06:09 +02:00
										 |  |  |           var ast = parseBinding('(user | a | b).name').ast; | 
					
						
							|  |  |  |           expect(new Unparser().unparse(ast)).toEqual('((user | a) | b).name'); | 
					
						
							| 
									
										
										
										
											2015-04-15 13:34:59 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-20 10:59:14 -08:00
										 |  |  |         it("should parse pipes with args", () => { | 
					
						
							| 
									
										
										
										
											2015-06-04 19:06:09 +02:00
										 |  |  |           var ast = parseBinding("(1|a:2)|b:3").ast; | 
					
						
							|  |  |  |           expect(new Unparser().unparse(ast)).toEqual('((1 | a:2) | b:3)'); | 
					
						
							| 
									
										
										
										
											2014-11-05 10:00:19 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         it('should only allow identifier or keyword as formatter names', () => { | 
					
						
							|  |  |  |           expect(() => parseBinding('"Foo"|(')).toThrowError(new RegExp('identifier or keyword')); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |           expect(() => parseBinding('"Foo"|1234')) | 
					
						
							|  |  |  |               .toThrowError(new RegExp('identifier or keyword')); | 
					
						
							|  |  |  |           expect(() => parseBinding('"Foo"|"uppercase"')) | 
					
						
							|  |  |  |               .toThrowError(new RegExp('identifier or keyword')); | 
					
						
							| 
									
										
										
										
											2014-11-05 10:00:19 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-04 19:06:09 +02:00
										 |  |  |         it('should parse pipes', () => { | 
					
						
							|  |  |  |           let unparser = new Unparser(); | 
					
						
							|  |  |  |           let exps = [ | 
					
						
							|  |  |  |             ['a(b | c)', 'a((b | c))'], | 
					
						
							|  |  |  |             ['a.b(c.d(e) | f)', 'a.b((c.d(e) | f))'], | 
					
						
							|  |  |  |             ['[1, 2, 3] | a', '([1, 2, 3] | a)'], | 
					
						
							|  |  |  |             ['{a: 1} | b', '({a: 1} | b)'], | 
					
						
							|  |  |  |             ['a[b] | c', '(a[b] | c)'], | 
					
						
							|  |  |  |             ['a?.b | c', '(a?.b | c)'], | 
					
						
							|  |  |  |             ['true | a', '(true | a)'], | 
					
						
							|  |  |  |             ['a | b:c | d', '(a | b:(c | d))'], | 
					
						
							|  |  |  |             ['(a | b:c) | d', '((a | b:c) | d)'] | 
					
						
							|  |  |  |           ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           ListWrapper.forEach(exps, e => { | 
					
						
							|  |  |  |             var ast = parseBinding(e[0]).ast; | 
					
						
							|  |  |  |             expect(unparser.unparse(ast)).toEqual(e[1]); | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |       it('should store the source in the result', | 
					
						
							|  |  |  |          () => { expect(parseBinding('someExpr').source).toBe('someExpr'); }); | 
					
						
							| 
									
										
										
										
											2014-12-10 19:21:15 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |       it('should store the passed-in location', | 
					
						
							|  |  |  |          () => { expect(parseBinding('someExpr', 'location').location).toBe('location'); }); | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should throw on chain expressions', () => { | 
					
						
							|  |  |  |         expect(() => parseBinding("1;2")).toThrowError(new RegExp("contain chained expression")); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-04 19:06:09 +02:00
										 |  |  |       it('should throw on assignment', () => { | 
					
						
							| 
									
										
										
										
											2014-11-06 12:00:09 -08:00
										 |  |  |         expect(() => parseBinding("1;2")).toThrowError(new RegExp("contain chained expression")); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-10-28 12:22:38 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     describe('parseTemplateBindings', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       function keys(templateBindings) { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         return ListWrapper.map(templateBindings, (binding) => binding.key); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-27 22:34:25 -08:00
										 |  |  |       function keyValues(templateBindings) { | 
					
						
							|  |  |  |         return ListWrapper.map(templateBindings, (binding) => { | 
					
						
							|  |  |  |           if (binding.keyIsVar) { | 
					
						
							| 
									
										
										
										
											2015-05-21 12:17:46 -07:00
										 |  |  |             return '#' + binding.key + (isBlank(binding.name) ? '=null' : '=' + binding.name); | 
					
						
							| 
									
										
										
										
											2015-01-27 22:34:25 -08:00
										 |  |  |           } else { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |             return binding.key + (isBlank(binding.expression) ? '' : `=${binding.expression}`) | 
					
						
							| 
									
										
										
										
											2015-01-27 22:34:25 -08:00
										 |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  |       function exprSources(templateBindings) { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         return ListWrapper.map(templateBindings, (binding) => isPresent(binding.expression) ? | 
					
						
							| 
									
										
										
										
											2015-06-12 07:50:45 -07:00
										 |  |  |                                                                   binding.expression.source : | 
					
						
							|  |  |  |                                                                   null); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       function exprAsts(templateBindings) { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         return ListWrapper.map(templateBindings, (binding) => isPresent(binding.expression) ? | 
					
						
							| 
									
										
										
										
											2015-06-12 07:50:45 -07:00
										 |  |  |                                                                   binding.expression : | 
					
						
							|  |  |  |                                                                   null); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should parse an empty string', () => { | 
					
						
							| 
									
										
										
										
											2014-11-25 18:38:28 -08:00
										 |  |  |         var bindings = parseTemplateBindings(''); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  |         expect(bindings).toEqual([]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-21 12:17:46 -07:00
										 |  |  |       it('should parse a string without a value', () => { | 
					
						
							| 
									
										
										
										
											2014-12-01 16:05:44 -08:00
										 |  |  |         var bindings = parseTemplateBindings('a'); | 
					
						
							|  |  |  |         expect(keys(bindings)).toEqual(['a']); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should only allow identifier, string, or keyword including dashes as keys', () => { | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08: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 16:05:44 -08:00
										 |  |  |         bindings = parseTemplateBindings("a-b:'c'"); | 
					
						
							|  |  |  |         expect(keys(bindings)).toEqual(['a-b']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         expect(() => { parseTemplateBindings('(:0'); }) | 
					
						
							|  |  |  |             .toThrowError(new RegExp('expected identifier, keyword, or string')); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         expect(() => { parseTemplateBindings('1234:0'); }) | 
					
						
							|  |  |  |             .toThrowError(new RegExp('expected identifier, keyword, or string')); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       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"); | 
					
						
							| 
									
										
										
										
											2015-05-21 12:17:46 -07:00
										 |  |  |         expect(keyValues(bindings)).toEqual(['a', '#b=\$implicit']); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       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"); | 
					
						
							| 
									
										
										
										
											2015-05-11 17:04:55 -07:00
										 |  |  |         expect(keys(bindings)).toEqual(['a', 'a-b']); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  |         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-12-10 19:21:15 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should store the passed-in location', () => { | 
					
						
							|  |  |  |         var bindings = parseTemplateBindings("a 1,b 2", 'location'); | 
					
						
							|  |  |  |         expect(bindings[0].expression.location).toEqual('location'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-01-27 22:34:25 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should support var/# notation', () => { | 
					
						
							|  |  |  |         var bindings = parseTemplateBindings("var i"); | 
					
						
							| 
									
										
										
										
											2015-05-21 12:17:46 -07:00
										 |  |  |         expect(keyValues(bindings)).toEqual(['#i=\$implicit']); | 
					
						
							| 
									
										
										
										
											2015-01-27 22:34:25 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         bindings = parseTemplateBindings("#i"); | 
					
						
							| 
									
										
										
										
											2015-05-21 12:17:46 -07:00
										 |  |  |         expect(keyValues(bindings)).toEqual(['#i=\$implicit']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bindings = parseTemplateBindings("var a; var b"); | 
					
						
							|  |  |  |         expect(keyValues(bindings)).toEqual(['#a=\$implicit', '#b=\$implicit']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bindings = parseTemplateBindings("#a; #b;"); | 
					
						
							|  |  |  |         expect(keyValues(bindings)).toEqual(['#a=\$implicit', '#b=\$implicit']); | 
					
						
							| 
									
										
										
										
											2015-01-27 22:34:25 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         bindings = parseTemplateBindings("var i-a = k-a"); | 
					
						
							|  |  |  |         expect(keyValues(bindings)).toEqual(['#i-a=k-a']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bindings = parseTemplateBindings("keyword var item; var i = k"); | 
					
						
							|  |  |  |         expect(keyValues(bindings)).toEqual(['keyword', '#item=\$implicit', '#i=k']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bindings = parseTemplateBindings("keyword: #item; #i = k"); | 
					
						
							|  |  |  |         expect(keyValues(bindings)).toEqual(['keyword', '#item=\$implicit', '#i=k']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bindings = parseTemplateBindings("directive: var item in expr; var a = b", 'location'); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         expect(keyValues(bindings)) | 
					
						
							|  |  |  |             .toEqual(['directive', '#item=\$implicit', 'directive-in=expr in location', '#a=b']); | 
					
						
							| 
									
										
										
										
											2015-01-27 22:34:25 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-02-23 13:12:49 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should parse pipes', () => { | 
					
						
							|  |  |  |         var bindings = parseTemplateBindings('key value|pipe'); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         var ast = bindings[0].expression.ast; | 
					
						
							| 
									
										
										
										
											2015-02-23 13:12:49 -08:00
										 |  |  |         expect(ast).toBeAnInstanceOf(Pipe); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-11-18 16:38:36 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-01-08 16:17:56 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     describe('parseInterpolation', () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |       it('should return null if no interpolation', | 
					
						
							|  |  |  |          () => { expect(parseInterpolation('nothing')).toBe(null); }); | 
					
						
							| 
									
										
										
										
											2015-01-08 16:17:56 -08: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 19:06:09 +02:00
										 |  |  |         var originalExp = 'before {{ a }} middle {{ b }} after'; | 
					
						
							|  |  |  |         var ast = parseInterpolation(originalExp).ast; | 
					
						
							|  |  |  |         expect(new Unparser().unparse(ast)).toEqual(originalExp); | 
					
						
							| 
									
										
										
										
											2015-01-08 16:17:56 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-02-05 20:13:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-19 17:47:25 -08:00
										 |  |  |     describe('addPipes', () => { | 
					
						
							|  |  |  |       it('should return the given ast whe the list of pipes is empty', () => { | 
					
						
							|  |  |  |         var ast = parseBinding("1 + 1", "Location"); | 
					
						
							|  |  |  |         var transformedAst = addPipes(ast, []); | 
					
						
							|  |  |  |         expect(transformedAst).toBe(ast); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should append pipe ast nodes', () => { | 
					
						
							|  |  |  |         var ast = parseBinding("1 + 1", "Location"); | 
					
						
							|  |  |  |         var transformedAst = addPipes(ast, ['one', 'two']); | 
					
						
							|  |  |  |         expect(transformedAst.ast.name).toEqual("two"); | 
					
						
							|  |  |  |         expect(transformedAst.ast.exp.name).toEqual("one"); | 
					
						
							|  |  |  |         expect(transformedAst.ast.exp.exp.operation).toEqual("+"); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should preserve location and source', () => { | 
					
						
							|  |  |  |         var ast = parseBinding("1 + 1", "Location"); | 
					
						
							|  |  |  |         var transformedAst = addPipes(ast, ['one', 'two']); | 
					
						
							|  |  |  |         expect(transformedAst.source).toEqual("1 + 1"); | 
					
						
							|  |  |  |         expect(transformedAst.location).toEqual("Location"); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-05 20:13:32 +01:00
										 |  |  |     describe('wrapLiteralPrimitive', () => { | 
					
						
							|  |  |  |       it('should wrap a literal primitive', () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |         expect(createParser().wrapLiteralPrimitive("foo", null).eval(null, emptyLocals())) | 
					
						
							|  |  |  |             .toEqual("foo"); | 
					
						
							| 
									
										
										
										
											2015-02-05 20:13:32 +01:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2014-10-28 12:22:38 -04:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-04-22 11:45:33 +02:00
										 |  |  | } |