'use strict'; describe('parser', function() { describe('lexer', function() { it('should tokenize a string', function() { var tokens = lex("a.bc[22]+1.3|f:'a\\\'c':\"d\\\"e\""); var i = 0; expect(tokens[i].index).toEqual(0); expect(tokens[i].text).toEqual('a.bc'); i++; expect(tokens[i].index).toEqual(4); expect(tokens[i].text).toEqual('['); i++; expect(tokens[i].index).toEqual(5); expect(tokens[i].text).toEqual(22); i++; expect(tokens[i].index).toEqual(7); expect(tokens[i].text).toEqual(']'); i++; expect(tokens[i].index).toEqual(8); expect(tokens[i].text).toEqual('+'); i++; expect(tokens[i].index).toEqual(9); expect(tokens[i].text).toEqual(1.3); i++; expect(tokens[i].index).toEqual(12); expect(tokens[i].text).toEqual('|'); i++; expect(tokens[i].index).toEqual(13); expect(tokens[i].text).toEqual('f'); i++; expect(tokens[i].index).toEqual(14); expect(tokens[i].text).toEqual(':'); i++; expect(tokens[i].index).toEqual(15); expect(tokens[i].string).toEqual("a'c"); i++; expect(tokens[i].index).toEqual(21); expect(tokens[i].text).toEqual(':'); i++; expect(tokens[i].index).toEqual(22); expect(tokens[i].string).toEqual('d"e'); }); it('should tokenize undefined', function() { var tokens = lex("undefined"); var i = 0; expect(tokens[i].index).toEqual(0); expect(tokens[i].text).toEqual('undefined'); expect(undefined).toEqual(tokens[i].fn()); }); it('should tokenize quoted string', function() { var str = "['\\'', \"\\\"\"]"; var tokens = lex(str); expect(tokens[1].index).toEqual(1); expect(tokens[1].string).toEqual("'"); expect(tokens[3].index).toEqual(7); expect(tokens[3].string).toEqual('"'); }); it('should tokenize escaped quoted string', function() { var str = '"\\"\\n\\f\\r\\t\\v\\u00A0"'; var tokens = lex(str); expect(tokens[0].string).toEqual('"\n\f\r\t\v\u00A0'); }); it('should tokenize unicode', function() { var tokens = lex('"\\u00A0"'); expect(tokens.length).toEqual(1); expect(tokens[0].string).toEqual('\u00a0'); }); it('should ignore whitespace', function() { var tokens = lex("a \t \n \r b"); expect(tokens[0].text).toEqual('a'); expect(tokens[1].text).toEqual('b'); }); it('should tokenize relation and equality', function() { var tokens = lex("! == != < > <= >= === !=="); expect(tokens[0].text).toEqual('!'); expect(tokens[1].text).toEqual('=='); expect(tokens[2].text).toEqual('!='); expect(tokens[3].text).toEqual('<'); expect(tokens[4].text).toEqual('>'); expect(tokens[5].text).toEqual('<='); expect(tokens[6].text).toEqual('>='); expect(tokens[7].text).toEqual('==='); expect(tokens[8].text).toEqual('!=='); }); it('should tokenize logical and ternary', function() { var tokens = lex("&& || ? :"); expect(tokens[0].text).toEqual('&&'); expect(tokens[1].text).toEqual('||'); expect(tokens[2].text).toEqual('?'); expect(tokens[3].text).toEqual(':'); }); it('should tokenize statements', function() { var tokens = lex("a;b;"); expect(tokens[0].text).toEqual('a'); expect(tokens[1].text).toEqual(';'); expect(token
angular.module("ngLocale", [], ["$provide", function($provide) {
var PLURAL_CATEGORY = {ZERO: "zero", ONE: "one", TWO: "two", FEW: "few", MANY: "many", OTHER: "other"};
$provide.value("$locale", {"NUMBER_FORMATS":{"DECIMAL_SEP":",","GROUP_SEP":".","PATTERNS":[{"minInt":1,"minFrac":0,"macFrac":0,"posPre":"","posSuf":"","negPre":"-","negSuf":"","gSize":3,"lgSize":3,"maxFrac":3},{"minInt":1,"minFrac":2,"macFrac":0,"posPre":"","posSuf":" \u00A4","negPre":"-","negSuf":" \u00A4","gSize":3,"lgSize":3,"maxFrac":2}],"CURRENCY_SYM":"€"},"pluralCat":function (n) {  if (n == 1) {    return PLURAL_CATEGORY.ONE;  }  return PLURAL_CATEGORY.OTHER;},"DATETIME_FORMATS":{"MONTH":["Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember"],"SHORTMONTH":["Jan","Feb","Mär","Apr","Mai","Jun","Jul","Aug","Sep","Okt","Nov","Dez"],"DAY":["Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag"],"SHORTDAY":["So.","Mo.","Di.","Mi.","Do.","Fr.","Sa."],"AMPMS":["vorm.","nachm."],"medium":"dd.MM.yyyy HH:mm:ss","short":"dd.MM.yy HH:mm","fullDate":"EEEE, d. MMMM y","longDate":"d. MMMM y","mediumDate":"dd.MM.yyyy","shortDate":"dd.MM.yy","mediumTime":"HH:mm:ss","shortTime":"HH:mm"},"id":"de-de"});
}]);
ect(scope.$eval("a=123; b=234")).toEqual(234); expect(scope.a).toEqual(123); expect(scope.b).toEqual(234); }); it('should evaluate function call without arguments', function() { scope['const'] = function(a,b){return 123;}; expect(scope.$eval("const()")).toEqual(123); }); it('should evaluate function call with arguments', function() { scope.add = function(a,b) { return a+b; }; expect(scope.$eval("add(1,2)")).toEqual(3); }); it('should evaluate function call from a return value', function() { scope.val = 33; scope.getter = function() { return function() { return this.val; }}; expect(scope.$eval("getter()()")).toBe(33); }); it('should evaluate multiplication and division', function() { scope.taxRate = 8; scope.subTotal = 100; expect(scope.$eval("taxRate / 100 * subTotal")).toEqual(8); expect(scope.$eval("subTotal * taxRate / 100")).toEqual(8); }); it('should evaluate array', function() { expect(scope.$eval("[]").length).toEqual(0); expect(scope.$eval("[1, 2]").length).toEqual(2); expect(scope.$eval("[1, 2]")[0]).toEqual(1); expect(scope.$eval("[1, 2]")[1]).toEqual(2); }); it('should evaluate array access', function() { expect(scope.$eval("[1][0]")).toEqual(1); expect(scope.$eval("[[1]][0][0]")).toEqual(1); expect(scope.$eval("[].length")).toEqual(0); expect(scope.$eval("[1, 2].length")).toEqual(2); }); it('should evaluate object', function() { expect(toJson(scope.$eval("{}"))).toEqual("{}"); expect(toJson(scope.$eval("{a:'b'}"))).toEqual('{"a":"b"}'); expect(toJson(scope.$eval("{'a':'b'}"))).toEqual('{"a":"b"}'); expect(toJson(scope.$eval("{\"a\":'b'}"))).toEqual('{"a":"b"}'); }); it('should evaluate object access', function() { expect(scope.$eval("{false:'WC', true:'CC'}[false]")).toEqual("WC"); }); it('should evaluate JSON', function() { expect(toJson(scope.$eval("[{}]"))).toEqual("[{}]"); expect(toJson(scope.$eval("[{a:[]}, {b:1}]"))).toEqual('[{"a":[]},{"b":1}]'); }); it('should evaluate multiple statements', function() { expect(scope.$eval("a=1;b=3;a+b")).toEqual(4); expect(scope.$eval(";;1;;")).toEqual(1); }); it('should evaluate object methods in correct context (this)', function() { var C = function () { this.a = 123; }; C.prototype.getA = function() { return this.a; }; scope.obj = new C(); expect(scope.$eval("obj.getA()")).toEqual(123); expect(scope.$eval("obj['getA']()")).toEqual(123); }); it('should evaluate methods in correct context (this) in argument', function() { var C = function () { this.a = 123; }; C.prototype.sum = function(value) { return this.a + value; }; C.prototype.getA = function() { return this.a; }; scope.obj = new C(); expect(scope.$eval("obj.sum(obj.getA())")).toEqual(246); expect(scope.$eval("obj['sum'](obj.getA())")).toEqual(246); }); it('should evaluate objects on scope context', function() { scope.a = "abc"; expect(scope.$eval("{a:a}").a).toEqual("abc"); }); it('should evaluate field access on function call result', function() { scope.a = function() { return {name:'misko'}; }; expect(scope.$eval("a().name")).toEqual("misko"); }); it('should evaluate field access after array access', function () { scope.items = [{}, {name:'misko'}]; expect(scope.$eval('items[1].name')).toEqual("misko"); }); it('should evaluate array assignment', function() { scope.items = []; expect(scope.$eval('items[1] = "abc"')).toEqual("abc"); expect(scope.$eval('items[1]')).toEqual("abc"); // Dont know how to make this work.... // expect(scope.$eval('books[1] = "moby"')).toEqual("moby"); // expect(scope.$eval('books[1]')).toEqual("moby"); }); it('should evaluate grouped filters', function() { scope.name = 'MISKO'; expect(scope.$eval('n = (name|lowercase)')).toEqual('misko'); expect(scope.$eval('n')).toEqual('misko'); }); it('should evaluate remainder', function() { expect(scope.$eval('1%2')).toEqual(1); }); it('should evaluate sum with undefined', function() { expect(scope.$eval('1+undefined')).toEqual(1); expect(scope.$eval('undefined+1')).toEqual(1); }); it('should throw exception on non-closed bracket', function() { expect(function() { scope.$eval('[].count('); }).toThrowMinErr('$parse', 'ueoe', 'Unexpected end of expression: [].count('); }); it('should evaluate double negation', function() { expect(scope.$eval('true')).toBeTruthy(); expect(scope.$eval('!true')).toBeFalsy(); expect(scope.$eval('!!true')).toBeTruthy(); expect(scope.$eval('{true:"a", false:"b"}[!!true]')).toEqual('a'); }); it('should evaluate negation', function() { expect(scope.$eval("!false || true")).toEqual(!false || true); expect(scope.$eval("!11 == 10")).toEqual(!11 == 10); expect(scope.$eval("12/6/2")).toEqual(12/6/2); }); it('should evaluate exclamation mark', function() { expect(scope.$eval('suffix = "!"')).toEqual('!'); }); it('should evaluate minus', function() { expect(scope.$eval("{a:'-'}")).toEqual({a: "-"}); }); it('should evaluate undefined', function() { expect(scope.$eval("undefined")).not.toBeDefined(); expect(scope.$eval("a=undefined")).not.toBeDefined(); expect(scope.a).not.toBeDefined(); }); it('should allow assignment after array dereference', function() { scope.obj = [{}]; scope.$eval('obj[0].name=1'); expect(scope.obj.name).toBeUndefined(); expect(scope.obj[0].name).toEqual(1); }); it('should short-circuit AND operator', function() { scope.run = function() { throw "IT SHOULD NOT HAVE RUN"; }; expect(scope.$eval('false && run()')).toBe(false); }); it('should short-circuit OR operator', function() { scope.run = function() { throw "IT SHOULD NOT HAVE RUN"; }; expect(scope.$eval('true || run()')).toBe(true); }); it('should support method calls on primitive types', function() { scope.empty = ''; scope.zero = 0; scope.bool = false; expect(scope.$eval('empty.substr(0)')).toBe(''); expect(scope.$eval('zero.toString()')).toBe('0'); expect(scope.$eval('bool.toString()')).toBe('false'); }); describe('sandboxing', function() { describe('Function constructor', function() { it('should NOT allow access to Function constructor in getter', function() { expect(function() { scope.$eval('{}.toString.constructor'); }).toThrowMinErr( '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' + 'Expression: {}.toString.constructor'); expect(function() { scope.$eval('{}.toString.constructor("alert(1)")'); }).toThrowMinErr( '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' + 'Expression: {}.toString.constructor("alert(1)")'); expect(function() { scope.$eval('[].toString.constructor.foo'); }).toThrowMinErr( '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' + 'Expression: [].toString.constructor.foo'); expect(function() { scope.$eval('{}.toString["constructor"]'); }).toThrowMinErr( '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' + 'Expression: {}.toString["constructor"]'); expect(function() { scope.$eval('{}["toString"]["constructor"]'); }).toThrowMinErr( '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' + 'Expression: {}["toString"]["constructor"]'); scope.a = []; expect(function() { scope.$eval('a.toString.constructor', scope); }).toThrowMinErr( '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' + 'Expression: a.toString.constructor'); expect(function() { scope.$eval('a.toString["constructor"]', scope); }).toThrowMinErr( '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' + 'Expression: a.toString["constructor"]'); }); it('should NOT allow access to Function constructor in setter', function() { expect(function() { scope.$eval('{}.toString.constructor = 1'); }).toThrowMinErr( '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' + 'Expression: {}.toString.constructor = 1'); expect(function() { scope.$eval('{}.toString.constructor.a = 1'); }).toThrowMinErr( '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' + 'Expression: {}.toString.constructor.a = 1'); expect(function() { scope.$eval('{}.toString["constructor"]["constructor"] = 1'); }).toThrowMinErr( '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' + 'Expression: {}.toString["constructor"]["constructor"] = 1'); scope.key1 = "const"; scope.key2 = "ructor"; expect(function() { scope.$eval('{}.toString[key1 + key2].foo = 1'); }).toThrowMinErr( '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' + 'Expression: {}.toString[key1 + key2].foo = 1'); expect(function() { scope.$eval('{}.toString["constructor"]["a"] = 1'); }).toThrowMinErr( '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' + 'Expression: {}.toString["constructor"]["a"] = 1'); scope.a = []; expect(function() { scope.$eval('a.toString.constructor = 1', scope); }).toThrowMinErr( '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' + 'Expression: a.toString.constructor = 1'); }); it('should NOT allow access to Function constructor that has been aliased', function() { scope.foo = { "bar": Function }; expect(function() { scope.$eval('foo["bar"]'); }).toThrowMinErr( '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' + 'Expression: foo["bar"]'); }); it('should NOT allow access to Function constructor in getter', function() { expect(function() { scope.$eval('{}.toString.constructor'); }).toThrowMinErr( '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' + 'Expression: {}.toString.constructor'); }); }); describe('Window and $element/node', function() { it('should NOT allow access to the Window or DOM when indexing', inject(function($window, $document) { scope.wrap = {w: $window, d: $document}; expect(function() { scope.$eval('wrap["w"]', scope); }).toThrowMinErr( '$parse', 'isecwindow', 'Referencing the Window in Angular expressions is ' + 'disallowed! Expression: wrap["w"]'); expect(function() { scope.$eval('wrap["d"]', scope); }).toThrowMinErr( '$parse', 'isecdom', 'Referencing DOM nodes in Angular expressions is ' + 'disallowed! Expression: wrap["d"]'); })); it('should NOT allow access to the Window or DOM returned from a function', inject(function($window, $document) { scope.getWin = valueFn($window); scope.getDoc = valueFn($document); expect(function() { scope.$eval('getWin()', scope); }).toThrowMinErr( '$parse', 'isecwindow', 'Referencing the Window in Angular expressions is ' + 'disallowed! Expression: getWin()'); expect(function() { scope.$eval('getDoc()', scope); }).toThrowMinErr( '$parse', 'isecdom', 'Referencing DOM nodes in Angular expressions is ' + 'disallowed! Expression: getDoc()'); })); }); }); describe('overriding constructor', function() { it('should evaluate grouped expressions', function() { scope.foo = function foo() { return "foo"; }; // When not overridden, access should be restricted both by the dot operator and by the // index operator. expect(function() { scope.$eval('foo.constructor()', scope) }).toThrowMinErr( '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' + 'Expression: foo.constructor()'); expect(function() { scope.$eval('foo["constructor"]()', scope) }).toThrowMinErr( '$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' + 'Expression: foo["constructor"]()'); // User defined value assigned to constructor. scope.foo.constructor = function constructor() { return "custom constructor"; } // Dot operator should still block it. expect(function() { scope.$eval('foo.constructor()', scope) }).toThrowMinErr( '$parse', 'isecfld', 'Referencing "constructor" field in Angular expressions is disallowed! ' + 'Expression: foo.constructor()'); // However, the index operator should allow it. expect(scope.$eval('foo["constructor"]()', scope)).toBe('custom constructor'); }); }); it('should call the function from the received instance and not from a new one', function() { var n = 0; scope.fn = function() { var c = n++; return { c: c, anotherFn: function() { return this.c == c; } }; }; expect(scope.$eval('fn().anotherFn()')).toBe(true); }); it('should call the function once when it is part of the context', function() { var count = 0; scope.fn = function() { count++; return { anotherFn: function() { return "lucas"; } }; }; expect(scope.$eval('fn().anotherFn()')).toBe('lucas'); expect(count).toBe(1); }); it('should call the function once when it is not part of the context', function() { var count = 0; scope.fn = function() { count++; return function() { return 'lucas'; }; }; expect(scope.$eval('fn()()')).toBe('lucas'); expect(count).toBe(1); }); it('should call the function once when it is part of the context on assignments', function() { var count = 0; var element = {}; scope.fn = function() { count++; return element; }; expect(scope.$eval('fn().name = "lucas"')).toBe('lucas'); expect(element.name).toBe('lucas'); expect(count).toBe(1); }); it('should call the function once when it is part of the context on array lookups', function() { var count = 0; var element = []; scope.fn = function() { count++; return element; }; expect(scope.$eval('fn()[0] = "lucas"')).toBe('lucas'); expect(element[0]).toBe('lucas'); expect(count).toBe(1); }); it('should call the function once when it is part of the context on array lookup function', function() { var count = 0; var element = [{anotherFn: function() { return 'lucas';} }]; scope.fn = function() { count++; return element; }; expect(scope.$eval('fn()[0].anotherFn()')).toBe('lucas'); expect(count).toBe(1); }); it('should call the function once when it is part of the context on property lookup function', function() { var count = 0; var element = {name: {anotherFn: function() { return 'lucas';} } }; scope.fn = function() { count++; return element; }; expect(scope.$eval('fn().name.anotherFn()')).toBe('lucas'); expect(count).toBe(1); }); it('should call the function once when it is part of a sub-expression', function() { var count = 0; scope.element = [{}]; scope.fn = function() { count++; return 0; }; expect(scope.$eval('element[fn()].name = "lucas"')).toBe('lucas'); expect(scope.element[0].name).toBe('lucas'); expect(count).toBe(1); }); describe('promises', function() { var deferred, promise, q; beforeEach(inject(function($q) { q = $q; deferred = q.defer(); promise = deferred.promise; })); describe('{{promise}}', function() { it('should evaluated resolved promise and get its value', function() { deferred.resolve('hello!'); scope.greeting = promise; expect(scope.$eval('greeting')).toBe(undefined); scope.$digest(); expect(scope.$eval('greeting')).toBe('hello!'); }); it('should evaluated rejected promise and ignore the rejection reason', function() { deferred.reject('sorry'); scope.greeting = promise; expect(scope.$eval('gretting')).toBe(undefined); scope.$digest(); expect(scope.$eval('greeting')).toBe(undefined); }); it('should evaluate a promise and eventualy get its value', function() { scope.greeting = promise; expect(scope.$eval('greeting')).toBe(undefined); scope.$digest(); expect(scope.$eval('greeting')).toBe(undefined); deferred.resolve('hello!'); expect(scope.$eval('greeting')).toBe(undefined); scope.$digest(); expect(scope.$eval('greeting')).toBe('hello!'); }); it('should evaluate a promise and eventualy ignore its rejection', function() { scope.greeting = promise; expect(scope.$eval('greeting')).toBe(undefined); scope.$digest(); expect(scope.$eval('greeting')).toBe(undefined); deferred.reject('sorry'); expect(scope.$eval('greeting')).toBe(undefined); scope.$digest(); expect(scope.$eval('greeting')).toBe(undefined); }); it('should evaluate a function call returning a promise and eventually get its return value', function() { scope.greetingFn = function() { return promise; }; expect(scope.$eval('greetingFn()')).toBe(undefined); scope.$digest(); expect(scope.$eval('greetingFn()')).toBe(undefined); deferred.resolve('hello!'); expect(scope.$eval('greetingFn()')).toBe(undefined); scope.$digest(); expect(scope.$eval('greetingFn()')).toBe('hello!'); }); describe('assignment into promises', function() { // This behavior is analogous to assignments to non-promise values // that are lazily set on the scope. it('should evaluate a resolved object promise and set its value', inject(function($parse) { scope.person = promise; deferred.resolve({'name': 'Bill Gates'}); var getter = $parse('person.name'); expect(getter(scope)).toBe(undefined); scope.$digest(); expect(getter(scope)).toBe('Bill Gates'); getter.assign(scope, 'Warren Buffet'); expect(getter(scope)).toBe('Warren Buffet'); })); it('should evaluate a resolved primitive type promise and set its value', inject(function($parse) { scope.greeting = promise; deferred.resolve('Salut!'); var getter = $parse('greeting'); expect(getter(scope)).toBe(undefined); scope.$digest(); expect(getter(scope)).toBe('Salut!'); getter.assign(scope, 'Bonjour'); expect(getter(scope)).toBe('Bonjour'); })); it('should evaluate an unresolved promise and set and remember its value', inject(function($parse) { scope.person = promise; var getter = $parse('person.name'); expect(getter(scope)).toBe(undefined); scope.$digest(); expect(getter(scope)).toBe(undefined); getter.assign(scope, 'Bonjour'); scope.$digest(); expect(getter(scope)).toBe('Bonjour'); var c1Getter = $parse('person.A.B.C1'); scope.$digest(); expect(c1Getter(scope)).toBe(undefined); c1Getter.assign(scope, 'c1_value'); scope.$digest(); expect(c1Getter(scope)).toBe('c1_value'); // Set another property on the person.A.B var c2Getter = $parse('person.A.B.C2'); scope.$digest(); expect(c2Getter(scope)).toBe(undefined); c2Getter.assign(scope, 'c2_value'); scope.$digest(); expect(c2Getter(scope)).toBe('c2_value'); // c1 should be unchanged. expect($parse('person.A')(scope)).toEqual( {B: {C1: 'c1_value', C2: 'c2_value'}}); })); it('should evaluate a resolved promise and overwrite the previous set value in the absense of the getter', inject(function($parse) { scope.person = promise; var c1Getter = $parse('person.A.B.C1'); c1Getter.assign(scope, 'c1_value'); // resolving the promise should update the tree. deferred.resolve({A: {B: {C1: 'resolved_c1'}}}); scope.$digest(); expect(c1Getter(scope)).toEqual('resolved_c1'); })); }); }); describe('dereferencing', function() { it('should evaluate and dereference properties leading to and from a promise', function() { scope.obj = {greeting: promise}; expect(scope.$eval('obj.greeting')).toBe(undefined); expect(scope.$eval('obj.greeting.polite')).toBe(undefined); scope.$digest(); expect(scope.$eval('obj.greeting')).toBe(undefined); expect(scope.$eval('obj.greeting.polite')).toBe(undefined); deferred.resolve({polite: 'Good morning!'}); scope.$digest(); expect(scope.$eval('obj.greeting')).toEqual({polite: 'Good morning!'}); expect(scope.$eval('obj.greeting.polite')).toBe('Good morning!'); }); it('should evaluate and dereference properties leading to and from a promise via bracket ' + 'notation', function() { scope.obj = {greeting: promise}; expect(scope.$eval('obj["greeting"]')).toBe(undefined); expect(scope.$eval('obj["greeting"]["polite"]')).toBe(undefined); scope.$digest(); expect(scope.$eval('obj["greeting"]')).toBe(undefined); expect(scope.$eval('obj["greeting"]["polite"]')).toBe(undefined); deferred.resolve({polite: 'Good morning!'}); scope.$digest(); expect(scope.$eval('obj["greeting"]')).toEqual({polite: 'Good morning!'}); expect(scope.$eval('obj["greeting"]["polite"]')).toBe('Good morning!'); }); it('should evaluate and dereference array references leading to and from a promise', function() { scope.greetings = [promise]; expect(scope.$eval('greetings[0]')).toBe(undefined); expect(scope.$eval('greetings[0][0]')).toBe(undefined); scope.$digest(); expect(scope.$eval('greetings[0]')).toBe(undefined); expect(scope.$eval('greetings[0][0]')).toBe(undefined); deferred.resolve(['Hi!', 'Cau!']); scope.$digest(); expect(scope.$eval('greetings[0]')).toEqual(['Hi!', 'Cau!']); expect(scope.$eval('greetings[0][0]')).toBe('Hi!'); }); it('should evaluate and dereference promises used as function arguments', function() { scope.greet = function(name) { return 'Hi ' + name + '!'; }; scope.name = promise; expect(scope.$eval('greet(name)')).toBe('Hi undefined!'); scope.$digest(); expect(scope.$eval('greet(name)')).toBe('Hi undefined!'); deferred.resolve('Veronica'); expect(scope.$eval('greet(name)')).toBe('Hi undefined!'); scope.$digest(); expect(scope.$eval('greet(name)')).toBe('Hi Veronica!'); }); it('should evaluate and dereference promises used as array indexes', function() { scope.childIndex = promise; scope.kids = ['Adam', 'Veronica', 'Elisa']; expect(scope.$eval('kids[childIndex]')).toBe(undefined); scope.$digest(); expect(scope.$eval('kids[childIndex]')).toBe(undefined); deferred.resolve(1); expect(scope.$eval('kids[childIndex]')).toBe(undefined); scope.$digest(); expect(scope.$eval('kids[childIndex]')).toBe('Veronica'); }); it('should evaluate and dereference promises used as keys in bracket notation', function() { scope.childKey = promise; scope.kids = {'a': 'Adam', 'v': 'Veronica', 'e': 'Elisa'}; expect(scope.$eval('kids[childKey]')).toBe(undefined); scope.$digest(); expect(scope.$eval('kids[childKey]')).toBe(undefined); deferred.resolve('v'); expect(scope.$eval('kids[childKey]')).toBe(undefined); scope.$digest(); expect(scope.$eval('kids[childKey]')).toBe('Veronica'); }); it('should not mess with the promise if it was not directly evaluated', function() { scope.obj = {greeting: promise, username: 'hi'}; var obj = scope.$eval('obj'); expect(obj.username).toEqual('hi'); expect(typeof obj.greeting.then).toBe('function'); }); }); }); describe('assignable', function() { it('should expose assignment function', inject(function($parse) { var fn = $parse('a'); expect(fn.assign).toBeTruthy(); var scope = {}; fn.assign(scope, 123); expect(scope).toEqual({a:123}); })); }); describe('locals', function() { it('should expose local variables', inject(function($parse) { expect($parse('a')({a: 0}, {a: 1})).toEqual(1); expect($parse('add(a,b)')({b: 1, add: function(a, b) { return a + b; }}, {a: 2})).toEqual(3); })); it('should expose traverse locals', inject(function($parse) { expect($parse('a.b')({a: {b: 0}}, {a: {b:1}})).toEqual(1); expect($parse('a.b')({a: null}, {a: {b:1}})).toEqual(1); expect($parse('a.b')({a: {b: 0}}, {a: null})).toEqual(undefined); })); }); describe('literal', function() { it('should mark scalar value expressions as literal', inject(function($parse) { expect($parse('0').literal).toBe(true); expect($parse('"hello"').literal).toBe(true); expect($parse('true').literal).toBe(true); expect($parse('false').literal).toBe(true); expect($parse('null').literal).toBe(true); expect($parse('undefined').literal).toBe(true); })); it('should mark array expressions as literal', inject(function($parse) { expect($parse('[]').literal).toBe(true); expect($parse('[1, 2, 3]').literal).toBe(true); expect($parse('[1, identifier]').literal).toBe(true); })); it('should mark object expressions as literal', inject(function($parse) { expect($parse('{}').literal).toBe(true); expect($parse('{x: 1}').literal).toBe(true); expect($parse('{foo: bar}').literal).toBe(true); })); it('should not mark function calls or operator expressions as literal', inject(function($parse) { expect($parse('1 + 1').literal).toBe(false); expect($parse('call()').literal).toBe(false); expect($parse('[].length').literal).toBe(false); })); }); describe('constant', function() { it('should mark scalar value expressions as constant', inject(function($parse) { expect($parse('12.3').constant).toBe(true); expect($parse('"string"').constant).toBe(true); expect($parse('true').constant).toBe(true); expect($parse('false').constant).toBe(true); expect($parse('null').constant).toBe(true); expect($parse('undefined').constant).toBe(true); })); it('should mark arrays as constant if they only contain constant elements', inject(function($parse) { expect($parse('[]').constant).toBe(true); expect($parse('[1, 2, 3]').constant).toBe(true); expect($parse('["string", null]').constant).toBe(true); expect($parse('[[]]').constant).toBe(true); expect($parse('[1, [2, 3], {4: 5}]').constant).toBe(true); })); it('should not mark arrays as constant if they contain any non-constant elements', inject(function($parse) { expect($parse('[foo]').constant).toBe(false); expect($parse('[x + 1]').constant).toBe(false); expect($parse('[bar[0]]').constant).toBe(false); })); it('should mark complex expressions involving constant values as constant', inject(function($parse) { expect($parse('!true').constant).toBe(true); expect($parse('1 - 1').constant).toBe(true); expect($parse('"foo" + "bar"').constant).toBe(true); expect($parse('5 != null').constant).toBe(true); expect($parse('{standard: 4/3, wide: 16/9}').constant).toBe(true); })); it('should not mark any expression involving variables or function calls as constant', inject(function($parse) { expect($parse('true.toString()').constant).toBe(false); expect($parse('foo(1, 2, 3)').constant).toBe(false); expect($parse('"name" + id').constant).toBe(false); })); }); }); }); });