Skip to content

Commit

Permalink
Adds better stack trace
Browse files Browse the repository at this point in the history
  • Loading branch information
elcuervo committed May 10, 2012
1 parent cab58a9 commit 3cd3ff3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
68 changes: 52 additions & 16 deletions lib/gerbil.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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++;
}
Expand All @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion test/test.js
Expand Up @@ -13,7 +13,7 @@ scenario("Validate some stuff", {
},

"this is another pending test": function(g) {
return g.pending()
return g.pending();
},

"test": function(g) {
Expand All @@ -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());
}
});

0 comments on commit 3cd3ff3

Please sign in to comment.