2010-01-06 00:36:58 +00:00
|
|
|
LexerTest = TestCase('LexerTest');
|
|
|
|
|
|
|
|
|
|
LexerTest.prototype.testTokenizeAString = function(){
|
2010-01-09 23:02:43 +00:00
|
|
|
var lexer = new Lexer("a.bc[22]+1.3|f:'a\\\'c':\"d\\\"e\"");
|
2010-01-06 00:36:58 +00:00
|
|
|
var tokens = lexer.parse();
|
|
|
|
|
var i = 0;
|
|
|
|
|
assertEquals(tokens[i].index, 0);
|
|
|
|
|
assertEquals(tokens[i].text, 'a.bc');
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
assertEquals(tokens[i].index, 4);
|
|
|
|
|
assertEquals(tokens[i].text, '[');
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
assertEquals(tokens[i].index, 5);
|
|
|
|
|
assertEquals(tokens[i].text, 22);
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
assertEquals(tokens[i].index, 7);
|
|
|
|
|
assertEquals(tokens[i].text, ']');
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
assertEquals(tokens[i].index, 8);
|
|
|
|
|
assertEquals(tokens[i].text, '+');
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
assertEquals(tokens[i].index, 9);
|
|
|
|
|
assertEquals(tokens[i].text, 1.3);
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
assertEquals(tokens[i].index, 12);
|
|
|
|
|
assertEquals(tokens[i].text, '|');
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
assertEquals(tokens[i].index, 13);
|
|
|
|
|
assertEquals(tokens[i].text, 'f');
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
assertEquals(tokens[i].index, 14);
|
|
|
|
|
assertEquals(tokens[i].text, ':');
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
assertEquals(tokens[i].index, 15);
|
2010-03-24 17:35:01 +00:00
|
|
|
assertEquals(tokens[i].string, "a'c");
|
2010-01-06 00:36:58 +00:00
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
assertEquals(tokens[i].index, 21);
|
|
|
|
|
assertEquals(tokens[i].text, ':');
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
assertEquals(tokens[i].index, 22);
|
2010-03-24 17:35:01 +00:00
|
|
|
assertEquals(tokens[i].string, 'd"e');
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
2010-03-25 20:01:08 +00:00
|
|
|
LexerTest.prototype.testTokenizeUndefined = function(){
|
|
|
|
|
var lexer = new Lexer("undefined");
|
|
|
|
|
var tokens = lexer.parse();
|
|
|
|
|
var i = 0;
|
|
|
|
|
assertEquals(tokens[i].index, 0);
|
|
|
|
|
assertEquals(tokens[i].text, 'undefined');
|
|
|
|
|
assertEquals(undefined, tokens[i].fn());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-01-06 00:36:58 +00:00
|
|
|
|
|
|
|
|
LexerTest.prototype.testTokenizeRegExp = function(){
|
2010-01-09 23:02:43 +00:00
|
|
|
var lexer = new Lexer("/r 1/");
|
2010-01-06 00:36:58 +00:00
|
|
|
var tokens = lexer.parse();
|
|
|
|
|
var i = 0;
|
|
|
|
|
assertEquals(tokens[i].index, 0);
|
|
|
|
|
assertEquals(tokens[i].text, 'r 1');
|
|
|
|
|
assertEquals("r 1".match(tokens[i].fn())[0], 'r 1');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LexerTest.prototype.testQuotedString = function(){
|
|
|
|
|
var str = "['\\'', \"\\\"\"]";
|
2010-01-09 23:02:43 +00:00
|
|
|
var lexer = new Lexer(str);
|
2010-01-06 00:36:58 +00:00
|
|
|
var tokens = lexer.parse();
|
|
|
|
|
|
|
|
|
|
assertEquals(1, tokens[1].index);
|
2010-03-24 17:35:01 +00:00
|
|
|
assertEquals("'", tokens[1].string);
|
2010-01-06 00:36:58 +00:00
|
|
|
|
|
|
|
|
assertEquals(7, tokens[3].index);
|
2010-03-24 17:35:01 +00:00
|
|
|
assertEquals('"', tokens[3].string);
|
2010-01-06 00:36:58 +00:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LexerTest.prototype.testQuotedStringEscape = function(){
|
|
|
|
|
var str = '"\\"\\n\\f\\r\\t\\v\\u00A0"';
|
2010-01-09 23:02:43 +00:00
|
|
|
var lexer = new Lexer(str);
|
2010-01-06 00:36:58 +00:00
|
|
|
var tokens = lexer.parse();
|
|
|
|
|
|
2010-03-24 17:35:01 +00:00
|
|
|
assertEquals('"\n\f\r\t\v\u00A0', tokens[0].string);
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LexerTest.prototype.testTokenizeUnicode = function(){
|
2010-01-09 23:02:43 +00:00
|
|
|
var lexer = new Lexer('"\\u00A0"');
|
2010-01-06 00:36:58 +00:00
|
|
|
var tokens = lexer.parse();
|
|
|
|
|
assertEquals(1, tokens.length);
|
2010-03-24 17:35:01 +00:00
|
|
|
assertEquals('\u00a0', tokens[0].string);
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LexerTest.prototype.testTokenizeRegExpWithOptions = function(){
|
2010-01-09 23:02:43 +00:00
|
|
|
var lexer = new Lexer("/r/g");
|
2010-01-06 00:36:58 +00:00
|
|
|
var tokens = lexer.parse();
|
|
|
|
|
var i = 0;
|
|
|
|
|
assertEquals(tokens[i].index, 0);
|
|
|
|
|
assertEquals(tokens[i].text, 'r');
|
|
|
|
|
assertEquals(tokens[i].flags, 'g');
|
|
|
|
|
assertEquals("rr".match(tokens[i].fn()).length, 2);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LexerTest.prototype.testTokenizeRegExpWithEscape = function(){
|
2010-01-09 23:02:43 +00:00
|
|
|
var lexer = new Lexer("/\\/\\d/");
|
2010-01-06 00:36:58 +00:00
|
|
|
var tokens = lexer.parse();
|
|
|
|
|
var i = 0;
|
|
|
|
|
assertEquals(tokens[i].index, 0);
|
|
|
|
|
assertEquals(tokens[i].text, '\\/\\d');
|
|
|
|
|
assertEquals("/1".match(tokens[i].fn())[0], '/1');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LexerTest.prototype.testIgnoreWhitespace = function(){
|
2010-01-09 23:02:43 +00:00
|
|
|
var lexer = new Lexer("a \t \n \r b");
|
2010-01-06 00:36:58 +00:00
|
|
|
var tokens = lexer.parse();
|
|
|
|
|
assertEquals(tokens[0].text, 'a');
|
|
|
|
|
assertEquals(tokens[1].text, 'b');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LexerTest.prototype.testRelation = function(){
|
2010-01-09 23:02:43 +00:00
|
|
|
var lexer = new Lexer("! == != < > <= >=");
|
2010-01-06 00:36:58 +00:00
|
|
|
var tokens = lexer.parse();
|
|
|
|
|
assertEquals(tokens[0].text, '!');
|
|
|
|
|
assertEquals(tokens[1].text, '==');
|
|
|
|
|
assertEquals(tokens[2].text, '!=');
|
|
|
|
|
assertEquals(tokens[3].text, '<');
|
|
|
|
|
assertEquals(tokens[4].text, '>');
|
|
|
|
|
assertEquals(tokens[5].text, '<=');
|
|
|
|
|
assertEquals(tokens[6].text, '>=');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LexerTest.prototype.testStatements = function(){
|
2010-01-09 23:02:43 +00:00
|
|
|
var lexer = new Lexer("a;b;");
|
2010-01-06 00:36:58 +00:00
|
|
|
var tokens = lexer.parse();
|
|
|
|
|
assertEquals(tokens[0].text, 'a');
|
|
|
|
|
assertEquals(tokens[1].text, ';');
|
|
|
|
|
assertEquals(tokens[2].text, 'b');
|
|
|
|
|
assertEquals(tokens[3].text, ';');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest = TestCase('ParserTest');
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testExpressions = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(scope.$eval("-1"), -1);
|
|
|
|
|
assertEquals(scope.$eval("1 + 2.5"), 3.5);
|
|
|
|
|
assertEquals(scope.$eval("1 + -2.5"), -1.5);
|
|
|
|
|
assertEquals(scope.$eval("1+2*3/4"), 1+2*3/4);
|
|
|
|
|
assertEquals(scope.$eval("0--1+1.5"), 0- -1 + 1.5);
|
|
|
|
|
assertEquals(scope.$eval("-0--1++2*-3/-4"), -0- -1+ +2*-3/-4);
|
|
|
|
|
assertEquals(scope.$eval("1/2*3"), 1/2*3);
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testComparison = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(scope.$eval("false"), false);
|
|
|
|
|
assertEquals(scope.$eval("!true"), false);
|
|
|
|
|
assertEquals(scope.$eval("1==1"), true);
|
|
|
|
|
assertEquals(scope.$eval("1!=2"), true);
|
|
|
|
|
assertEquals(scope.$eval("1<2"), true);
|
|
|
|
|
assertEquals(scope.$eval("1<=1"), true);
|
|
|
|
|
assertEquals(scope.$eval("1>2"), 1>2);
|
|
|
|
|
assertEquals(scope.$eval("2>=1"), 2>=1);
|
2010-02-12 22:16:33 +00:00
|
|
|
|
2010-04-04 00:04:36 +00:00
|
|
|
assertEquals(true === 2<3, scope.$eval("true==2<3"));
|
2010-02-12 22:16:33 +00:00
|
|
|
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testLogical = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(scope.$eval("0&&2"), 0&&2);
|
|
|
|
|
assertEquals(scope.$eval("0||2"), 0||2);
|
|
|
|
|
assertEquals(scope.$eval("0||1&&2"), 0||1&&2);
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testString = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(scope.$eval("'a' + 'b c'"), "ab c");
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testFilters = function(){
|
|
|
|
|
angular.filter.substring = function(input, start, end) {
|
|
|
|
|
return input.substring(start, end);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
angular.filter.upper = {_case:function(input) {
|
|
|
|
|
return input.toUpperCase();
|
|
|
|
|
}};
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
2010-01-06 00:36:58 +00:00
|
|
|
try {
|
2010-03-30 03:25:42 +00:00
|
|
|
scope.$eval("1|nonExistant");
|
2010-01-06 00:36:58 +00:00
|
|
|
fail();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
assertEquals(e, "Function 'nonExistant' at column '3' in '1|nonExistant' is not defined.");
|
|
|
|
|
}
|
2010-03-30 03:25:42 +00:00
|
|
|
scope.$set('offset', 3);
|
|
|
|
|
assertEquals(scope.$eval("'abcd'|upper._case"), "ABCD");
|
|
|
|
|
assertEquals(scope.$eval("'abcd'|substring:1:offset"), "bc");
|
|
|
|
|
assertEquals(scope.$eval("'abcd'|substring:1:3|upper._case"), "BC");
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testScopeAccess = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
scope.$set('a', 123);
|
|
|
|
|
scope.$set('b.c', 456);
|
|
|
|
|
assertEquals(scope.$eval("a", scope), 123);
|
|
|
|
|
assertEquals(scope.$eval("b.c", scope), 456);
|
|
|
|
|
assertEquals(scope.$eval("x.y.z", scope), undefined);
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testGrouping = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(scope.$eval("(1+2)*3"), (1+2)*3);
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testAssignments = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(scope.$eval("a=12"), 12);
|
|
|
|
|
assertEquals(scope.$get("a"), 12);
|
2010-01-06 00:36:58 +00:00
|
|
|
|
2010-03-30 03:25:42 +00:00
|
|
|
scope = createScope();
|
|
|
|
|
assertEquals(scope.$eval("x.y.z=123;"), 123);
|
|
|
|
|
assertEquals(scope.$get("x.y.z"), 123);
|
2010-01-06 00:36:58 +00:00
|
|
|
|
2010-03-30 03:25:42 +00:00
|
|
|
assertEquals(234, scope.$eval("a=123; b=234"));
|
|
|
|
|
assertEquals(123, scope.$get("a"));
|
|
|
|
|
assertEquals(234, scope.$get("b"));
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testFunctionCallsNoArgs = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
scope.$set('const', function(a,b){return 123;});
|
|
|
|
|
assertEquals(scope.$eval("const()"), 123);
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testFunctionCalls = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
scope.$set('add', function(a,b){
|
2010-01-06 00:36:58 +00:00
|
|
|
return a+b;
|
|
|
|
|
});
|
2010-03-30 03:25:42 +00:00
|
|
|
assertEquals(3, scope.$eval("add(1,2)"));
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testCalculationBug = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
scope.$set('taxRate', 8);
|
|
|
|
|
scope.$set('subTotal', 100);
|
|
|
|
|
assertEquals(scope.$eval("taxRate / 100 * subTotal"), 8);
|
|
|
|
|
assertEquals(scope.$eval("subTotal * taxRate / 100"), 8);
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testArray = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(scope.$eval("[]").length, 0);
|
|
|
|
|
assertEquals(scope.$eval("[1, 2]").length, 2);
|
|
|
|
|
assertEquals(scope.$eval("[1, 2]")[0], 1);
|
|
|
|
|
assertEquals(scope.$eval("[1, 2]")[1], 2);
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testArrayAccess = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(scope.$eval("[1][0]"), 1);
|
|
|
|
|
assertEquals(scope.$eval("[[1]][0][0]"), 1);
|
|
|
|
|
assertEquals(scope.$eval("[].length"), 0);
|
|
|
|
|
assertEquals(scope.$eval("[1, 2].length"), 2);
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testObject = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(toJson(scope.$eval("{}")), "{}");
|
|
|
|
|
assertEquals(toJson(scope.$eval("{a:'b'}")), '{"a":"b"}');
|
|
|
|
|
assertEquals(toJson(scope.$eval("{'a':'b'}")), '{"a":"b"}');
|
|
|
|
|
assertEquals(toJson(scope.$eval("{\"a\":'b'}")), '{"a":"b"}');
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testObjectAccess = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals("WC", scope.$eval("{false:'WC', true:'CC'}[false]"));
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testJSON = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(toJson(scope.$eval("[{}]")), "[{}]");
|
|
|
|
|
assertEquals(toJson(scope.$eval("[{a:[]}, {b:1}]")), '[{"a":[]},{"b":1}]');
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testMultippleStatements = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(scope.$eval("a=1;b=3;a+b"), 4);
|
|
|
|
|
assertEquals(scope.$eval(";;1;;"), 1);
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testParseThrow = function(){
|
|
|
|
|
expectAsserts(1);
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
scope.$set('e', 'abc');
|
2010-01-06 00:36:58 +00:00
|
|
|
try {
|
2010-03-30 03:25:42 +00:00
|
|
|
scope.$eval("throw e");
|
2010-01-06 00:36:58 +00:00
|
|
|
} catch(e) {
|
|
|
|
|
assertEquals(e, 'abc');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testMethodsGetDispatchedWithCorrectThis = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
2010-01-06 00:36:58 +00:00
|
|
|
var C = function (){
|
|
|
|
|
this.a=123;
|
|
|
|
|
};
|
|
|
|
|
C.prototype.getA = function(){
|
|
|
|
|
return this.a;
|
|
|
|
|
};
|
|
|
|
|
|
2010-03-30 03:25:42 +00:00
|
|
|
scope.$set("obj", new C());
|
|
|
|
|
assertEquals(123, scope.$eval("obj.getA()"));
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
ParserTest.prototype.testMethodsArgumentsGetCorrectThis = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
2010-01-06 00:36:58 +00:00
|
|
|
var C = function (){
|
|
|
|
|
this.a=123;
|
|
|
|
|
};
|
|
|
|
|
C.prototype.sum = function(value){
|
|
|
|
|
return this.a + value;
|
|
|
|
|
};
|
|
|
|
|
C.prototype.getA = function(){
|
|
|
|
|
return this.a;
|
|
|
|
|
};
|
|
|
|
|
|
2010-03-30 03:25:42 +00:00
|
|
|
scope.$set("obj", new C());
|
|
|
|
|
assertEquals(246, scope.$eval("obj.sum(obj.getA())"));
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testObjectPointsToScopeValue = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
scope.$set('a', "abc");
|
|
|
|
|
assertEquals("abc", scope.$eval("{a:a}").a);
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testFieldAccess = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
2010-01-06 00:36:58 +00:00
|
|
|
var fn = function(){
|
|
|
|
|
return {name:'misko'};
|
|
|
|
|
};
|
2010-03-30 03:25:42 +00:00
|
|
|
scope.$set('a', fn);
|
|
|
|
|
assertEquals("misko", scope.$eval("a().name"));
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testArrayIndexBug = function () {
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
scope.$set('items', [{}, {name:'misko'}]);
|
2010-01-06 00:36:58 +00:00
|
|
|
|
2010-03-30 03:25:42 +00:00
|
|
|
assertEquals("misko", scope.$eval('items[1].name'));
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testArrayAssignment = function () {
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
scope.$set('items', []);
|
2010-01-06 00:36:58 +00:00
|
|
|
|
2010-03-30 03:25:42 +00:00
|
|
|
assertEquals("abc", scope.$eval('items[1] = "abc"'));
|
|
|
|
|
assertEquals("abc", scope.$eval('items[1]'));
|
2010-01-06 00:36:58 +00:00
|
|
|
// Dont know how to make this work....
|
2010-03-30 03:25:42 +00:00
|
|
|
// assertEquals("moby", scope.$eval('books[1] = "moby"'));
|
|
|
|
|
// assertEquals("moby", scope.$eval('books[1]'));
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testFiltersCanBeGrouped = function () {
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope({name:'MISKO'});
|
|
|
|
|
assertEquals('misko', scope.$eval('n = (name|lowercase)'));
|
|
|
|
|
assertEquals('misko', scope.$eval('n'));
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testFiltersCanBeGrouped = function () {
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope({name:'MISKO'});
|
|
|
|
|
assertEquals('misko', scope.$eval('n = (name|lowercase)'));
|
|
|
|
|
assertEquals('misko', scope.$eval('n'));
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testRemainder = function () {
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(1, scope.$eval('1%2'));
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testSumOfUndefinedIsNotUndefined = function () {
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(1, scope.$eval('1+undefined'));
|
|
|
|
|
assertEquals(1, scope.$eval('undefined+1'));
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testMissingThrowsError = function() {
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
2010-01-06 00:36:58 +00:00
|
|
|
try {
|
2010-03-30 03:25:42 +00:00
|
|
|
scope.$eval('[].count(');
|
2010-01-06 00:36:58 +00:00
|
|
|
fail();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
assertEquals('Unexpected end of expression: [].count(', e);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testItShouldCreateClosureFunctionWithNoArguments = function () {
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
var fn = scope.$eval("{:value}");
|
|
|
|
|
scope.$set("value", 1);
|
2010-01-06 00:36:58 +00:00
|
|
|
assertEquals(1, fn());
|
2010-03-30 03:25:42 +00:00
|
|
|
scope.$set("value", 2);
|
2010-01-06 00:36:58 +00:00
|
|
|
assertEquals(2, fn());
|
2010-03-30 03:25:42 +00:00
|
|
|
fn = scope.$eval("{():value}");
|
2010-01-06 00:36:58 +00:00
|
|
|
assertEquals(2, fn());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testItShouldCreateClosureFunctionWithArguments = function () {
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
scope.$set("value", 1);
|
|
|
|
|
var fn = scope.$eval("{(a):value+a}");
|
2010-01-06 00:36:58 +00:00
|
|
|
assertEquals(11, fn(10));
|
2010-03-30 03:25:42 +00:00
|
|
|
scope.$set("value", 2);
|
2010-01-06 00:36:58 +00:00
|
|
|
assertEquals(12, fn(10));
|
2010-03-30 03:25:42 +00:00
|
|
|
fn = scope.$eval("{(a,b):value+a+b}");
|
2010-01-06 00:36:58 +00:00
|
|
|
assertEquals(112, fn(10, 100));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testItShouldHaveDefaultArugument = function(){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
var fn = scope.$eval("{:$*2}");
|
2010-01-06 00:36:58 +00:00
|
|
|
assertEquals(4, fn(2));
|
|
|
|
|
};
|
|
|
|
|
|
2010-01-20 01:53:20 +00:00
|
|
|
ParserTest.prototype.testDoubleNegationBug = function (){
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(true, scope.$eval('true'));
|
|
|
|
|
assertEquals(false, scope.$eval('!true'));
|
|
|
|
|
assertEquals(true, scope.$eval('!!true'));
|
|
|
|
|
assertEquals('a', scope.$eval('{true:"a", false:"b"}[!!true]'));
|
2010-01-20 01:53:20 +00:00
|
|
|
};
|
|
|
|
|
|
2010-02-12 22:16:33 +00:00
|
|
|
ParserTest.prototype.testNegationBug = function () {
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(!false || true, scope.$eval("!false || true"));
|
|
|
|
|
assertEquals(!11 == 10, scope.$eval("!11 == 10"));
|
|
|
|
|
assertEquals(12/6/2, scope.$eval("12/6/2"));
|
2010-02-12 22:16:33 +00:00
|
|
|
};
|
|
|
|
|
|
2010-03-24 17:35:01 +00:00
|
|
|
ParserTest.prototype.testBugStringConfusesParser = function() {
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals('!', scope.$eval('suffix = "!"'));
|
2010-03-24 17:35:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParserTest.prototype.testParsingBug = function () {
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals({a: "-"}, scope.$eval("{a:'-'}"));
|
2010-03-24 17:35:01 +00:00
|
|
|
};
|
2010-03-25 20:01:08 +00:00
|
|
|
|
|
|
|
|
ParserTest.prototype.testUndefined = function () {
|
2010-03-30 03:25:42 +00:00
|
|
|
var scope = createScope();
|
|
|
|
|
assertEquals(undefined, scope.$eval("undefined"));
|
|
|
|
|
assertEquals(undefined, scope.$eval("a=undefined"));
|
|
|
|
|
assertEquals(undefined, scope.$get("a"));
|
2010-03-25 20:01:08 +00:00
|
|
|
};
|