PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/ng/parseSpec.js

https://github.com/9kopb/angular.js
JavaScript | 850 lines | 836 code | 14 blank | 0 comment | 4 complexity | 3e553e79cbe53a5a27dba3940599a3a3 MD5 | raw file
Possible License(s): JSON, Apache-2.0
  1. 'use strict';
  2. describe('parser', function() {
  3. describe('lexer', function() {
  4. it('should tokenize a string', function() {
  5. var tokens = lex("a.bc[22]+1.3|f:'a\\\'c':\"d\\\"e\"");
  6. var i = 0;
  7. expect(tokens[i].index).toEqual(0);
  8. expect(tokens[i].text).toEqual('a.bc');
  9. i++;
  10. expect(tokens[i].index).toEqual(4);
  11. expect(tokens[i].text).toEqual('[');
  12. i++;
  13. expect(tokens[i].index).toEqual(5);
  14. expect(tokens[i].text).toEqual(22);
  15. i++;
  16. expect(tokens[i].index).toEqual(7);
  17. expect(tokens[i].text).toEqual(']');
  18. i++;
  19. expect(tokens[i].index).toEqual(8);
  20. expect(tokens[i].text).toEqual('+');
  21. i++;
  22. expect(tokens[i].index).toEqual(9);
  23. expect(tokens[i].text).toEqual(1.3);
  24. i++;
  25. expect(tokens[i].index).toEqual(12);
  26. expect(tokens[i].text).toEqual('|');
  27. i++;
  28. expect(tokens[i].index).toEqual(13);
  29. expect(tokens[i].text).toEqual('f');
  30. i++;
  31. expect(tokens[i].index).toEqual(14);
  32. expect(tokens[i].text).toEqual(':');
  33. i++;
  34. expect(tokens[i].index).toEqual(15);
  35. expect(tokens[i].string).toEqual("a'c");
  36. i++;
  37. expect(tokens[i].index).toEqual(21);
  38. expect(tokens[i].text).toEqual(':');
  39. i++;
  40. expect(tokens[i].index).toEqual(22);
  41. expect(tokens[i].string).toEqual('d"e');
  42. });
  43. it('should tokenize undefined', function() {
  44. var tokens = lex("undefined");
  45. var i = 0;
  46. expect(tokens[i].index).toEqual(0);
  47. expect(tokens[i].text).toEqual('undefined');
  48. expect(undefined).toEqual(tokens[i].fn());
  49. });
  50. it('should tokenize quoted string', function() {
  51. var str = "['\\'', \"\\\"\"]";
  52. var tokens = lex(str);
  53. expect(tokens[1].index).toEqual(1);
  54. expect(tokens[1].string).toEqual("'");
  55. expect(tokens[3].index).toEqual(7);
  56. expect(tokens[3].string).toEqual('"');
  57. });
  58. it('should tokenize escaped quoted string', function() {
  59. var str = '"\\"\\n\\f\\r\\t\\v\\u00A0"';
  60. var tokens = lex(str);
  61. expect(tokens[0].string).toEqual('"\n\f\r\t\v\u00A0');
  62. });
  63. it('should tokenize unicode', function() {
  64. var tokens = lex('"\\u00A0"');
  65. expect(tokens.length).toEqual(1);
  66. expect(tokens[0].string).toEqual('\u00a0');
  67. });
  68. it('should ignore whitespace', function() {
  69. var tokens = lex("a \t \n \r b");
  70. expect(tokens[0].text).toEqual('a');
  71. expect(tokens[1].text).toEqual('b');
  72. });
  73. it('should tokenize relation and equality', function() {
  74. var tokens = lex("! == != < > <= >= === !==");
  75. expect(tokens[0].text).toEqual('!');
  76. expect(tokens[1].text).toEqual('==');
  77. expect(tokens[2].text).toEqual('!=');
  78. expect(tokens[3].text).toEqual('<');
  79. expect(tokens[4].text).toEqual('>');
  80. expect(tokens[5].text).toEqual('<=');
  81. expect(tokens[6].text).toEqual('>=');
  82. expect(tokens[7].text).toEqual('===');
  83. expect(tokens[8].text).toEqual('!==');
  84. });
  85. it('should tokenize statements', function() {
  86. var tokens = lex("a;b;");
  87. expect(tokens[0].text).toEqual('a');
  88. expect(tokens[1].text).toEqual(';');
  89. expect(tokens[2].text).toEqual('b');
  90. expect(tokens[3].text).toEqual(';');
  91. });
  92. it('should tokenize function invocation', function() {
  93. var tokens = lex("a()")
  94. expect(map(tokens, function(t) { return t.text;})).toEqual(['a', '(', ')']);
  95. });
  96. it('should tokenize method invocation', function() {
  97. var tokens = lex("a.b.c (d) - e.f()");
  98. expect(map(tokens, function(t) { return t.text;})).
  99. toEqual(['a.b', '.', 'c', '(', 'd', ')', '-', 'e', '.', 'f', '(', ')']);
  100. });
  101. it('should tokenize number', function() {
  102. var tokens = lex("0.5");
  103. expect(tokens[0].text).toEqual(0.5);
  104. });
  105. it('should tokenize negative number', inject(function($rootScope) {
  106. var value = $rootScope.$eval("-0.5");
  107. expect(value).toEqual(-0.5);
  108. value = $rootScope.$eval("{a:-0.5}");
  109. expect(value).toEqual({a:-0.5});
  110. }));
  111. it('should tokenize number with exponent', inject(function($rootScope) {
  112. var tokens = lex("0.5E-10");
  113. expect(tokens[0].text).toEqual(0.5E-10);
  114. expect($rootScope.$eval("0.5E-10")).toEqual(0.5E-10);
  115. tokens = lex("0.5E+10");
  116. expect(tokens[0].text).toEqual(0.5E+10);
  117. }));
  118. it('should throws exception for invalid exponent', function() {
  119. expect(function() {
  120. lex("0.5E-");
  121. }).toThrow(new Error('Lexer Error: Invalid exponent at column 4 in expression [0.5E-].'));
  122. expect(function() {
  123. lex("0.5E-A");
  124. }).toThrow(new Error('Lexer Error: Invalid exponent at column 4 in expression [0.5E-A].'));
  125. });
  126. it('should tokenize number starting with a dot', function() {
  127. var tokens = lex(".5");
  128. expect(tokens[0].text).toEqual(0.5);
  129. });
  130. it('should throw error on invalid unicode', function() {
  131. expect(function() {
  132. lex("'\\u1''bla'");
  133. }).toThrow(new Error("Lexer Error: Invalid unicode escape [\\u1''b] at column 2 in expression ['\\u1''bla']."));
  134. });
  135. });
  136. var $filterProvider, scope;
  137. beforeEach(module(['$filterProvider', function (filterProvider) {
  138. $filterProvider = filterProvider;
  139. }]));
  140. forEach([true, false], function(cspEnabled) {
  141. describe('csp ' + cspEnabled, function() {
  142. beforeEach(inject(function ($rootScope, $sniffer) {
  143. scope = $rootScope;
  144. $sniffer.csp = cspEnabled;
  145. getterFnCache = {}; // clear cache
  146. }));
  147. it('should parse expressions', function() {
  148. expect(scope.$eval("-1")).toEqual(-1);
  149. expect(scope.$eval("1 + 2.5")).toEqual(3.5);
  150. expect(scope.$eval("1 + -2.5")).toEqual(-1.5);
  151. expect(scope.$eval("1+2*3/4")).toEqual(1+2*3/4);
  152. expect(scope.$eval("0--1+1.5")).toEqual(0- -1 + 1.5);
  153. expect(scope.$eval("-0--1++2*-3/-4")).toEqual(-0- -1+ +2*-3/-4);
  154. expect(scope.$eval("1/2*3")).toEqual(1/2*3);
  155. });
  156. it('should parse comparison', function() {
  157. expect(scope.$eval("false")).toBeFalsy();
  158. expect(scope.$eval("!true")).toBeFalsy();
  159. expect(scope.$eval("1==1")).toBeTruthy();
  160. expect(scope.$eval("1==true")).toBeTruthy();
  161. expect(scope.$eval("1===1")).toBeTruthy();
  162. expect(scope.$eval("1==='1'")).toBeFalsy();
  163. expect(scope.$eval("1===true")).toBeFalsy();
  164. expect(scope.$eval("'true'===true")).toBeFalsy();
  165. expect(scope.$eval("1!==2")).toBeTruthy();
  166. expect(scope.$eval("1!=='1'")).toBeTruthy();
  167. expect(scope.$eval("1!=2")).toBeTruthy();
  168. expect(scope.$eval("1<2")).toBeTruthy();
  169. expect(scope.$eval("1<=1")).toBeTruthy();
  170. expect(scope.$eval("1>2")).toEqual(1>2);
  171. expect(scope.$eval("2>=1")).toEqual(2>=1);
  172. expect(scope.$eval("true==2<3")).toEqual(true == 2<3);
  173. expect(scope.$eval("true===2<3")).toEqual(true === 2<3);
  174. });
  175. it('should parse logical', function() {
  176. expect(scope.$eval("0&&2")).toEqual(0&&2);
  177. expect(scope.$eval("0||2")).toEqual(0||2);
  178. expect(scope.$eval("0||1&&2")).toEqual(0||1&&2);
  179. });
  180. it('should parse string', function() {
  181. expect(scope.$eval("'a' + 'b c'")).toEqual("ab c");
  182. });
  183. it('should parse filters', function() {
  184. $filterProvider.register('substring', valueFn(function(input, start, end) {
  185. return input.substring(start, end);
  186. }));
  187. expect(function() {
  188. scope.$eval("1|nonexistent");
  189. }).toThrow(new Error("Unknown provider: nonexistentFilterProvider <- nonexistentFilter"));
  190. scope.offset = 3;
  191. expect(scope.$eval("'abcd'|substring:1:offset")).toEqual("bc");
  192. expect(scope.$eval("'abcd'|substring:1:3|uppercase")).toEqual("BC");
  193. });
  194. it('should access scope', function() {
  195. scope.a = 123;
  196. scope.b = {c: 456};
  197. expect(scope.$eval("a", scope)).toEqual(123);
  198. expect(scope.$eval("b.c", scope)).toEqual(456);
  199. expect(scope.$eval("x.y.z", scope)).not.toBeDefined();
  200. });
  201. it('should resolve deeply nested paths (important for CSP mode)', function() {
  202. scope.a = {b: {c: {d: {e: {f: {g: {h: {i: {j: {k: {l: {m: {n: 'nooo!'}}}}}}}}}}}}};
  203. expect(scope.$eval("a.b.c.d.e.f.g.h.i.j.k.l.m.n", scope)).toBe('nooo!');
  204. });
  205. it('should be forgiving', function() {
  206. scope.a = {b: 23};
  207. expect(scope.$eval('b')).toBeUndefined();
  208. expect(scope.$eval('a.x')).toBeUndefined();
  209. expect(scope.$eval('a.b.c.d')).toBeUndefined();
  210. });
  211. it('should support property names that collide with native object properties', function() {
  212. // regression
  213. scope.watch = 1;
  214. scope.constructor = 2;
  215. scope.toString = 3;
  216. expect(scope.$eval('watch', scope)).toBe(1);
  217. expect(scope.$eval('constructor', scope)).toBe(2);
  218. expect(scope.$eval('toString', scope)).toBe(3);
  219. });
  220. it('should evaluate grouped expressions', function() {
  221. expect(scope.$eval("(1+2)*3")).toEqual((1+2)*3);
  222. });
  223. it('should evaluate assignments', function() {
  224. expect(scope.$eval("a=12")).toEqual(12);
  225. expect(scope.a).toEqual(12);
  226. expect(scope.$eval("x.y.z=123;")).toEqual(123);
  227. expect(scope.x.y.z).toEqual(123);
  228. expect(scope.$eval("a=123; b=234")).toEqual(234);
  229. expect(scope.a).toEqual(123);
  230. expect(scope.b).toEqual(234);
  231. });
  232. it('should evaluate function call without arguments', function() {
  233. scope['const'] = function(a,b){return 123;};
  234. expect(scope.$eval("const()")).toEqual(123);
  235. });
  236. it('should evaluate function call with arguments', function() {
  237. scope.add = function(a,b) {
  238. return a+b;
  239. };
  240. expect(scope.$eval("add(1,2)")).toEqual(3);
  241. });
  242. it('should evaluate function call from a return value', function() {
  243. scope.val = 33;
  244. scope.getter = function() { return function() { return this.val; }};
  245. expect(scope.$eval("getter()()")).toBe(33);
  246. });
  247. it('should evaluate multiplication and division', function() {
  248. scope.taxRate = 8;
  249. scope.subTotal = 100;
  250. expect(scope.$eval("taxRate / 100 * subTotal")).toEqual(8);
  251. expect(scope.$eval("subTotal * taxRate / 100")).toEqual(8);
  252. });
  253. it('should evaluate array', function() {
  254. expect(scope.$eval("[]").length).toEqual(0);
  255. expect(scope.$eval("[1, 2]").length).toEqual(2);
  256. expect(scope.$eval("[1, 2]")[0]).toEqual(1);
  257. expect(scope.$eval("[1, 2]")[1]).toEqual(2);
  258. });
  259. it('should evaluate array access', function() {
  260. expect(scope.$eval("[1][0]")).toEqual(1);
  261. expect(scope.$eval("[[1]][0][0]")).toEqual(1);
  262. expect(scope.$eval("[].length")).toEqual(0);
  263. expect(scope.$eval("[1, 2].length")).toEqual(2);
  264. });
  265. it('should evaluate object', function() {
  266. expect(toJson(scope.$eval("{}"))).toEqual("{}");
  267. expect(toJson(scope.$eval("{a:'b'}"))).toEqual('{"a":"b"}');
  268. expect(toJson(scope.$eval("{'a':'b'}"))).toEqual('{"a":"b"}');
  269. expect(toJson(scope.$eval("{\"a\":'b'}"))).toEqual('{"a":"b"}');
  270. });
  271. it('should evaluate object access', function() {
  272. expect(scope.$eval("{false:'WC', true:'CC'}[false]")).toEqual("WC");
  273. });
  274. it('should evaluate JSON', function() {
  275. expect(toJson(scope.$eval("[{}]"))).toEqual("[{}]");
  276. expect(toJson(scope.$eval("[{a:[]}, {b:1}]"))).toEqual('[{"a":[]},{"b":1}]');
  277. });
  278. it('should evaluate multiple statements', function() {
  279. expect(scope.$eval("a=1;b=3;a+b")).toEqual(4);
  280. expect(scope.$eval(";;1;;")).toEqual(1);
  281. });
  282. it('should evaluate object methods in correct context (this)', function() {
  283. var C = function () {
  284. this.a = 123;
  285. };
  286. C.prototype.getA = function() {
  287. return this.a;
  288. };
  289. scope.obj = new C();
  290. expect(scope.$eval("obj.getA()")).toEqual(123);
  291. expect(scope.$eval("obj['getA']()")).toEqual(123);
  292. });
  293. it('should evaluate methods in correct context (this) in argument', function() {
  294. var C = function () {
  295. this.a = 123;
  296. };
  297. C.prototype.sum = function(value) {
  298. return this.a + value;
  299. };
  300. C.prototype.getA = function() {
  301. return this.a;
  302. };
  303. scope.obj = new C();
  304. expect(scope.$eval("obj.sum(obj.getA())")).toEqual(246);
  305. expect(scope.$eval("obj['sum'](obj.getA())")).toEqual(246);
  306. });
  307. it('should evaluate objects on scope context', function() {
  308. scope.a = "abc";
  309. expect(scope.$eval("{a:a}").a).toEqual("abc");
  310. });
  311. it('should evaluate field access on function call result', function() {
  312. scope.a = function() {
  313. return {name:'misko'};
  314. };
  315. expect(scope.$eval("a().name")).toEqual("misko");
  316. });
  317. it('should evaluate field access after array access', function () {
  318. scope.items = [{}, {name:'misko'}];
  319. expect(scope.$eval('items[1].name')).toEqual("misko");
  320. });
  321. it('should evaluate array assignment', function() {
  322. scope.items = [];
  323. expect(scope.$eval('items[1] = "abc"')).toEqual("abc");
  324. expect(scope.$eval('items[1]')).toEqual("abc");
  325. // Dont know how to make this work....
  326. // expect(scope.$eval('books[1] = "moby"')).toEqual("moby");
  327. // expect(scope.$eval('books[1]')).toEqual("moby");
  328. });
  329. it('should evaluate grouped filters', function() {
  330. scope.name = 'MISKO';
  331. expect(scope.$eval('n = (name|lowercase)')).toEqual('misko');
  332. expect(scope.$eval('n')).toEqual('misko');
  333. });
  334. it('should evaluate remainder', function() {
  335. expect(scope.$eval('1%2')).toEqual(1);
  336. });
  337. it('should evaluate sum with undefined', function() {
  338. expect(scope.$eval('1+undefined')).toEqual(1);
  339. expect(scope.$eval('undefined+1')).toEqual(1);
  340. });
  341. it('should throw exception on non-closed bracket', function() {
  342. expect(function() {
  343. scope.$eval('[].count(');
  344. }).toThrow('Unexpected end of expression: [].count(');
  345. });
  346. it('should evaluate double negation', function() {
  347. expect(scope.$eval('true')).toBeTruthy();
  348. expect(scope.$eval('!true')).toBeFalsy();
  349. expect(scope.$eval('!!true')).toBeTruthy();
  350. expect(scope.$eval('{true:"a", false:"b"}[!!true]')).toEqual('a');
  351. });
  352. it('should evaluate negation', function() {
  353. expect(scope.$eval("!false || true")).toEqual(!false || true);
  354. expect(scope.$eval("!11 == 10")).toEqual(!11 == 10);
  355. expect(scope.$eval("12/6/2")).toEqual(12/6/2);
  356. });
  357. it('should evaluate exclamation mark', function() {
  358. expect(scope.$eval('suffix = "!"')).toEqual('!');
  359. });
  360. it('should evaluate minus', function() {
  361. expect(scope.$eval("{a:'-'}")).toEqual({a: "-"});
  362. });
  363. it('should evaluate undefined', function() {
  364. expect(scope.$eval("undefined")).not.toBeDefined();
  365. expect(scope.$eval("a=undefined")).not.toBeDefined();
  366. expect(scope.a).not.toBeDefined();
  367. });
  368. it('should allow assignment after array dereference', function() {
  369. scope.obj = [{}];
  370. scope.$eval('obj[0].name=1');
  371. expect(scope.obj.name).toBeUndefined();
  372. expect(scope.obj[0].name).toEqual(1);
  373. });
  374. it('should short-circuit AND operator', function() {
  375. scope.run = function() {
  376. throw "IT SHOULD NOT HAVE RUN";
  377. };
  378. expect(scope.$eval('false && run()')).toBe(false);
  379. });
  380. it('should short-circuit OR operator', function() {
  381. scope.run = function() {
  382. throw "IT SHOULD NOT HAVE RUN";
  383. };
  384. expect(scope.$eval('true || run()')).toBe(true);
  385. });
  386. it('should support method calls on primitive types', function() {
  387. scope.empty = '';
  388. scope.zero = 0;
  389. scope.bool = false;
  390. expect(scope.$eval('empty.substr(0)')).toBe('');
  391. expect(scope.$eval('zero.toString()')).toBe('0');
  392. expect(scope.$eval('bool.toString()')).toBe('false');
  393. });
  394. it('should call the function from the received instance and not from a new one', function() {
  395. var n = 0;
  396. scope.fn = function() {
  397. var c = n++;
  398. return { c: c, anotherFn: function() { return this.c == c; } };
  399. };
  400. expect(scope.$eval('fn().anotherFn()')).toBe(true);
  401. });
  402. it('should call the function once when it is part of the context', function() {
  403. var count = 0;
  404. scope.fn = function() {
  405. count++;
  406. return { anotherFn: function() { return "lucas"; } };
  407. };
  408. expect(scope.$eval('fn().anotherFn()')).toBe('lucas');
  409. expect(count).toBe(1);
  410. });
  411. it('should call the function once when it is not part of the context', function() {
  412. var count = 0;
  413. scope.fn = function() {
  414. count++;
  415. return function() { return 'lucas'; };
  416. };
  417. expect(scope.$eval('fn()()')).toBe('lucas');
  418. expect(count).toBe(1);
  419. });
  420. it('should call the function once when it is not part of the context', function() {
  421. var count = 0;
  422. scope.fn = function() {
  423. count++;
  424. return function() { return 'lucas'; };
  425. };
  426. expect(scope.$eval('fn()()')).toBe('lucas');
  427. expect(count).toBe(1);
  428. });
  429. it('should call the function once when it is part of the context on assignments', function() {
  430. var count = 0;
  431. var element = {};
  432. scope.fn = function() {
  433. count++;
  434. return element;
  435. };
  436. expect(scope.$eval('fn().name = "lucas"')).toBe('lucas');
  437. expect(element.name).toBe('lucas');
  438. expect(count).toBe(1);
  439. });
  440. it('should call the function once when it is part of the context on array lookups', function() {
  441. var count = 0;
  442. var element = [];
  443. scope.fn = function() {
  444. count++;
  445. return element;
  446. };
  447. expect(scope.$eval('fn()[0] = "lucas"')).toBe('lucas');
  448. expect(element[0]).toBe('lucas');
  449. expect(count).toBe(1);
  450. });
  451. it('should call the function once when it is part of the context on array lookup function', function() {
  452. var count = 0;
  453. var element = [{anotherFn: function() { return 'lucas';} }];
  454. scope.fn = function() {
  455. count++;
  456. return element;
  457. };
  458. expect(scope.$eval('fn()[0].anotherFn()')).toBe('lucas');
  459. expect(count).toBe(1);
  460. });
  461. it('should call the function once when it is part of the context on array lookup function', function() {
  462. var count = 0;
  463. var element = {name: {anotherFn: function() { return 'lucas';} } };
  464. scope.fn = function() {
  465. count++;
  466. return element;
  467. };
  468. expect(scope.$eval('fn().name.anotherFn()')).toBe('lucas');
  469. expect(count).toBe(1);
  470. });
  471. it('should call the function once when it is part of a sub-expression', function() {
  472. var count = 0;
  473. scope.element = [{}];
  474. scope.fn = function() {
  475. count++;
  476. return 0;
  477. };
  478. expect(scope.$eval('element[fn()].name = "lucas"')).toBe('lucas');
  479. expect(scope.element[0].name).toBe('lucas');
  480. expect(count).toBe(1);
  481. });
  482. describe('promises', function() {
  483. var deferred, promise, q;
  484. beforeEach(inject(function($q) {
  485. q = $q;
  486. deferred = q.defer();
  487. promise = deferred.promise;
  488. }));
  489. describe('{{promise}}', function() {
  490. it('should evaluated resolved promise and get its value', function() {
  491. deferred.resolve('hello!');
  492. scope.greeting = promise;
  493. expect(scope.$eval('greeting')).toBe(undefined);
  494. scope.$digest();
  495. expect(scope.$eval('greeting')).toBe('hello!');
  496. });
  497. it('should evaluated rejected promise and ignore the rejection reason', function() {
  498. deferred.reject('sorry');
  499. scope.greeting = promise;
  500. expect(scope.$eval('gretting')).toBe(undefined);
  501. scope.$digest();
  502. expect(scope.$eval('greeting')).toBe(undefined);
  503. });
  504. it('should evaluate a promise and eventualy get its value', function() {
  505. scope.greeting = promise;
  506. expect(scope.$eval('greeting')).toBe(undefined);
  507. scope.$digest();
  508. expect(scope.$eval('greeting')).toBe(undefined);
  509. deferred.resolve('hello!');
  510. expect(scope.$eval('greeting')).toBe(undefined);
  511. scope.$digest();
  512. expect(scope.$eval('greeting')).toBe('hello!');
  513. });
  514. it('should evaluate a promise and eventualy ignore its rejection', function() {
  515. scope.greeting = promise;
  516. expect(scope.$eval('greeting')).toBe(undefined);
  517. scope.$digest();
  518. expect(scope.$eval('greeting')).toBe(undefined);
  519. deferred.reject('sorry');
  520. expect(scope.$eval('greeting')).toBe(undefined);
  521. scope.$digest();
  522. expect(scope.$eval('greeting')).toBe(undefined);
  523. });
  524. });
  525. describe('dereferencing', function() {
  526. it('should evaluate and dereference properties leading to and from a promise', function() {
  527. scope.obj = {greeting: promise};
  528. expect(scope.$eval('obj.greeting')).toBe(undefined);
  529. expect(scope.$eval('obj.greeting.polite')).toBe(undefined);
  530. scope.$digest();
  531. expect(scope.$eval('obj.greeting')).toBe(undefined);
  532. expect(scope.$eval('obj.greeting.polite')).toBe(undefined);
  533. deferred.resolve({polite: 'Good morning!'});
  534. scope.$digest();
  535. expect(scope.$eval('obj.greeting')).toEqual({polite: 'Good morning!'});
  536. expect(scope.$eval('obj.greeting.polite')).toBe('Good morning!');
  537. });
  538. it('should evaluate and dereference properties leading to and from a promise via bracket ' +
  539. 'notation', function() {
  540. scope.obj = {greeting: promise};
  541. expect(scope.$eval('obj["greeting"]')).toBe(undefined);
  542. expect(scope.$eval('obj["greeting"]["polite"]')).toBe(undefined);
  543. scope.$digest();
  544. expect(scope.$eval('obj["greeting"]')).toBe(undefined);
  545. expect(scope.$eval('obj["greeting"]["polite"]')).toBe(undefined);
  546. deferred.resolve({polite: 'Good morning!'});
  547. scope.$digest();
  548. expect(scope.$eval('obj["greeting"]')).toEqual({polite: 'Good morning!'});
  549. expect(scope.$eval('obj["greeting"]["polite"]')).toBe('Good morning!');
  550. });
  551. it('should evaluate and dereference array references leading to and from a promise',
  552. function() {
  553. scope.greetings = [promise];
  554. expect(scope.$eval('greetings[0]')).toBe(undefined);
  555. expect(scope.$eval('greetings[0][0]')).toBe(undefined);
  556. scope.$digest();
  557. expect(scope.$eval('greetings[0]')).toBe(undefined);
  558. expect(scope.$eval('greetings[0][0]')).toBe(undefined);
  559. deferred.resolve(['Hi!', 'Cau!']);
  560. scope.$digest();
  561. expect(scope.$eval('greetings[0]')).toEqual(['Hi!', 'Cau!']);
  562. expect(scope.$eval('greetings[0][0]')).toBe('Hi!');
  563. });
  564. it('should evaluate and dereference promises used as function arguments', function() {
  565. scope.greet = function(name) { return 'Hi ' + name + '!'; };
  566. scope.name = promise;
  567. expect(scope.$eval('greet(name)')).toBe('Hi undefined!');
  568. scope.$digest();
  569. expect(scope.$eval('greet(name)')).toBe('Hi undefined!');
  570. deferred.resolve('Veronica');
  571. expect(scope.$eval('greet(name)')).toBe('Hi undefined!');
  572. scope.$digest();
  573. expect(scope.$eval('greet(name)')).toBe('Hi Veronica!');
  574. });
  575. it('should evaluate and dereference promises used as array indexes', function() {
  576. scope.childIndex = promise;
  577. scope.kids = ['Adam', 'Veronica', 'Elisa'];
  578. expect(scope.$eval('kids[childIndex]')).toBe(undefined);
  579. scope.$digest();
  580. expect(scope.$eval('kids[childIndex]')).toBe(undefined);
  581. deferred.resolve(1);
  582. expect(scope.$eval('kids[childIndex]')).toBe(undefined);
  583. scope.$digest();
  584. expect(scope.$eval('kids[childIndex]')).toBe('Veronica');
  585. });
  586. it('should evaluate and dereference promises used as keys in bracket notation', function() {
  587. scope.childKey = promise;
  588. scope.kids = {'a': 'Adam', 'v': 'Veronica', 'e': 'Elisa'};
  589. expect(scope.$eval('kids[childKey]')).toBe(undefined);
  590. scope.$digest();
  591. expect(scope.$eval('kids[childKey]')).toBe(undefined);
  592. deferred.resolve('v');
  593. expect(scope.$eval('kids[childKey]')).toBe(undefined);
  594. scope.$digest();
  595. expect(scope.$eval('kids[childKey]')).toBe('Veronica');
  596. });
  597. it('should not mess with the promise if it was not directly evaluated', function() {
  598. scope.obj = {greeting: promise, username: 'hi'};
  599. var obj = scope.$eval('obj');
  600. expect(obj.username).toEqual('hi');
  601. expect(typeof obj.greeting.then).toBe('function');
  602. });
  603. });
  604. });
  605. describe('assignable', function() {
  606. it('should expose assignment function', inject(function($parse) {
  607. var fn = $parse('a');
  608. expect(fn.assign).toBeTruthy();
  609. var scope = {};
  610. fn.assign(scope, 123);
  611. expect(scope).toEqual({a:123});
  612. }));
  613. });
  614. describe('locals', function() {
  615. it('should expose local variables', inject(function($parse) {
  616. expect($parse('a')({a: 0}, {a: 1})).toEqual(1);
  617. expect($parse('add(a,b)')({b: 1, add: function(a, b) { return a + b; }}, {a: 2})).toEqual(3);
  618. }));
  619. it('should expose traverse locals', inject(function($parse) {
  620. expect($parse('a.b')({a: {b: 0}}, {a: {b:1}})).toEqual(1);
  621. expect($parse('a.b')({a: null}, {a: {b:1}})).toEqual(1);
  622. expect($parse('a.b')({a: {b: 0}}, {a: null})).toEqual(undefined);
  623. }));
  624. });
  625. describe('literal', function() {
  626. it('should mark scalar value expressions as literal', inject(function($parse) {
  627. expect($parse('0').literal).toBe(true);
  628. expect($parse('"hello"').literal).toBe(true);
  629. expect($parse('true').literal).toBe(true);
  630. expect($parse('false').literal).toBe(true);
  631. expect($parse('null').literal).toBe(true);
  632. expect($parse('undefined').literal).toBe(true);
  633. }));
  634. it('should mark array expressions as literal', inject(function($parse) {
  635. expect($parse('[]').literal).toBe(true);
  636. expect($parse('[1, 2, 3]').literal).toBe(true);
  637. expect($parse('[1, identifier]').literal).toBe(true);
  638. }));
  639. it('should mark object expressions as literal', inject(function($parse) {
  640. expect($parse('{}').literal).toBe(true);
  641. expect($parse('{x: 1}').literal).toBe(true);
  642. expect($parse('{foo: bar}').literal).toBe(true);
  643. }));
  644. it('should not mark function calls or operator expressions as literal', inject(function($parse) {
  645. expect($parse('1 + 1').literal).toBe(false);
  646. expect($parse('call()').literal).toBe(false);
  647. expect($parse('[].length').literal).toBe(false);
  648. }));
  649. });
  650. describe('constant', function() {
  651. it('should mark scalar value expressions as constant', inject(function($parse) {
  652. expect($parse('12.3').constant).toBe(true);
  653. expect($parse('"string"').constant).toBe(true);
  654. expect($parse('true').constant).toBe(true);
  655. expect($parse('false').constant).toBe(true);
  656. expect($parse('null').constant).toBe(true);
  657. expect($parse('undefined').constant).toBe(true);
  658. }));
  659. it('should mark arrays as constant if they only contain constant elements', inject(function($parse) {
  660. expect($parse('[]').constant).toBe(true);
  661. expect($parse('[1, 2, 3]').constant).toBe(true);
  662. expect($parse('["string", null]').constant).toBe(true);
  663. expect($parse('[[]]').constant).toBe(true);
  664. expect($parse('[1, [2, 3], {4: 5}]').constant).toBe(true);
  665. }));
  666. it('should not mark arrays as constant if they contain any non-constant elements', inject(function($parse) {
  667. expect($parse('[foo]').constant).toBe(false);
  668. expect($parse('[x + 1]').constant).toBe(false);
  669. expect($parse('[bar[0]]').constant).toBe(false);
  670. }));
  671. it('should mark complex expressions involving constant values as constant', inject(function($parse) {
  672. expect($parse('!true').constant).toBe(true);
  673. expect($parse('1 - 1').constant).toBe(true);
  674. expect($parse('"foo" + "bar"').constant).toBe(true);
  675. expect($parse('5 != null').constant).toBe(true);
  676. expect($parse('{standard: 4/3, wide: 16/9}').constant).toBe(true);
  677. }));
  678. it('should not mark any expression involving variables or function calls as constant', inject(function($parse) {
  679. expect($parse('true.toString()').constant).toBe(false);
  680. expect($parse('foo(1, 2, 3)').constant).toBe(false);
  681. expect($parse('"name" + id').constant).toBe(false);
  682. }));
  683. });
  684. });
  685. });
  686. });