Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds better stack trace

  • Loading branch information...
commit 3cd3ff3f0ef503c60c53b15bfa6be15922ed5224 1 parent cab58a9
@elcuervo authored
Showing with 60 additions and 17 deletions.
  1. +52 −16 lib/gerbil.js
  2. +8 −1 test/test.js
View
68 lib/gerbil.js
@@ -29,12 +29,20 @@ var Gerbil = function Gerbil(description, tests) {
this.ok = function(test) {
this.success++;
- test.scenario.config.formatter.ok(Gerbil.format("{0} ({1} assertions)", [test.name, test.assertions]));
+ var message = Gerbil.format("{0} ({1} assertions)", [
+ test.name, test.assertions
+ ]);
+
+ test.scenario.config.formatter.ok(message);
};
this.fail = function(test) {
this.failures++;
- test.scenario.config.formatter.fail(Gerbil.format("{0} - assertion number {1} failed - {2}", [test.name, test.assertions + 1, test.message]));
+ var message = Gerbil.format("{0} - assertion number {1} failed - {2}", [
+ test.name, test.assertions + 1, test.message
+ ]);
+
+ test.scenario.config.formatter.fail(message);
};
this.postergate = function(test) {
@@ -95,7 +103,7 @@ var Gerbil = function Gerbil(description, tests) {
} while(test);
this.cleanup.call(scope);
} catch(exception) {
- throw Gerbil.Error(exception);
+ throw Gerbil.Error({ message: exception });
} finally {
setTimeout(this.check, this.timeout, this);
}
@@ -148,10 +156,14 @@ Gerbil.format = function(s, args) {
return s.replace(re, function(_, match){ return args[match]; });
};
-Gerbil.Error = function GerbilError(message) {
- if(arguments.length === 2) message = Gerbil.format(arguments[0], arguments[1]);
- var error = new Error(message);
- return error.stack || error.message;
+Gerbil.Error = function GerbilError(options) {
+ if(options.args) {
+ options.message = Gerbil.format(options.message, options.args);
+ }
+ var error = new Error(options.message);
+ if(options.stack) error.stack = options.stack;
+
+ return error;
};
Gerbil.Queue = function GerbilQueue() {
@@ -191,7 +203,7 @@ Gerbil.Test = function GerbilTest(name, test, scenario) {
this.fails = function(exception) {
this.failed = true;
- this.message = exception;
+ this.message = exception.stack;
};
this.measure = function() {
@@ -234,7 +246,7 @@ Gerbil.Test.prototype = {
assert: function(expectation) {
if(!expectation) {
- throw Gerbil.Error("Assertion Failed");
+ throw Gerbil.Error({ message: "Assertion Failed" });
} else {
this.assertions++;
}
@@ -247,29 +259,53 @@ Gerbil.Test.prototype = {
fn();
errorMessage = expectedError.name + " was expected but not raised.";
} catch(exception) {
- if (typeof exception == typeof expectedError)
+ if (typeof exception == typeof expectedError) {
errorMessage = expectedError.name + " was expected but " + exception.name + " was raised.";
+ }
}
- if (errorMessage) throw Gerbil.Error(errorMessage);
+ if (errorMessage) throw Gerbil.Error({message: errorMessage});
},
assertEqual: function(first, second) {
- if(first == undefined || second == undefined) throw Gerbil.Error("attr2 = {0} ({1}) and attr2 = {2} ({3})", [first, typeof first, second, typeof second]);
- if(typeof(first) != typeof(second)) throw Gerbil.Error("Different type {0} vs {1}", [typeof first, typeof second]);
+ if(first == undefined || second == undefined) {
+ throw Gerbil.Error({
+ message: "attr2 = {0} ({1}) and attr2 = {2} ({3})",
+ args: [first, typeof first, second, typeof second]
+ });
+ }
+
+ if(typeof first != typeof second) {
+ throw Gerbil.Error({
+ message: "Different type {0} vs {1}",
+ args: [typeof first, typeof second]
+ });
+ }
this.assertions++;
var errorMessage = "Not equal {0} != {1}";
switch(first.constructor) {
case Array:
- if (first.length != second.length) throw Gerbil.Error("Different Lengths");
+ if (first.length != second.length) {
+ throw Gerbil.Error({ message:"Different Lengths" });
+ }
for (var i = 0; i < first.length; i++) {
- if (first[i] != second[i]) throw Gerbil.Error(errorMessage, [first[i], second[i]]);
+ if (first[i] != second[i]) {
+ throw Gerbil.Error({
+ message: errorMessage,
+ args: [first[i], second[i]]
+ });
+ }
}
break;
case String:
case Number:
- if (first != second) throw Gerbil.Error(errorMessage, [first, second]);
+ if (first != second) {
+ throw Gerbil.Error({
+ message: errorMessage,
+ args: [first, second]
+ });
+ }
break;
default:
break;
View
9 test/test.js
@@ -13,7 +13,7 @@ scenario("Validate some stuff", {
},
"this is another pending test": function(g) {
- return g.pending()
+ return g.pending();
},
"test": function(g) {
@@ -34,5 +34,12 @@ scenario("Validate some stuff", {
g.async(function() {
g.assert(false);
});
+ },
+
+ "show improved stack trace": function(g) {
+ var f = function() {
+ throw Error();
+ };
+ g.assert(f());
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.