Browse Source

Stack trace for asserts shouldn't include assert module

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
0accebe583
  1. 46
      lib/assert.js

46
lib/assert.js

@ -35,26 +35,16 @@ var assert = exports;
// 2. The AssertionError is defined in assert. // 2. The AssertionError is defined in assert.
// new assert.AssertionError({message: message, actual: actual, expected: expected}) // new assert.AssertionError({message: message, actual: actual, expected: expected})
assert.AssertionError = function AssertionError(options) { assert.AssertionError = function AssertionError (options) {
this.name = "AssertionError"; this.name = "AssertionError";
this.message = options.message; this.message = options.message;
this.actual = options.actual; this.actual = options.actual;
this.expected = options.expected; this.expected = options.expected;
this.operator = options.operator; this.operator = options.operator;
var stackStartFunction = options.stackStartFunction || fail;
//v8 specific
if (Error.captureStackTrace) { if (Error.captureStackTrace) {
Error.captureStackTrace(this, fail); Error.captureStackTrace(this, stackStartFunction);
//node specific, removes the node machinery stack frames
if (typeof(__filename) !== undefined) {
var stack = this.stack.split('\n');
for (var i = stack.length - 1; i >= 0; i--) {
if (stack[i].indexOf(__filename) != -1) {
this.stack = stack.slice(0, i+2).join('\n');
break;
}
}
}
} }
}; };
@ -85,12 +75,13 @@ assert.AssertionError.__proto__ = Error.prototype;
// both the actual and expected values to the assertion error for // both the actual and expected values to the assertion error for
// display purposes. // display purposes.
function fail(actual, expected, message, operator) { function fail(actual, expected, message, operator, stackStartFunction) {
throw new assert.AssertionError({ throw new assert.AssertionError({
message: message, message: message,
actual: actual, actual: actual,
expected: expected, expected: expected,
operator: operator operator: operator,
stackStartFunction: stackStartFunction
}); });
} }
@ -105,7 +96,7 @@ assert.fail = fail;
// assert.strictEqual(true, guard, message_opt);. // assert.strictEqual(true, guard, message_opt);.
assert.ok = function ok(value, message) { assert.ok = function ok(value, message) {
if (!!!value) fail(value, true, message, "=="); if (!!!value) fail(value, true, message, "==", assert.ok);
}; };
// 5. The equality assertion tests shallow, coercive equality with // 5. The equality assertion tests shallow, coercive equality with
@ -113,22 +104,24 @@ assert.ok = function ok(value, message) {
// assert.equal(actual, expected, message_opt); // assert.equal(actual, expected, message_opt);
assert.equal = function equal(actual, expected, message) { assert.equal = function equal(actual, expected, message) {
if (actual != expected) fail(actual, expected, message, "=="); if (actual != expected) fail(actual, expected, message, "==", assert.equal);
}; };
// 6. The non-equality assertion tests for whether two objects are not equal // 6. The non-equality assertion tests for whether two objects are not equal
// with != assert.notEqual(actual, expected, message_opt); // with != assert.notEqual(actual, expected, message_opt);
assert.notEqual = function notEqual(actual, expected, message) { assert.notEqual = function notEqual(actual, expected, message) {
if (actual == expected) fail(actual, expected, message, "!="); if (actual == expected) {
fail(actual, expected, message, "!=", assert.notEqual);
}
}; };
// 7. The equivalence assertion tests a deep equality relation. // 7. The equivalence assertion tests a deep equality relation.
// assert.deepEqual(actual, expected, message_opt); // assert.deepEqual(actual, expected, message_opt);
exports.deepEqual = function deepEqual(actual, expected, message) { assert.deepEqual = function deepEqual(actual, expected, message) {
if (!_deepEqual(actual, expected)) { if (!_deepEqual(actual, expected)) {
fail(actual, expected, message, "deepEqual"); fail(actual, expected, message, "deepEqual", assert.deepEqual);
} }
}; };
@ -212,9 +205,9 @@ function objEquiv (a, b) {
// 8. The non-equivalence assertion tests for any deep inequality. // 8. The non-equivalence assertion tests for any deep inequality.
// assert.notDeepEqual(actual, expected, message_opt); // assert.notDeepEqual(actual, expected, message_opt);
exports.notDeepEqual = function notDeepEqual(actual, expected, message) { assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
if (_deepEqual(actual, expected)) { if (_deepEqual(actual, expected)) {
fail(actual, expected, message, "notDeepEqual"); fail(actual, expected, message, "notDeepEqual", assert.notDeepEqual);
} }
}; };
@ -222,14 +215,18 @@ exports.notDeepEqual = function notDeepEqual(actual, expected, message) {
// assert.strictEqual(actual, expected, message_opt); // assert.strictEqual(actual, expected, message_opt);
assert.strictEqual = function strictEqual(actual, expected, message) { assert.strictEqual = function strictEqual(actual, expected, message) {
if (actual !== expected) fail(actual, expected, message, "==="); if (actual !== expected) {
fail(actual, expected, message, "===", assert.strictEqual);
}
}; };
// 10. The strict non-equality assertion tests for strict inequality, as determined by !==. // 10. The strict non-equality assertion tests for strict inequality, as determined by !==.
// assert.notStrictEqual(actual, expected, message_opt); // assert.notStrictEqual(actual, expected, message_opt);
assert.notStrictEqual = function notStrictEqual(actual, expected, message) { assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
if (actual === expected) fail(actual, expected, message, "!=="); if (actual === expected) {
fail(actual, expected, message, "!==", assert.notStrictEqual);
}
}; };
function _throws (shouldThrow, block, err, message) { function _throws (shouldThrow, block, err, message) {
@ -285,3 +282,4 @@ assert.throws = function(block, /*optional*/error, /*optional*/message) {
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) { assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
_throws.apply(this, [false].concat(pSlice.call(arguments))); _throws.apply(this, [false].concat(pSlice.call(arguments)));
}; };

Loading…
Cancel
Save