PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/test/ng/parseSpec.js

https://github.com/thanushcst/angular.js
JavaScript | 1183 lines | 1161 code | 19 blank | 3 comment | 5 complexity | 612d7a901b88702b0742e3d8b65aa695 MD5 | raw file
Possible License(s): JSON
  1. 'use strict';
  2. describe('parser', function() {
  3. beforeEach(function() {
  4. /* global getterFnCache: true */
  5. // clear cache
  6. getterFnCache = {};
  7. });
  8. describe('lexer', function() {
  9. var lex;
  10. beforeEach(function () {
  11. /* global Lexer: false */
  12. lex = function () {
  13. var lexer = new Lexer({csp: false});
  14. return lexer.lex.apply(lexer, arguments);
  15. };
  16. });
  17. it('should tokenize a string', function() {
  18. var tokens = lex("a.bc[22]+1.3|f:'a\\\'c':\"d\\\"e\"");
  19. var i = 0;
  20. expect(tokens[i].index).toEqual(0);
  21. expect(tokens[i].text).toEqual('a.bc');
  22. i++;
  23. expect(tokens[i].index).toEqual(4);
  24. expect(tokens[i].text).toEqual('[');
  25. i++;
  26. expect(tokens[i].index).toEqual(5);
  27. expect(tokens[i].text).toEqual(22);
  28. i++;
  29. expect(tokens[i].index).toEqual(7);
  30. expect(tokens[i].text).toEqual(']');
  31. i++;
  32. expect(tokens[i].index).toEqual(8);
  33. expect(tokens[i].text).toEqual('+');
  34. i++;
  35. expect(tokens[i].index).toEqual(9);
  36. expect(tokens[i].text).toEqual(1.3);
  37. i++;
  38. expect(tokens[i].index).toEqual(12);
  39. expect(tokens[i].text).toEqual('|');
  40. i++;
  41. expect(tokens[i].index).toEqual(13);
  42. expect(tokens[i].text).toEqual('f');
  43. i++;
  44. expect(tokens[i].index).toEqual(14);
  45. expect(tokens[i].text).toEqual(':');
  46. i++;
  47. expect(tokens[i].index).toEqual(15);
  48. expect(tokens[i].string).toEqual("a'c");
  49. i++;
  50. expect(tokens[i].index).toEqual(21);
  51. expect(tokens[i].text).toEqual(':');
  52. i++;
  53. expect(tokens[i].index).toEqual(22);
  54. expect(tokens[i].string).toEqual('d"e');
  55. });
  56. it('should tokenize undefined', function() {
  57. var tokens = lex("undefined");
  58. var i = 0;
  59. expect(tokens[i].index).toEqual(0);
  60. expect(tokens[i].text).toEqual('undefined');
  61. expect(undefined).toEqual(tokens[i].fn());
  62. });
  63. it('should tokenize quoted string', function() {
  64. var str = "['\\'', \"\\\"\"]";
  65. var tokens = lex(str);
  66. expect(tokens[1].index).toEqual(1);
  67. expect(tokens[1].string).toEqual("'");
  68. expect(tokens[3].index).toEqual(7);
  69. expect(tokens[3].string).toEqual('"');
  70. });
  71. it('should tokenize escaped quoted string', function() {
  72. var str = '"\\"\\n\\f\\r\\t\\v\\u00A0"';
  73. var tokens = lex(str);
  74. expect(tokens[0].string).toEqual('"\n\f\r\t\v\u00A0');
  75. });
  76. it('should tokenize unicode', function() {
  77. var tokens = lex('"\\u00A0"');
  78. expect(tokens.length).toEqual(1);
  79. expect(tokens[0].string).toEqual('\u00a0');
  80. });
  81. it('should ignore whitespace', function() {
  82. var tokens = lex("a \t \n \r b");
  83. expect(tokens[0].text).toEqual('a');
  84. expect(tokens[1].text).toEqual('b');
  85. });
  86. it('should tokenize relation and equality', function() {
  87. var tokens = lex("! == != < > <= >= === !==");
  88. expect(tokens[0].text).toEqual('!');
  89. expect(tokens[1].text).toEqual('==');
  90. expect(tokens[2].text).toEqual('!=');
  91. expect(tokens[3].text).toEqual('<');
  92. expect(tokens[4].text).toEqual('>');
  93. expect(tokens[5].text).toEqual('<=');
  94. expect(tokens[6].text).toEqual('>=');
  95. expect(tokens[7].text).toEqual('===');
  96. expect(tokens[8].text).toEqual('!==');
  97. });
  98. it('should tokenize logical and ternary', function() {
  99. var tokens = lex("&& || ? :");
  100. expect(tokens[0].text).toEqual('&&');
  101. expect(tokens[1].text).toEqual('||');
  102. expect(tokens[2].text).toEqual('?');
  103. expect(tokens[3].text).toEqual(':');
  104. });
  105. it('should tokenize statements', function() {
  106. var tokens = lex("a;b;");
  107. expect(tokens[0].text).toEqual('a');
  108. expect(tokens[1].text).toEqual(';');
  109. expect(tokens[2].text).toEqual('b');
  110. expect(tokens[3].text).toEqual(';');
  111. });
  112. it('should tokenize function invocation', function() {
  113. var tokens = lex("a()");
  114. expect(map(tokens, function(t) { return t.text;})).toEqual(['a', '(', ')']);
  115. });
  116. it('should tokenize method invocation', function() {
  117. var tokens = lex("a.b.c (d) - e.f()");
  118. expect(map(tokens, function(t) { return t.text;})).
  119. toEqual(['a.b', '.', 'c', '(', 'd', ')', '-', 'e', '.', 'f', '(', ')']);
  120. });
  121. it('should tokenize number', function() {
  122. var tokens = lex("0.5");
  123. expect(tokens[0].text).toEqual(0.5);
  124. });
  125. it('should tokenize negative number', inject(function($rootScope) {
  126. var value = $rootScope.$eval("-0.5");
  127. expect(value).toEqual(-0.5);
  128. value = $rootScope.$eval("{a:-0.5}");
  129. expect(value).toEqual({a:-0.5});
  130. }));
  131. it('should tokenize number with exponent', inject(function($rootScope) {
  132. var tokens = lex("0.5E-10");
  133. expect(tokens[0].text).toEqual(0.5E-10);
  134. expect($rootScope.$eval("0.5E-10")).toEqual(0.5E-10);
  135. tokens = lex("0.5E+10");
  136. expect(tokens[0].text).toEqual(0.5E+10);
  137. }));
  138. it('should throws exception for invalid exponent', function() {
  139. expect(function() {
  140. lex("0.5E-");
  141. }).toThrowMinErr('$parse', 'lexerr', 'Lexer Error: Invalid exponent at column 4 in expression [0.5E-].');
  142. expect(function() {
  143. lex("0.5E-A");
  144. }).toThrowMinErr('$parse', 'lexerr', 'Lexer Error: Invalid exponent at column 4 in expression [0.5E-A].');
  145. });
  146. it('should tokenize number starting with a dot', function() {
  147. var tokens = lex(".5");
  148. expect(tokens[0].text).toEqual(0.5);
  149. });
  150. it('should throw error on invalid unicode', function() {
  151. expect(function() {
  152. lex("'\\u1''bla'");
  153. }).toThrowMinErr("$parse", "lexerr", "Lexer Error: Invalid unicode escape [\\u1''b] at column 2 in expression ['\\u1''bla'].");
  154. });
  155. });
  156. var $filterProvider, scope;
  157. beforeEach(module(['$filterProvider', function (filterProvider) {
  158. $filterProvider = filterProvider;
  159. }]));
  160. forEach([true, false], function(cspEnabled) {
  161. describe('csp: ' + cspEnabled, function() {
  162. var originalSecurityPolicy;
  163. beforeEach(function() {
  164. originalSecurityPolicy = window.document.securityPolicy;
  165. window.document.securityPolicy = {isActive : cspEnabled};
  166. });
  167. afterEach(function() {
  168. window.document.securityPolicy = originalSecurityPolicy;
  169. });
  170. beforeEach(inject(function ($rootScope) {
  171. scope = $rootScope;
  172. }));
  173. it('should parse expressions', function() {
  174. /*jshint -W006, -W007 */
  175. expect(scope.$eval("-1")).toEqual(-1);
  176. expect(scope.$eval("1 + 2.5")).toEqual(3.5);
  177. expect(scope.$eval("1 + -2.5")).toEqual(-1.5);
  178. expect(scope.$eval("1+2*3/4")).toEqual(1+2*3/4);
  179. expect(scope.$eval("0--1+1.5")).toEqual(0- -1 + 1.5);
  180. expect(scope.$eval("-0--1++2*-3/-4")).toEqual(-0- -1+ +2*-3/-4);
  181. expect(scope.$eval("1/2*3")).toEqual(1/2*3);
  182. });
  183. it('should parse comparison', function() {
  184. /* jshint -W041 */
  185. expect(scope.$eval("false")).toBeFalsy();
  186. expect(scope.$eval("!true")).toBeFalsy();
  187. expect(scope.$eval("1==1")).toBeTruthy();
  188. expect(scope.$eval("1==true")).toBeTruthy();
  189. expect(scope.$eval("1===1")).toBeTruthy();
  190. expect(scope.$eval("1==='1'")).toBeFalsy();
  191. expect(scope.$eval("1===true")).toBeFalsy();
  192. expect(scope.$eval("'true'===true")).toBeFalsy();
  193. expect(scope.$eval("1!==2")).toBeTruthy();
  194. expect(scope.$eval("1!=='1'")).toBeTruthy();
  195. expect(scope.$eval("1!=2")).toBeTruthy();
  196. expect(scope.$eval("1<2")).toBeTruthy();
  197. expect(scope.$eval("1<=1")).toBeTruthy();
  198. expect(scope.$eval("1>2")).toEqual(1>2);
  199. expect(scope.$eval("2>=1")).toEqual(2>=1);
  200. expect(scope.$eval("true==2<3")).toEqual(true == 2<3);
  201. expect(scope.$eval("true===2<3")).toEqual(true === 2<3);
  202. });
  203. it('should parse logical', function() {
  204. expect(scope.$eval("0&&2")).toEqual(0&&2);
  205. expect(scope.$eval("0||2")).toEqual(0||2);
  206. expect(scope.$eval("0||1&&2")).toEqual(0||1&&2);
  207. });
  208. it('should parse ternary', function(){
  209. var returnTrue = scope.returnTrue = function(){ return true; };
  210. var returnFalse = scope.returnFalse = function(){ return false; };
  211. var returnString = scope.returnString = function(){ return 'asd'; };
  212. var returnInt = scope.returnInt = function(){ return 123; };
  213. var identity = scope.identity = function(x){ return x; };
  214. // Simple.
  215. expect(scope.$eval('0?0:2')).toEqual(0?0:2);
  216. expect(scope.$eval('1?0:2')).toEqual(1?0:2);
  217. // Nested on the left.
  218. expect(scope.$eval('0?0?0:0:2')).toEqual(0?0?0:0:2);
  219. expect(scope.$eval('1?0?0:0:2')).toEqual(1?0?0:0:2);
  220. expect(scope.$eval('0?1?0:0:2')).toEqual(0?1?0:0:2);
  221. expect(scope.$eval('0?0?1:0:2')).toEqual(0?0?1:0:2);
  222. expect(scope.$eval('0?0?0:2:3')).toEqual(0?0?0:2:3);
  223. expect(scope.$eval('1?1?0:0:2')).toEqual(1?1?0:0:2);
  224. expect(scope.$eval('1?1?1:0:2')).toEqual(1?1?1:0:2);
  225. expect(scope.$eval('1?1?1:2:3')).toEqual(1?1?1:2:3);
  226. expect(scope.$eval('1?1?1:2:3')).toEqual(1?1?1:2:3);
  227. // Nested on the right.
  228. expect(scope.$eval('0?0:0?0:2')).toEqual(0?0:0?0:2);
  229. expect(scope.$eval('1?0:0?0:2')).toEqual(1?0:0?0:2);
  230. expect(scope.$eval('0?1:0?0:2')).toEqual(0?1:0?0:2);
  231. expect(scope.$eval('0?0:1?0:2')).toEqual(0?0:1?0:2);
  232. expect(scope.$eval('0?0:0?2:3')).toEqual(0?0:0?2:3);
  233. expect(scope.$eval('1?1:0?0:2')).toEqual(1?1:0?0:2);
  234. expect(scope.$eval('1?1:1?0:2')).toEqual(1?1:1?0:2);
  235. expect(scope.$eval('1?1:1?2:3')).toEqual(1?1:1?2:3);
  236. expect(scope.$eval('1?1:1?2:3')).toEqual(1?1:1?2:3);
  237. // Precedence with respect to logical operators.
  238. expect(scope.$eval('0&&1?0:1')).toEqual(0&&1?0:1);
  239. expect(scope.$eval('1||0?0:0')).toEqual(1||0?0:0);
  240. expect(scope.$eval('0?0&&1:2')).toEqual(0?0&&1:2);
  241. expect(scope.$eval('0?1&&1:2')).toEqual(0?1&&1:2);
  242. expect(scope.$eval('0?0||0:1')).toEqual(0?0||0:1);
  243. expect(scope.$eval('0?0||1:2')).toEqual(0?0||1:2);
  244. expect(scope.$eval('1?0&&1:2')).toEqual(1?0&&1:2);
  245. expect(scope.$eval('1?1&&1:2')).toEqual(1?1&&1:2);
  246. expect(scope.$eval('1?0||0:1')).toEqual(1?0||0:1);
  247. expect(scope.$eval('1?0||1:2')).toEqual(1?0||1:2);
  248. expect(scope.$eval('0?1:0&&1')).toEqual(0?1:0&&1);
  249. expect(scope.$eval('0?2:1&&1')).toEqual(0?2:1&&1);
  250. expect(scope.$eval('0?1:0||0')).toEqual(0?1:0||0);
  251. expect(scope.$eval('0?2:0||1')).toEqual(0?2:0||1);
  252. expect(scope.$eval('1?1:0&&1')).toEqual(1?1:0&&1);
  253. expect(scope.$eval('1?2:1&&1')).toEqual(1?2:1&&1);
  254. expect(scope.$eval('1?1:0||0')).toEqual(1?1:0||0);
  255. expect(scope.$eval('1?2:0||1')).toEqual(1?2:0||1);
  256. // Function calls.
  257. expect(scope.$eval('returnTrue() ? returnString() : returnInt()')).toEqual(returnTrue() ? returnString() : returnInt());
  258. expect(scope.$eval('returnFalse() ? returnString() : returnInt()')).toEqual(returnFalse() ? returnString() : returnInt());
  259. expect(scope.$eval('returnTrue() ? returnString() : returnInt()')).toEqual(returnTrue() ? returnString() : returnInt());
  260. expect(scope.$eval('identity(returnFalse() ? returnString() : returnInt())')).toEqual(identity(returnFalse() ? returnString() : returnInt()));
  261. });
  262. it('should parse string', function() {
  263. expect(scope.$eval("'a' + 'b c'")).toEqual("ab c");
  264. });
  265. it('should parse filters', function() {
  266. $filterProvider.register('substring', valueFn(function(input, start, end) {
  267. return input.substring(start, end);
  268. }));
  269. expect(function() {
  270. scope.$eval("1|nonexistent");
  271. }).toThrowMinErr('$injector', 'unpr', 'Unknown provider: nonexistentFilterProvider <- nonexistentFilter');
  272. scope.offset = 3;
  273. expect(scope.$eval("'abcd'|substring:1:offset")).toEqual("bc");
  274. expect(scope.$eval("'abcd'|substring:1:3|uppercase")).toEqual("BC");
  275. });
  276. it('should access scope', function() {
  277. scope.a = 123;
  278. scope.b = {c: 456};
  279. expect(scope.$eval("a", scope)).toEqual(123);
  280. expect(scope.$eval("b.c", scope)).toEqual(456);
  281. expect(scope.$eval("x.y.z", scope)).not.toBeDefined();
  282. });
  283. it('should resolve deeply nested paths (important for CSP mode)', function() {
  284. scope.a = {b: {c: {d: {e: {f: {g: {h: {i: {j: {k: {l: {m: {n: 'nooo!'}}}}}}}}}}}}};
  285. expect(scope.$eval("a.b.c.d.e.f.g.h.i.j.k.l.m.n", scope)).toBe('nooo!');
  286. });
  287. forEach([2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 42, 99], function(pathLength) {
  288. it('should resolve nested paths of length ' + pathLength, function() {
  289. // Create a nested object {x2: {x3: {x4: ... {x[n]: 42} ... }}}.
  290. var obj = 42, locals = {};
  291. for (var i = pathLength; i >= 2; i--) {
  292. var newObj = {};
  293. newObj['x' + i] = obj;
  294. obj = newObj;
  295. }
  296. // Assign to x1 and build path 'x1.x2.x3. ... .x[n]' to access the final value.
  297. scope.x1 = obj;
  298. var path = 'x1';
  299. for (i = 2; i <= pathLength; i++) {
  300. path += '.x' + i;
  301. }
  302. expect(scope.$eval(path)).toBe(42);
  303. locals['x' + pathLength] = 'not 42';
  304. expect(scope.$eval(path, locals)).toBe(42);
  305. });
  306. });
  307. it('should be forgiving', function() {
  308. scope.a = {b: 23};
  309. expect(scope.$eval('b')).toBeUndefined();
  310. expect(scope.$eval('a.x')).toBeUndefined();
  311. expect(scope.$eval('a.b.c.d')).toBeUndefined();
  312. });
  313. it('should support property names that collide with native object properties', function() {
  314. // regression
  315. scope.watch = 1;
  316. scope.toString = function toString() {
  317. return "custom toString";
  318. };
  319. expect(scope.$eval('watch', scope)).toBe(1);
  320. expect(scope.$eval('toString()', scope)).toBe('custom toString');
  321. });
  322. it('should not break if hasOwnProperty is referenced in an expression', function() {
  323. scope.obj = { value: 1};
  324. // By evaluating an expression that calls hasOwnProperty, the getterFnCache
  325. // will store a property called hasOwnProperty. This is effectively:
  326. // getterFnCache['hasOwnProperty'] = null
  327. scope.$eval('obj.hasOwnProperty("value")');
  328. // If we rely on this property then evaluating any expression will fail
  329. // because it is not able to find out if obj.value is there in the cache
  330. expect(scope.$eval('obj.value')).toBe(1);
  331. });
  332. it('should not break if the expression is "hasOwnProperty"', function() {
  333. scope.fooExp = 'barVal';
  334. // By evaluating hasOwnProperty, the $parse cache will store a getter for
  335. // the scope's own hasOwnProperty function, which will mess up future cache look ups.
  336. // i.e. cache['hasOwnProperty'] = function(scope) { return scope.hasOwnProperty; }
  337. scope.$eval('hasOwnProperty');
  338. expect(scope.$eval('fooExp')).toBe('barVal');
  339. });
  340. it('should evaluate grouped expressions', function() {
  341. expect(scope.$eval("(1+2)*3")).toEqual((1+2)*3);
  342. });
  343. it('should evaluate assignments', function() {
  344. expect(scope.$eval("a=12")).toEqual(12);
  345. expect(scope.a).toEqual(12);
  346. expect(scope.$eval("x.y.z=123;")).toEqual(123);
  347. expect(scope.x.y.z).toEqual(123);
  348. expect(scope.$eval("a=123; b=234")).toEqual(234);
  349. expect(scope.a).toEqual(123);
  350. expect(scope.b).toEqual(234);
  351. });
  352. it('should evaluate function call without arguments', function() {
  353. scope['const'] = function(a,b){return 123;};
  354. expect(scope.$eval("const()")).toEqual(123);
  355. });
  356. it('should evaluate function call with arguments', function() {
  357. scope.add = function(a,b) {
  358. return a+b;
  359. };
  360. expect(scope.$eval("add(1,2)")).toEqual(3);
  361. });
  362. it('should evaluate function call from a return value', function() {
  363. scope.val = 33;
  364. scope.getter = function() { return function() { return this.val; }; };
  365. expect(scope.$eval("getter()()")).toBe(33);
  366. });
  367. it('should evaluate multiplication and division', function() {
  368. scope.taxRate = 8;
  369. scope.subTotal = 100;
  370. expect(scope.$eval("taxRate / 100 * subTotal")).toEqual(8);
  371. expect(scope.$eval("subTotal * taxRate / 100")).toEqual(8);
  372. });
  373. it('should evaluate array', function() {
  374. expect(scope.$eval("[]").length).toEqual(0);
  375. expect(scope.$eval("[1, 2]").length).toEqual(2);
  376. expect(scope.$eval("[1, 2]")[0]).toEqual(1);
  377. expect(scope.$eval("[1, 2]")[1]).toEqual(2);
  378. expect(scope.$eval("[1, 2,]")[1]).toEqual(2);
  379. expect(scope.$eval("[1, 2,]").length).toEqual(2);
  380. });
  381. it('should evaluate array access', function() {
  382. expect(scope.$eval("[1][0]")).toEqual(1);
  383. expect(scope.$eval("[[1]][0][0]")).toEqual(1);
  384. expect(scope.$eval("[].length")).toEqual(0);
  385. expect(scope.$eval("[1, 2].length")).toEqual(2);
  386. });
  387. it('should evaluate object', function() {
  388. expect(toJson(scope.$eval("{}"))).toEqual("{}");
  389. expect(toJson(scope.$eval("{a:'b'}"))).toEqual('{"a":"b"}');
  390. expect(toJson(scope.$eval("{'a':'b'}"))).toEqual('{"a":"b"}');
  391. expect(toJson(scope.$eval("{\"a\":'b'}"))).toEqual('{"a":"b"}');
  392. expect(toJson(scope.$eval("{a:'b',}"))).toEqual('{"a":"b"}');
  393. expect(toJson(scope.$eval("{'a':'b',}"))).toEqual('{"a":"b"}');
  394. expect(toJson(scope.$eval("{\"a\":'b',}"))).toEqual('{"a":"b"}');
  395. });
  396. it('should evaluate object access', function() {
  397. expect(scope.$eval("{false:'WC', true:'CC'}[false]")).toEqual("WC");
  398. });
  399. it('should evaluate JSON', function() {
  400. expect(toJson(scope.$eval("[{}]"))).toEqual("[{}]");
  401. expect(toJson(scope.$eval("[{a:[]}, {b:1}]"))).toEqual('[{"a":[]},{"b":1}]');
  402. });
  403. it('should evaluate multiple statements', function() {
  404. expect(scope.$eval("a=1;b=3;a+b")).toEqual(4);
  405. expect(scope.$eval(";;1;;")).toEqual(1);
  406. });
  407. it('should evaluate object methods in correct context (this)', function() {
  408. var C = function () {
  409. this.a = 123;
  410. };
  411. C.prototype.getA = function() {
  412. return this.a;
  413. };
  414. scope.obj = new C();
  415. expect(scope.$eval("obj.getA()")).toEqual(123);
  416. expect(scope.$eval("obj['getA']()")).toEqual(123);
  417. });
  418. it('should evaluate methods in correct context (this) in argument', function() {
  419. var C = function () {
  420. this.a = 123;
  421. };
  422. C.prototype.sum = function(value) {
  423. return this.a + value;
  424. };
  425. C.prototype.getA = function() {
  426. return this.a;
  427. };
  428. scope.obj = new C();
  429. expect(scope.$eval("obj.sum(obj.getA())")).toEqual(246);
  430. expect(scope.$eval("obj['sum'](obj.getA())")).toEqual(246);
  431. });
  432. it('should evaluate objects on scope context', function() {
  433. scope.a = "abc";
  434. expect(scope.$eval("{a:a}").a).toEqual("abc");
  435. });
  436. it('should evaluate field access on function call result', function() {
  437. scope.a = function() {
  438. return {name:'misko'};
  439. };
  440. expect(scope.$eval("a().name")).toEqual("misko");
  441. });
  442. it('should evaluate field access after array access', function () {
  443. scope.items = [{}, {name:'misko'}];
  444. expect(scope.$eval('items[1].name')).toEqual("misko");
  445. });
  446. it('should evaluate array assignment', function() {
  447. scope.items = [];
  448. expect(scope.$eval('items[1] = "abc"')).toEqual("abc");
  449. expect(scope.$eval('items[1]')).toEqual("abc");
  450. // Dont know how to make this work....
  451. // expect(scope.$eval('books[1] = "moby"')).toEqual("moby");
  452. // expect(scope.$eval('books[1]')).toEqual("moby");
  453. });
  454. it('should evaluate grouped filters', function() {
  455. scope.name = 'MISKO';
  456. expect(scope.$eval('n = (name|lowercase)')).toEqual('misko');
  457. expect(scope.$eval('n')).toEqual('misko');
  458. });
  459. it('should evaluate remainder', function() {
  460. expect(scope.$eval('1%2')).toEqual(1);
  461. });
  462. it('should evaluate sum with undefined', function() {
  463. expect(scope.$eval('1+undefined')).toEqual(1);
  464. expect(scope.$eval('undefined+1')).toEqual(1);
  465. });
  466. it('should throw exception on non-closed bracket', function() {
  467. expect(function() {
  468. scope.$eval('[].count(');
  469. }).toThrowMinErr('$parse', 'ueoe', 'Unexpected end of expression: [].count(');
  470. });
  471. it('should evaluate double negation', function() {
  472. expect(scope.$eval('true')).toBeTruthy();
  473. expect(scope.$eval('!true')).toBeFalsy();
  474. expect(scope.$eval('!!true')).toBeTruthy();
  475. expect(scope.$eval('{true:"a", false:"b"}[!!true]')).toEqual('a');
  476. });
  477. it('should evaluate negation', function() {
  478. /* jshint -W018 */
  479. expect(scope.$eval("!false || true")).toEqual(!false || true);
  480. expect(scope.$eval("!11 == 10")).toEqual(!11 == 10);
  481. expect(scope.$eval("12/6/2")).toEqual(12/6/2);
  482. });
  483. it('should evaluate exclamation mark', function() {
  484. expect(scope.$eval('suffix = "!"')).toEqual('!');
  485. });
  486. it('should evaluate minus', function() {
  487. expect(scope.$eval("{a:'-'}")).toEqual({a: "-"});
  488. });
  489. it('should evaluate undefined', function() {
  490. expect(scope.$eval("undefined")).not.toBeDefined();
  491. expect(scope.$eval("a=undefined")).not.toBeDefined();
  492. expect(scope.a).not.toBeDefined();
  493. });
  494. it('should allow assignment after array dereference', function() {
  495. scope.obj = [{}];
  496. scope.$eval('obj[0].name=1');
  497. expect(scope.obj.name).toBeUndefined();
  498. expect(scope.obj[0].name).toEqual(1);
  499. });
  500. it('should short-circuit AND operator', function() {
  501. scope.run = function() {
  502. throw "IT SHOULD NOT HAVE RUN";
  503. };
  504. expect(scope.$eval('false && run()')).toBe(false);
  505. });
  506. it('should short-circuit OR operator', function() {
  507. scope.run = function() {
  508. throw "IT SHOULD NOT HAVE RUN";
  509. };
  510. expect(scope.$eval('true || run()')).toBe(true);
  511. });
  512. it('should support method calls on primitive types', function() {
  513. scope.empty = '';
  514. scope.zero = 0;
  515. scope.bool = false;
  516. expect(scope.$eval('empty.substr(0)')).toBe('');
  517. expect(scope.$eval('zero.toString()')).toBe('0');
  518. expect(scope.$eval('bool.toString()')).toBe('false');
  519. });
  520. it('should evaluate expressions with line terminators', function() {
  521. scope.a = "a";
  522. scope.b = {c: "bc"};
  523. expect(scope.$eval('a + \n b.c + \r "\td" + \t \r\n\r "\r\n\n"')).toEqual("abc\td\r\n\n");
  524. });
  525. describe('sandboxing', function() {
  526. describe('Function constructor', function() {
  527. it('should NOT allow access to Function constructor in getter', function() {
  528. expect(function() {
  529. scope.$eval('{}.toString.constructor');
  530. }).toThrowMinErr(
  531. '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' +
  532. 'Expression: {}.toString.constructor');
  533. expect(function() {
  534. scope.$eval('{}.toString.constructor("alert(1)")');
  535. }).toThrowMinErr(
  536. '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' +
  537. 'Expression: {}.toString.constructor("alert(1)")');
  538. expect(function() {
  539. scope.$eval('[].toString.constructor.foo');
  540. }).toThrowMinErr(
  541. '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' +
  542. 'Expression: [].toString.constructor.foo');
  543. expect(function() {
  544. scope.$eval('{}.toString["constructor"]');
  545. }).toThrowMinErr(
  546. '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' +
  547. 'Expression: {}.toString["constructor"]');
  548. expect(function() {
  549. scope.$eval('{}["toString"]["constructor"]');
  550. }).toThrowMinErr(
  551. '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' +
  552. 'Expression: {}["toString"]["constructor"]');
  553. scope.a = [];
  554. expect(function() {
  555. scope.$eval('a.toString.constructor', scope);
  556. }).toThrowMinErr(
  557. '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' +
  558. 'Expression: a.toString.constructor');
  559. expect(function() {
  560. scope.$eval('a.toString["constructor"]', scope);
  561. }).toThrowMinErr(
  562. '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' +
  563. 'Expression: a.toString["constructor"]');
  564. });
  565. it('should NOT allow access to Function constructor in setter', function() {
  566. expect(function() {
  567. scope.$eval('{}.toString.constructor = 1');
  568. }).toThrowMinErr(
  569. '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' +
  570. 'Expression: {}.toString.constructor = 1');
  571. expect(function() {
  572. scope.$eval('{}.toString.constructor.a = 1');
  573. }).toThrowMinErr(
  574. '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' +
  575. 'Expression: {}.toString.constructor.a = 1');
  576. expect(function() {
  577. scope.$eval('{}.toString["constructor"]["constructor"] = 1');
  578. }).toThrowMinErr(
  579. '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' +
  580. 'Expression: {}.toString["constructor"]["constructor"] = 1');
  581. scope.key1 = "const";
  582. scope.key2 = "ructor";
  583. expect(function() {
  584. scope.$eval('{}.toString[key1 + key2].foo = 1');
  585. }).toThrowMinErr(
  586. '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' +
  587. 'Expression: {}.toString[key1 + key2].foo = 1');
  588. expect(function() {
  589. scope.$eval('{}.toString["constructor"]["a"] = 1');
  590. }).toThrowMinErr(
  591. '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' +
  592. 'Expression: {}.toString["constructor"]["a"] = 1');
  593. scope.a = [];
  594. expect(function() {
  595. scope.$eval('a.toString.constructor = 1', scope);
  596. }).toThrowMinErr(
  597. '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' +
  598. 'Expression: a.toString.constructor = 1');
  599. });
  600. it('should NOT allow access to Function constructor that has been aliased', function() {
  601. scope.foo = { "bar": Function };
  602. expect(function() {
  603. scope.$eval('foo["bar"]');
  604. }).toThrowMinErr(
  605. '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' +
  606. 'Expression: foo["bar"]');
  607. });
  608. it('should NOT allow access to Function constructor in getter', function() {
  609. expect(function() {
  610. scope.$eval('{}.toString.constructor');
  611. }).toThrowMinErr(
  612. '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' +
  613. 'Expression: {}.toString.constructor');
  614. });
  615. });
  616. describe('Window and $element/node', function() {
  617. it('should NOT allow access to the Window or DOM when indexing', inject(function($window, $document) {
  618. scope.wrap = {w: $window, d: $document};
  619. expect(function() {
  620. scope.$eval('wrap["w"]', scope);
  621. }).toThrowMinErr(
  622. '$parse', 'isecwindow', 'Referencing the Window in Angular expressions is ' +
  623. 'disallowed! Expression: wrap["w"]');
  624. expect(function() {
  625. scope.$eval('wrap["d"]', scope);
  626. }).toThrowMinErr(
  627. '$parse', 'isecdom', 'Referencing DOM nodes in Angular expressions is ' +
  628. 'disallowed! Expression: wrap["d"]');
  629. }));
  630. it('should NOT allow access to the Window or DOM returned from a function', inject(function($window, $document) {
  631. scope.getWin = valueFn($window);
  632. scope.getDoc = valueFn($document);
  633. expect(function() {
  634. scope.$eval('getWin()', scope);
  635. }).toThrowMinErr(
  636. '$parse', 'isecwindow', 'Referencing the Window in Angular expressions is ' +
  637. 'disallowed! Expression: getWin()');
  638. expect(function() {
  639. scope.$eval('getDoc()', scope);
  640. }).toThrowMinErr(
  641. '$parse', 'isecdom', 'Referencing DOM nodes in Angular expressions is ' +
  642. 'disallowed! Expression: getDoc()');
  643. }));
  644. it('should NOT allow calling functions on Window or DOM', inject(function($window, $document) {
  645. scope.a = {b: { win: $window, doc: $document }};
  646. expect(function() {
  647. scope.$eval('a.b.win.alert(1)', scope);
  648. }).toThrowMinErr(
  649. '$parse', 'isecwindow', 'Referencing the Window in Angular expressions is ' +
  650. 'disallowed! Expression: a.b.win.alert(1)');
  651. expect(function() {
  652. scope.$eval('a.b.doc.on("click")', scope);
  653. }).toThrowMinErr(
  654. '$parse', 'isecdom', 'Referencing DOM nodes in Angular expressions is ' +
  655. 'disallowed! Expression: a.b.doc.on("click")');
  656. }));
  657. // Issue #4805
  658. it('should NOT throw isecdom when referencing a Backbone Collection', function() {
  659. // Backbone stuff is sort of hard to mock, if you have a better way of doing this,
  660. // please fix this.
  661. var fakeBackboneCollection = {
  662. children: [{}, {}, {}],
  663. find: function() {},
  664. on: function() {},
  665. off: function() {},
  666. bind: function() {}
  667. };
  668. scope.backbone = fakeBackboneCollection;
  669. expect(function() { scope.$eval('backbone'); }).not.toThrow();
  670. });
  671. it('should NOT throw isecdom when referencing an array with node properties', function() {
  672. var array = [1,2,3];
  673. array.on = array.attr = array.prop = array.bind = true;
  674. scope.array = array;
  675. expect(function() { scope.$eval('array'); }).not.toThrow();
  676. });
  677. });
  678. });
  679. describe('overriding constructor', function() {
  680. it('should evaluate grouped expressions', function() {
  681. scope.foo = function foo() {
  682. return "foo";
  683. };
  684. // When not overridden, access should be restricted both by the dot operator and by the
  685. // index operator.
  686. expect(function() {
  687. scope.$eval('foo.constructor()', scope);
  688. }).toThrowMinErr(
  689. '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' +
  690. 'Expression: foo.constructor()');
  691. expect(function() {
  692. scope.$eval('foo["constructor"]()', scope);
  693. }).toThrowMinErr(
  694. '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' +
  695. 'Expression: foo["constructor"]()');
  696. // User defined value assigned to constructor.
  697. scope.foo.constructor = function constructor() {
  698. return "custom constructor";
  699. };
  700. // Dot operator should still block it.
  701. expect(function() {
  702. scope.$eval('foo.constructor()', scope);
  703. }).toThrowMinErr(
  704. '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' +
  705. 'Expression: foo.constructor()');
  706. // However, the index operator should allow it.
  707. expect(scope.$eval('foo["constructor"]()', scope)).toBe('custom constructor');
  708. });
  709. });
  710. it('should call the function from the received instance and not from a new one', function() {
  711. var n = 0;
  712. scope.fn = function() {
  713. var c = n++;
  714. return { c: c, anotherFn: function() { return this.c == c; } };
  715. };
  716. expect(scope.$eval('fn().anotherFn()')).toBe(true);
  717. });
  718. it('should call the function once when it is part of the context', function() {
  719. var count = 0;
  720. scope.fn = function() {
  721. count++;
  722. return { anotherFn: function() { return "lucas"; } };
  723. };
  724. expect(scope.$eval('fn().anotherFn()')).toBe('lucas');
  725. expect(count).toBe(1);
  726. });
  727. it('should call the function once when it is not part of the context', function() {
  728. var count = 0;
  729. scope.fn = function() {
  730. count++;
  731. return function() { return 'lucas'; };
  732. };
  733. expect(scope.$eval('fn()()')).toBe('lucas');
  734. expect(count).toBe(1);
  735. });
  736. it('should call the function once when it is part of the context on assignments', function() {
  737. var count = 0;
  738. var element = {};
  739. scope.fn = function() {
  740. count++;
  741. return element;
  742. };
  743. expect(scope.$eval('fn().name = "lucas"')).toBe('lucas');
  744. expect(element.name).toBe('lucas');
  745. expect(count).toBe(1);
  746. });
  747. it('should call the function once when it is part of the context on array lookups', function() {
  748. var count = 0;
  749. var element = [];
  750. scope.fn = function() {
  751. count++;
  752. return element;
  753. };
  754. expect(scope.$eval('fn()[0] = "lucas"')).toBe('lucas');
  755. expect(element[0]).toBe('lucas');
  756. expect(count).toBe(1);
  757. });
  758. it('should call the function once when it is part of the context on array lookup function', function() {
  759. var count = 0;
  760. var element = [{anotherFn: function() { return 'lucas';} }];
  761. scope.fn = function() {
  762. count++;
  763. return element;
  764. };
  765. expect(scope.$eval('fn()[0].anotherFn()')).toBe('lucas');
  766. expect(count).toBe(1);
  767. });
  768. it('should call the function once when it is part of the context on property lookup function', function() {
  769. var count = 0;
  770. var element = {name: {anotherFn: function() { return 'lucas';} } };
  771. scope.fn = function() {
  772. count++;
  773. return element;
  774. };
  775. expect(scope.$eval('fn().name.anotherFn()')).toBe('lucas');
  776. expect(count).toBe(1);
  777. });
  778. it('should call the function once when it is part of a sub-expression', function() {
  779. var count = 0;
  780. scope.element = [{}];
  781. scope.fn = function() {
  782. count++;
  783. return 0;
  784. };
  785. expect(scope.$eval('element[fn()].name = "lucas"')).toBe('lucas');
  786. expect(scope.element[0].name).toBe('lucas');
  787. expect(count).toBe(1);
  788. });
  789. describe('assignable', function() {
  790. it('should expose assignment function', inject(function($parse) {
  791. var fn = $parse('a');
  792. expect(fn.assign).toBeTruthy();
  793. var scope = {};
  794. fn.assign(scope, 123);
  795. expect(scope).toEqual({a:123});
  796. }));
  797. });
  798. describe('one-time binding', function() {
  799. it('should only use the cache when it is not a one-time binding', inject(function($parse) {
  800. expect($parse('foo')).toBe($parse('foo'));
  801. expect($parse('::foo')).not.toBe($parse('::foo'));
  802. }));
  803. it('should stay stable once the value defined', inject(function($parse, $rootScope) {
  804. var fn = $parse('::foo');
  805. expect(fn.$$unwatch).not.toBe(true);
  806. $rootScope.$watch(fn);
  807. $rootScope.$digest();
  808. expect(fn.$$unwatch).not.toBe(true);
  809. $rootScope.foo = 'bar';
  810. $rootScope.$digest();
  811. expect(fn.$$unwatch).toBe(true);
  812. expect(fn($rootScope)).toBe('bar');
  813. expect(fn()).toBe('bar');
  814. $rootScope.foo = 'man';
  815. $rootScope.$digest();
  816. expect(fn.$$unwatch).toBe(true);
  817. expect(fn($rootScope)).toBe('bar');
  818. expect(fn()).toBe('bar');
  819. }));
  820. it('should have a stable value if at the end of a $digest it has a defined value', inject(function($parse, $rootScope) {
  821. var fn = $parse('::foo');
  822. $rootScope.$watch(fn);
  823. $rootScope.$watch('foo', function() { if ($rootScope.foo === 'bar') {$rootScope.foo = undefined; } });
  824. $rootScope.foo = 'bar';
  825. $rootScope.$digest();
  826. expect(fn.$$unwatch).toBe(false);
  827. $rootScope.foo = 'man';
  828. $rootScope.$digest();
  829. expect(fn.$$unwatch).toBe(true);
  830. expect(fn($rootScope)).toBe('man');
  831. expect(fn()).toBe('man');
  832. $rootScope.foo = 'shell';
  833. $rootScope.$digest();
  834. expect(fn.$$unwatch).toBe(true);
  835. expect(fn($rootScope)).toBe('man');
  836. expect(fn()).toBe('man');
  837. }));
  838. it('should keep a copy of the stable element', inject(function($parse, $rootScope) {
  839. var fn = $parse('::foo'),
  840. value = {bar: 'bar'};
  841. $rootScope.$watch(fn);
  842. $rootScope.foo = value;
  843. $rootScope.$digest();
  844. value.baz = 'baz';
  845. expect(fn()).toEqual({bar: 'bar'});
  846. }));
  847. it('should not throw if the stable value is `null`', inject(function($parse, $rootScope) {
  848. var fn = $parse('::foo');
  849. $rootScope.$watch(fn);
  850. $rootScope.foo = null;
  851. $rootScope.$digest();
  852. $rootScope.foo = 'foo';
  853. $rootScope.$digest();
  854. expect(fn()).toEqual(null);
  855. }));
  856. });
  857. describe('locals', function() {
  858. it('should expose local variables', inject(function($parse) {
  859. expect($parse('a')({a: 0}, {a: 1})).toEqual(1);
  860. expect($parse('add(a,b)')({b: 1, add: function(a, b) { return a + b; }}, {a: 2})).toEqual(3);
  861. }));
  862. it('should expose traverse locals', inject(function($parse) {
  863. expect($parse('a.b')({a: {b: 0}}, {a: {b:1}})).toEqual(1);
  864. expect($parse('a.b')({a: null}, {a: {b:1}})).toEqual(1);
  865. expect($parse('a.b')({a: {b: 0}}, {a: null})).toEqual(undefined);
  866. expect($parse('a.b.c')({a: null}, {a: {b: {c: 1}}})).toEqual(1);
  867. }));
  868. it('should not use locals to resolve object properties', inject(function($parse) {
  869. expect($parse('a[0].b')({a: [ {b : 'scope'} ]}, {b : 'locals'})).toBe('scope');
  870. expect($parse('a[0]["b"]')({a: [ {b : 'scope'} ]}, {b : 'locals'})).toBe('scope');
  871. expect($parse('a[0][0].b')({a: [[{b : 'scope'}]]}, {b : 'locals'})).toBe('scope');
  872. expect($parse('a[0].b.c')({a: [ {b: {c: 'scope'}}] }, {b : {c: 'locals'} })).toBe('scope');
  873. }));
  874. });
  875. describe('literal', function() {
  876. it('should mark scalar value expressions as literal', inject(function($parse) {
  877. expect($parse('0').literal).toBe(true);
  878. expect($parse('"hello"').literal).toBe(true);
  879. expect($parse('true').literal).toBe(true);
  880. expect($parse('false').literal).toBe(true);
  881. expect($parse('null').literal).toBe(true);
  882. expect($parse('undefined').literal).toBe(true);
  883. }));
  884. it('should mark array expressions as literal', inject(function($parse) {
  885. expect($parse('[]').literal).toBe(true);
  886. expect($parse('[1, 2, 3]').literal).toBe(true);
  887. expect($parse('[1, identifier]').literal).toBe(true);
  888. }));
  889. it('should mark object expressions as literal', inject(function($parse) {
  890. expect($parse('{}').literal).toBe(true);
  891. expect($parse('{x: 1}').literal).toBe(true);
  892. expect($parse('{foo: bar}').literal).toBe(true);
  893. }));
  894. it('should not mark function calls or operator expressions as literal', inject(function($parse) {
  895. expect($parse('1 + 1').literal).toBe(false);
  896. expect($parse('call()').literal).toBe(false);
  897. expect($parse('[].length').literal).toBe(false);
  898. }));
  899. });
  900. describe('constant', function() {
  901. it('should mark scalar value expressions as constant', inject(function($parse) {
  902. expect($parse('12.3').constant).toBe(true);
  903. expect($parse('"string"').constant).toBe(true);
  904. expect($parse('true').constant).toBe(true);
  905. expect($parse('false').constant).toBe(true);
  906. expect($parse('null').constant).toBe(true);
  907. expect($parse('undefined').constant).toBe(true);
  908. }));
  909. it('should mark arrays as constant if they only contain constant elements', inject(function($parse) {
  910. expect($parse('[]').constant).toBe(true);
  911. expect($parse('[1, 2, 3]').constant).toBe(true);
  912. expect($parse('["string", null]').constant).toBe(true);
  913. expect($parse('[[]]').constant).toBe(true);
  914. expect($parse('[1, [2, 3], {4: 5}]').constant).toBe(true);
  915. }));
  916. it('should not mark arrays as constant if they contain any non-constant elements', inject(function($parse) {
  917. expect($parse('[foo]').constant).toBe(false);
  918. expect($parse('[x + 1]').constant).toBe(false);
  919. expect($parse('[bar[0]]').constant).toBe(false);
  920. }));
  921. it('should mark complex expressions involving constant values as constant', inject(function($parse) {
  922. expect($parse('!true').constant).toBe(true);
  923. expect($parse('-42').constant).toBe(true);
  924. expect($parse('1 - 1').constant).toBe(true);
  925. expect($parse('"foo" + "bar"').constant).toBe(true);
  926. expect($parse('5 != null').constant).toBe(true);
  927. expect($parse('{standard: 4/3, wide: 16/9}').constant).toBe(true);
  928. }));
  929. it('should not mark any expression involving variables or function calls as constant', inject(function($parse) {
  930. expect($parse('true.toString()').constant).toBe(false);
  931. expect($parse('foo(1, 2, 3)').constant).toBe(false);
  932. expect($parse('"name" + id').constant).toBe(false);
  933. }));
  934. });
  935. describe('null/undefined in expressions', function() {
  936. // simpleGetterFn1
  937. it('should return null for `a` where `a` is null', inject(function($rootScope) {
  938. $rootScope.a = null;
  939. expect($rootScope.$eval('a')).toBe(null);
  940. }));
  941. it('should return undefined for `a` where `a` is undefined', inject(function($rootScope) {
  942. expect($rootScope.$eval('a')).toBeUndefined();
  943. }));
  944. // simpleGetterFn2
  945. it('should return undefined for properties of `null` constant', inject(function($rootScope) {
  946. expect($rootScope.$eval('null.a')).toBeUndefined();
  947. }));
  948. it('should return undefined for properties of `null` values', inject(function($rootScope) {
  949. $rootScope.a = null;
  950. expect($rootScope.$eval('a.b')).toBeUndefined();
  951. }));
  952. it('should return null for `a.b` where `b` is null', inject(function($rootScope) {
  953. $rootScope.a = { b: null };
  954. expect($rootScope.$eval('a.b')).toBe(null);
  955. }));
  956. // cspSafeGetter && pathKeys.length < 6 || pathKeys.length > 2
  957. it('should return null for `a.b.c.d.e` where `e` is null', inject(function($rootScope) {
  958. $rootScope.a = { b: { c: { d: { e: null } } } };
  959. expect($rootScope.$eval('a.b.c.d.e')).toBe(null);
  960. }));
  961. it('should return undefined for `a.b.c.d.e` where `d` is null', inject(function($rootScope) {
  962. $rootScope.a = { b: { c: { d: null } } };
  963. expect($rootScope.$eval('a.b.c.d.e')).toBeUndefined();
  964. }));
  965. // cspSafeGetter || pathKeys.length > 6
  966. it('should return null for `a.b.c.d.e.f.g` where `g` is null', inject(function($rootScope) {
  967. $rootScope.a = { b: { c: { d: { e: { f: { g: null } } } } } };
  968. expect($rootScope.$eval('a.b.c.d.e.f.g')).toBe(null);
  969. }));
  970. it('should return undefined for `a.b.c.d.e.f.g` where `f` is null', inject(function($rootScope) {
  971. $rootScope.a = { b: { c: { d: { e: { f: null } } } } };
  972. expect($rootScope.$eval('a.b.c.d.e.f.g')).toBeUndefined();
  973. }));
  974. it('should return undefined if the return value of a function invocation is undefined',
  975. inject(function($rootScope) {
  976. $rootScope.fn = function() {};
  977. expect($rootScope.$eval('fn()')).toBeUndefined();
  978. }));
  979. it('should ignore undefined values when doing addition/concatenation',
  980. inject(function($rootScope) {
  981. $rootScope.fn = function() {};
  982. expect($rootScope.$eval('foo + "bar" + fn()')).toBe('bar');
  983. }));
  984. });
  985. });
  986. });
  987. });