aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMisko Hevery2010-11-16 13:57:41 -0800
committerIgor Minar2010-11-16 14:19:55 -0800
commitb2d63ac48bdc61b5a4afdd10b8485c0c1ab8cdca (patch)
tree0b798ba07d5b593dcc3e27964e81eb0542440d56 /test
parent4af32de84a264e05eebfa6dbc09ce10fac1e1417 (diff)
downloadangular.js-b2d63ac48bdc61b5a4afdd10b8485c0c1ab8cdca.tar.bz2
Changed error handling so that better stack traces are displayed in the ng-errors
Diffstat (limited to 'test')
-rw-r--r--test/BinderTest.js13
-rw-r--r--test/JsonSpec.js23
-rw-r--r--test/ParserSpec.js16
-rw-r--r--test/ScopeSpec.js16
-rw-r--r--test/directivesSpec.js2
-rw-r--r--test/servicesSpec.js36
6 files changed, 70 insertions, 36 deletions
diff --git a/test/BinderTest.js b/test/BinderTest.js
index 71b2f6b6..d35d46f4 100644
--- a/test/BinderTest.js
+++ b/test/BinderTest.js
@@ -276,15 +276,15 @@ BinderTest.prototype.testIfTextBindingThrowsErrorDecorateTheSpan = function(){
a.scope.$eval();
var span = childNode(doc, 0);
assertTrue(span.hasClass('ng-exception'));
- assertEquals('ErrorMsg1', fromJson(span.text()));
- assertEquals('"ErrorMsg1"', span.attr('ng-exception'));
+ assertTrue(!!span.text().match(/ErrorMsg1/));
+ assertTrue(!!span.attr('ng-exception').match(/ErrorMsg1/));
a.scope.$set('error.throw', function(){throw "MyError";});
a.scope.$eval();
span = childNode(doc, 0);
assertTrue(span.hasClass('ng-exception'));
assertTrue(span.text(), span.text().match('MyError') !== null);
- assertEquals('"MyError"', span.attr('ng-exception'));
+ assertEquals('MyError', span.attr('ng-exception'));
a.scope.$set('error.throw', function(){return "ok";});
a.scope.$eval();
@@ -438,13 +438,12 @@ BinderTest.prototype.testActionOnAHrefThrowsError = function(){
var model = {books:[]};
var c = this.compile('<a ng:click="action()">Add Phone</a>', model);
c.scope.action = function(){
- throw {a:'abc', b:2};
+ throw new Error('MyError');
};
var input = c.node;
browserTrigger(input, 'click');
- var error = fromJson(input.attr('ng-exception'));
- assertEquals("abc", error.a);
- assertEquals(2, error.b);
+ var error = input.attr('ng-exception');
+ assertTrue(!!error.match(/MyError/));
assertTrue("should have an error class", input.hasClass('ng-exception'));
// TODO: I think that exception should never get cleared so this portion of test makes no sense
diff --git a/test/JsonSpec.js b/test/JsonSpec.js
index 0acbd79c..f0019bef 100644
--- a/test/JsonSpec.js
+++ b/test/JsonSpec.js
@@ -74,7 +74,7 @@ describe('json', function(){
});
it('should serialize $ properties', function() {
- var obj = {$a: 'a'}
+ var obj = {$a: 'a'};
expect(angular.toJson(obj)).toEqual('{"$a":"a"}');
});
@@ -118,31 +118,38 @@ describe('json', function(){
describe('security', function(){
it('should not allow naked expressions', function(){
- expect(function(){fromJson('1+2');}).toThrow("Did not understand '+2' while evaluating '1+2'.");
+ expect(function(){fromJson('1+2');}).
+ toThrow(new Error("Parse Error: Token '+' is extra token not part of expression at column 2 of expression [1+2] starting at [+2]."));
});
it('should not allow naked expressions group', function(){
- expect(function(){fromJson('(1+2)');}).toThrow("Expression at column='0' of expression '(1+2)' starting at '(1+2)' is not valid json.");
+ expect(function(){fromJson('(1+2)');}).
+ toThrow(new Error("Parse Error: Token '(' is not valid json at column 1 of expression [(1+2)] starting at [(1+2)]."));
});
it('should not allow expressions in objects', function(){
- expect(function(){fromJson('{a:abc()}');}).toThrow("Expression at column='3' of expression '{a:abc()}' starting at 'abc()}' is not valid json.");
+ expect(function(){fromJson('{a:abc()}');}).
+ toThrow(new Error("Parse Error: Token 'abc' is not valid json at column 4 of expression [{a:abc()}] starting at [abc()}]."));
});
it('should not allow expressions in arrays', function(){
- expect(function(){fromJson('[1+2]');}).toThrow("Expression at column='2' of expression '[1+2]' starting at '+2]' is not valid json.");
+ expect(function(){fromJson('[1+2]');}).
+ toThrow(new Error("Parse Error: Token '+' is not valid json at column 3 of expression [[1+2]] starting at [+2]]."));
});
it('should not allow vars', function(){
- expect(function(){fromJson('[1, x]');}).toThrow("Expression at column='4' of expression '[1, x]' starting at 'x]' is not valid json.");
+ expect(function(){fromJson('[1, x]');}).
+ toThrow(new Error("Parse Error: Token 'x' is not valid json at column 5 of expression [[1, x]] starting at [x]]."));
});
it('should not allow dereference', function(){
- expect(function(){fromJson('["".constructor]');}).toThrow("Expression at column='3' of expression '[\"\".constructor]' starting at '.constructor]' is not valid json.");
+ expect(function(){fromJson('["".constructor]');}).
+ toThrow(new Error("Parse Error: Token '.' is not valid json at column 4 of expression [[\"\".constructor]] starting at [.constructor]]."));
});
it('should not allow expressions ofter valid json', function(){
- expect(function(){fromJson('[].constructor');}).toThrow("Expression at column='2' of expression '[].constructor' starting at '.constructor' is not valid json.");
+ expect(function(){fromJson('[].constructor');}).
+ toThrow(new Error("Parse Error: Token '.' is not valid json at column 3 of expression [[].constructor] starting at [.constructor]."));
});
});
diff --git a/test/ParserSpec.js b/test/ParserSpec.js
index f772e4a4..06cec2b3 100644
--- a/test/ParserSpec.js
+++ b/test/ParserSpec.js
@@ -158,11 +158,11 @@ describe('parser', function() {
it('should throws exception for invalid exponent', function() {
expect(function() {
lex("0.5E-");
- }).toThrow('Lexer found invalid exponential value "0.5E-"');
+ }).toThrow(new Error('Lexer Error: Invalid exponent at column 4 in expression [0.5E-].'));
expect(function() {
lex("0.5E-A");
- }).toThrow('Lexer found invalid exponential value "0.5E-A"');
+ }).toThrow(new Error('Lexer Error: Invalid exponent at column 4 in expression [0.5E-A].'));
});
it('should tokenize number starting with a dot', function() {
@@ -173,7 +173,7 @@ describe('parser', function() {
it('should throw error on invalid unicode', function() {
expect(function() {
lex("'\\u1''bla'");
- }).toThrow("Lexer Error: Invalid unicode escape [\\u1''b] starting at column '0' in expression ''\\u1''bla''.");
+ }).toThrow(new Error("Lexer Error: Invalid unicode escape [\\u1''b] at column 2 in expression ['\\u1''bla']."));
});
});
@@ -225,7 +225,7 @@ describe('parser', function() {
expect(function() {
scope.$eval("1|nonExistant");
- }).toThrow("Function 'nonExistant' at column '3' in '1|nonExistant' is not defined.");
+ }).toThrow(new Error("Parse Error: Token 'nonExistant' should be a function at column 3 of expression [1|nonExistant] starting at [nonExistant]."));
scope.$set('offset', 3);
expect(scope.$eval("'abcd'|upper._case")).toEqual("ABCD");
@@ -312,14 +312,6 @@ describe('parser', function() {
expect(scope.$eval(";;1;;")).toEqual(1);
});
- it('should evaluate throw', function() {
- scope.$set('e', 'abc');
-
- expect(function() {
- scope.$eval("throw e");
- }).toThrow('abc');
- });
-
it('should evaluate object methods in correct context (this)', function() {
var C = function () {
this.a = 123;
diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js
index 8b4ada5a..38350b17 100644
--- a/test/ScopeSpec.js
+++ b/test/ScopeSpec.js
@@ -106,7 +106,7 @@ describe('scope/model', function(){
model.$watch('name', function(newVal, oldVal){
count ++;
nameNewVal = newVal;
- nameOldVal = oldVal
+ nameOldVal = oldVal;
});
expect(count).toBe(1);
@@ -123,12 +123,12 @@ describe('scope/model', function(){
model.$watch('name', function(newVal, oldVal){
count ++;
nameNewVal = newVal;
- nameOldVal = oldVal
+ nameOldVal = oldVal;
}, undefined, false);
expect(count).toBe(0);
expect(nameNewVal).toBe('crazy val 1');
- expect(nameOldVal).toBe('crazy val 2')
+ expect(nameOldVal).toBe('crazy val 2');
});
});
@@ -143,17 +143,17 @@ describe('scope/model', function(){
describe('$tryEval', function(){
it('should report error on element', function(){
var scope = createScope();
- scope.$tryEval('throw "myerror";', function(error){
+ scope.$tryEval(function(){throw "myError";}, function(error){
scope.error = error;
});
- expect(scope.error).toEqual('myerror');
+ expect(scope.error).toEqual('myError');
});
it('should report error on visible element', function(){
var element = jqLite('<div></div>');
var scope = createScope();
- scope.$tryEval('throw "myError"', element);
- expect(element.attr('ng-exception')).toEqual('"myError"'); // errors are jsonified
+ scope.$tryEval(function(){throw "myError";}, element);
+ expect(element.attr('ng-exception')).toEqual('myError');
expect(element.hasClass('ng-exception')).toBeTruthy();
});
@@ -163,7 +163,7 @@ describe('scope/model', function(){
scope.$exceptionHandler = function(e){
this.error = e;
};
- scope.$tryEval('throw "myError"');
+ scope.$tryEval(function(){throw "myError";});
expect(scope.error).toEqual("myError");
});
});
diff --git a/test/directivesSpec.js b/test/directivesSpec.js
index d575c062..4b949fcb 100644
--- a/test/directivesSpec.js
+++ b/test/directivesSpec.js
@@ -168,7 +168,7 @@ describe("directives", function(){
var log = "";
log += element.attr('ng-exception') + ';';
log += element.hasClass('ng-exception') + ';';
- expect(log).toEqual("\"Expected ng:repeat in form of 'item in collection' but got 'i dont parse'.\";true;");
+ expect(log.match(/Expected ng:repeat in form of 'item in collection' but got 'i dont parse'./)).toBeTruthy();
});
it('should expose iterator offset as $index when iterating over arrays', function() {
diff --git a/test/servicesSpec.js b/test/servicesSpec.js
index f4d9aabe..13e61b18 100644
--- a/test/servicesSpec.js
+++ b/test/servicesSpec.js
@@ -70,6 +70,42 @@ describe("service", function(){
scope.$log.info();
scope.$log.error();
});
+
+ describe('Error', function(){
+ var e, $log, $console, errorArgs;
+ beforeEach(function(){
+ e = new Error('');
+ e.message = undefined;
+ e.sourceURL = undefined;
+ e.line = undefined;
+ e.stack = undefined;
+
+ $console = angular.service('$log')({console:{error:function(){
+ errorArgs = arguments;
+ }}});
+ });
+
+ it('should pass error if does not have trace', function(){
+ $console.error('abc', e);
+ expect(errorArgs).toEqual(['abc', e]);
+ });
+
+ it('should print stack', function(){
+ e.stack = 'stack';
+ $console.error('abc', e);
+ expect(errorArgs).toEqual(['abc', 'stack']);
+ });
+
+ it('should print line', function(){
+ e.message = 'message';
+ e.sourceURL = 'sourceURL';
+ e.line = '123';
+ $console.error('abc', e);
+ expect(errorArgs).toEqual(['abc', 'message\nsourceURL:123']);
+ });
+
+ });
+
});
describe("$exceptionHandler", function(){