PageRenderTime 30ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/test/ng/parseSpec.js

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