|
@ -41,6 +41,7 @@ assert.AssertionError = function AssertionError(options) { |
|
|
this.actual = options.actual; |
|
|
this.actual = options.actual; |
|
|
this.expected = options.expected; |
|
|
this.expected = options.expected; |
|
|
this.operator = options.operator; |
|
|
this.operator = options.operator; |
|
|
|
|
|
|
|
|
//v8 specific
|
|
|
//v8 specific
|
|
|
if (Error.captureStackTrace) { |
|
|
if (Error.captureStackTrace) { |
|
|
Error.captureStackTrace(this, fail); |
|
|
Error.captureStackTrace(this, fail); |
|
@ -58,10 +59,15 @@ assert.AssertionError = function AssertionError(options) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
assert.AssertionError.prototype.toString = function() { |
|
|
assert.AssertionError.prototype.toString = function() { |
|
|
if(this.message) |
|
|
if (this.message) { |
|
|
return [this.name+":", this.message].join(' '); |
|
|
return [this.name+":", this.message].join(' '); |
|
|
else |
|
|
} else { |
|
|
return [this.name+":", JSON.stringify(this.expected ), this.operator , JSON.stringify(this.actual)].join(" "); |
|
|
return [ this.name+":" |
|
|
|
|
|
, JSON.stringify(this.expected ) |
|
|
|
|
|
, this.operator |
|
|
|
|
|
, JSON.stringify(this.actual) |
|
|
|
|
|
].join(" "); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// assert.AssertionError instanceof Error
|
|
|
// assert.AssertionError instanceof Error
|
|
@ -99,8 +105,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) |
|
|
if (!!!value) fail(value, true, message, "=="); |
|
|
fail(value, true, message, "=="); |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 5. The equality assertion tests shallow, coercive equality with
|
|
|
// 5. The equality assertion tests shallow, coercive equality with
|
|
@ -108,25 +113,23 @@ 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) |
|
|
if (actual != expected) fail(actual, expected, message, "=="); |
|
|
fail(actual, expected, message, "=="); |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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) |
|
|
if (actual == expected) fail(actual, expected, message, "!="); |
|
|
fail(actual, expected, message, "!="); |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 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) { |
|
|
exports.deepEqual = function deepEqual(actual, expected, message) { |
|
|
if (!_deepEqual(actual, expected)) |
|
|
if (!_deepEqual(actual, expected)) { |
|
|
fail(actual, expected, message, "deepEqual"); |
|
|
fail(actual, expected, message, "deepEqual"); |
|
|
|
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
function _deepEqual(actual, expected) { |
|
|
function _deepEqual(actual, expected) { |
|
@ -136,14 +139,12 @@ function _deepEqual(actual, expected) { |
|
|
|
|
|
|
|
|
// 7.2. If the expected value is a Date object, the actual value is
|
|
|
// 7.2. If the expected value is a Date object, the actual value is
|
|
|
// equivalent if it is also a Date object that refers to the same time.
|
|
|
// equivalent if it is also a Date object that refers to the same time.
|
|
|
} else if (actual instanceof Date |
|
|
} else if (actual instanceof Date && expected instanceof Date) { |
|
|
&& expected instanceof Date) { |
|
|
|
|
|
return actual.getTime() === expected.getTime(); |
|
|
return actual.getTime() === expected.getTime(); |
|
|
|
|
|
|
|
|
// 7.3. Other pairs that do not both pass typeof value == "object",
|
|
|
// 7.3. Other pairs that do not both pass typeof value == "object",
|
|
|
// equivalence is determined by ==.
|
|
|
// equivalence is determined by ==.
|
|
|
} else if (typeof actual != 'object' |
|
|
} else if (typeof actual != 'object' && typeof expected != 'object') { |
|
|
&& typeof expected != 'object') { |
|
|
|
|
|
return actual == expected; |
|
|
return actual == expected; |
|
|
|
|
|
|
|
|
// 7.4. For all other Object pairs, including Array objects, equivalence is
|
|
|
// 7.4. For all other Object pairs, including Array objects, equivalence is
|
|
@ -212,44 +213,30 @@ function objEquiv (a, b) { |
|
|
// assert.notDeepEqual(actual, expected, message_opt);
|
|
|
// assert.notDeepEqual(actual, expected, message_opt);
|
|
|
|
|
|
|
|
|
exports.notDeepEqual = function notDeepEqual(actual, expected, message) { |
|
|
exports.notDeepEqual = function notDeepEqual(actual, expected, message) { |
|
|
if (_deepEqual(actual, expected)) |
|
|
if (_deepEqual(actual, expected)) { |
|
|
fail(actual, expected, message, "notDeepEqual"); |
|
|
fail(actual, expected, message, "notDeepEqual"); |
|
|
|
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 9. The strict equality assertion tests strict equality, as determined by ===.
|
|
|
// 9. The strict equality assertion tests strict equality, as determined by ===.
|
|
|
// 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) |
|
|
if (actual !== expected) fail(actual, expected, message, "==="); |
|
|
fail(actual, expected, message, "==="); |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 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) |
|
|
if (actual === expected) fail(actual, expected, message, "!=="); |
|
|
fail(actual, expected, message, "!=="); |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 11. Expected to throw an error:
|
|
|
function _throws (shouldThrow, block, err, message) { |
|
|
// assert.throws(block, Error_opt, message_opt);
|
|
|
var exception = null, |
|
|
|
|
|
threw = false, |
|
|
assert.throws = function(block, /*optional*/error, /*optional*/message){ |
|
|
|
|
|
var args = [true] |
|
|
|
|
|
_throws.apply(this, args.concat(pSlice.call(arguments))); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// EXTENSION! This is annoying to write outside this module.
|
|
|
|
|
|
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message){ |
|
|
|
|
|
var args = [false] |
|
|
|
|
|
_throws.apply(this, args.concat(pSlice.call(arguments))); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var _throws = function (shouldThrow, block, err, message) { |
|
|
|
|
|
var exception = null |
|
|
|
|
|
threw = false |
|
|
|
|
|
typematters = true; |
|
|
typematters = true; |
|
|
|
|
|
|
|
|
message = message || ""; |
|
|
message = message || ""; |
|
|
|
|
|
|
|
|
//handle optional arguments
|
|
|
//handle optional arguments
|
|
@ -270,17 +257,31 @@ var _throws = function (shouldThrow, block, err, message) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (shouldThrow && !threw) { |
|
|
if (shouldThrow && !threw) { |
|
|
fail("Missing expected exception"+ |
|
|
fail( "Missing expected exception" |
|
|
(err && err.name ? " ("+err.name+")." : '.') + |
|
|
+ (err && err.name ? " ("+err.name+")." : '.') |
|
|
(message ? " " + message : "")); |
|
|
+ (message ? " " + message : "") |
|
|
|
|
|
); |
|
|
} |
|
|
} |
|
|
if (!shouldThrow && threw && typematters && exception instanceof err) { |
|
|
if (!shouldThrow && threw && typematters && exception instanceof err) { |
|
|
fail("Got unwanted exception" + |
|
|
fail( "Got unwanted exception" |
|
|
(err && err.name ? " ("+err.name+")." : '.') + |
|
|
+ (err && err.name ? " ("+err.name+")." : '.') |
|
|
(message ? " " + message : "")); |
|
|
+ (message ? " " + message : "") |
|
|
|
|
|
); |
|
|
} |
|
|
} |
|
|
if ((shouldThrow && threw && typematters && !(exception instanceof err)) || |
|
|
if ((shouldThrow && threw && typematters && !(exception instanceof err)) || |
|
|
(!shouldThrow && threw)) { |
|
|
(!shouldThrow && threw)) { |
|
|
throw exception; |
|
|
throw exception; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 11. Expected to throw an error:
|
|
|
|
|
|
// assert.throws(block, Error_opt, message_opt);
|
|
|
|
|
|
|
|
|
|
|
|
assert.throws = function(block, /*optional*/error, /*optional*/message) { |
|
|
|
|
|
_throws.apply(this, [true].concat(pSlice.call(arguments))); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// EXTENSION! This is annoying to write outside this module.
|
|
|
|
|
|
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) { |
|
|
|
|
|
_throws.apply(this, [false].concat(pSlice.call(arguments))); |
|
|
}; |
|
|
}; |
|
|