PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/test/ng/parseSpec.js

https://github.com/cedricss/angular.js
JavaScript | 676 lines | 662 code | 14 blank | 0 comment | 4 complexity | 7a75dea8afe65294a947eaa9a2fe1a57 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. describe('promises', function() {
  395. var deferred, promise, q;
  396. beforeEach(inject(function($q) {
  397. q = $q;
  398. deferred = q.defer();
  399. promise = deferred.promise;
  400. }));
  401. describe('{{promise}}', function() {
  402. it('should evaluated resolved promise and get its value', function() {
  403. deferred.resolve('hello!');
  404. scope.greeting = promise;
  405. expect(scope.$eval('greeting')).toBe(undefined);
  406. scope.$digest();
  407. expect(scope.$eval('greeting')).toBe('hello!');
  408. });
  409. it('should evaluated rejected promise and ignore the rejection reason', function() {
  410. deferred.reject('sorry');
  411. scope.greeting = promise;
  412. expect(scope.$eval('gretting')).toBe(undefined);
  413. scope.$digest();
  414. expect(scope.$eval('greeting')).toBe(undefined);
  415. });
  416. it('should evaluate a promise and eventualy get its value', function() {
  417. scope.greeting = promise;
  418. expect(scope.$eval('greeting')).toBe(undefined);
  419. scope.$digest();
  420. expect(scope.$eval('greeting')).toBe(undefined);
  421. deferred.resolve('hello!');
  422. expect(scope.$eval('greeting')).toBe(undefined);
  423. scope.$digest();
  424. expect(scope.$eval('greeting')).toBe('hello!');
  425. });
  426. it('should evaluate a promise and eventualy ignore its rejection', function() {
  427. scope.greeting = promise;
  428. expect(scope.$eval('greeting')).toBe(undefined);
  429. scope.$digest();
  430. expect(scope.$eval('greeting')).toBe(undefined);
  431. deferred.reject('sorry');
  432. expect(scope.$eval('greeting')).toBe(undefined);
  433. scope.$digest();
  434. expect(scope.$eval('greeting')).toBe(undefined);
  435. });
  436. });
  437. describe('dereferencing', function() {
  438. it('should evaluate and dereference properties leading to and from a promise', function() {
  439. scope.obj = {greeting: promise};
  440. expect(scope.$eval('obj.greeting')).toBe(undefined);
  441. expect(scope.$eval('obj.greeting.polite')).toBe(undefined);
  442. scope.$digest();
  443. expect(scope.$eval('obj.greeting')).toBe(undefined);
  444. expect(scope.$eval('obj.greeting.polite')).toBe(undefined);
  445. deferred.resolve({polite: 'Good morning!'});
  446. scope.$digest();
  447. expect(scope.$eval('obj.greeting')).toEqual({polite: 'Good morning!'});
  448. expect(scope.$eval('obj.greeting.polite')).toBe('Good morning!');
  449. });
  450. it('should evaluate and dereference properties leading to and from a promise via bracket ' +
  451. 'notation', function() {
  452. scope.obj = {greeting: promise};
  453. expect(scope.$eval('obj["greeting"]')).toBe(undefined);
  454. expect(scope.$eval('obj["greeting"]["polite"]')).toBe(undefined);
  455. scope.$digest();
  456. expect(scope.$eval('obj["greeting"]')).toBe(undefined);
  457. expect(scope.$eval('obj["greeting"]["polite"]')).toBe(undefined);
  458. deferred.resolve({polite: 'Good morning!'});
  459. scope.$digest();
  460. expect(scope.$eval('obj["greeting"]')).toEqual({polite: 'Good morning!'});
  461. expect(scope.$eval('obj["greeting"]["polite"]')).toBe('Good morning!');
  462. });
  463. it('should evaluate and dereference array references leading to and from a promise',
  464. function() {
  465. scope.greetings = [promise];
  466. expect(scope.$eval('greetings[0]')).toBe(undefined);
  467. expect(scope.$eval('greetings[0][0]')).toBe(undefined);
  468. scope.$digest();
  469. expect(scope.$eval('greetings[0]')).toBe(undefined);
  470. expect(scope.$eval('greetings[0][0]')).toBe(undefined);
  471. deferred.resolve(['Hi!', 'Cau!']);
  472. scope.$digest();
  473. expect(scope.$eval('greetings[0]')).toEqual(['Hi!', 'Cau!']);
  474. expect(scope.$eval('greetings[0][0]')).toBe('Hi!');
  475. });
  476. it('should evaluate and dereference promises used as function arguments', function() {
  477. scope.greet = function(name) { return 'Hi ' + name + '!'; };
  478. scope.name = promise;
  479. expect(scope.$eval('greet(name)')).toBe('Hi undefined!');
  480. scope.$digest();
  481. expect(scope.$eval('greet(name)')).toBe('Hi undefined!');
  482. deferred.resolve('Veronica');
  483. expect(scope.$eval('greet(name)')).toBe('Hi undefined!');
  484. scope.$digest();
  485. expect(scope.$eval('greet(name)')).toBe('Hi Veronica!');
  486. });
  487. it('should evaluate and dereference promises used as array indexes', function() {
  488. scope.childIndex = promise;
  489. scope.kids = ['Adam', 'Veronica', 'Elisa'];
  490. expect(scope.$eval('kids[childIndex]')).toBe(undefined);
  491. scope.$digest();
  492. expect(scope.$eval('kids[childIndex]')).toBe(undefined);
  493. deferred.resolve(1);
  494. expect(scope.$eval('kids[childIndex]')).toBe(undefined);
  495. scope.$digest();
  496. expect(scope.$eval('kids[childIndex]')).toBe('Veronica');
  497. });
  498. it('should evaluate and dereference promises used as keys in bracket notation', function() {
  499. scope.childKey = promise;
  500. scope.kids = {'a': 'Adam', 'v': 'Veronica', 'e': 'Elisa'};
  501. expect(scope.$eval('kids[childKey]')).toBe(undefined);
  502. scope.$digest();
  503. expect(scope.$eval('kids[childKey]')).toBe(undefined);
  504. deferred.resolve('v');
  505. expect(scope.$eval('kids[childKey]')).toBe(undefined);
  506. scope.$digest();
  507. expect(scope.$eval('kids[childKey]')).toBe('Veronica');
  508. });
  509. it('should not mess with the promise if it was not directly evaluated', function() {
  510. scope.obj = {greeting: promise, username: 'hi'};
  511. var obj = scope.$eval('obj');
  512. expect(obj.username).toEqual('hi');
  513. expect(typeof obj.greeting.then).toBe('function');
  514. });
  515. });
  516. });
  517. describe('assignable', function() {
  518. it('should expose assignment function', inject(function($parse) {
  519. var fn = $parse('a');
  520. expect(fn.assign).toBeTruthy();
  521. var scope = {};
  522. fn.assign(scope, 123);
  523. expect(scope).toEqual({a:123});
  524. }));
  525. });
  526. describe('locals', function() {
  527. it('should expose local variables', inject(function($parse) {
  528. expect($parse('a')({a: 0}, {a: 1})).toEqual(1);
  529. expect($parse('add(a,b)')({b: 1, add: function(a, b) { return a + b; }}, {a: 2})).toEqual(3);
  530. }));
  531. it('should expose traverse locals', inject(function($parse) {
  532. expect($parse('a.b')({a: {b: 0}}, {a: {b:1}})).toEqual(1);
  533. expect($parse('a.b')({a: null}, {a: {b:1}})).toEqual(1);
  534. expect($parse('a.b')({a: {b: 0}}, {a: null})).toEqual(undefined);
  535. }));
  536. });
  537. });
  538. });
  539. });