node/lib/assert.js

327 lines
10 KiB
JavaScript
Raw Normal View History

// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
//
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
//
// Originally from narwhal.js (http://narwhaljs.org)
// Copyright (c) 2009 Thomas Robinson <280north.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the 'Software'), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
2010-12-02 00:36:23 +00:00
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
// UTILITY
var util = require('util');
var pSlice = Array.prototype.slice;
// 1. The assert module provides functions that throw
// AssertionError's when particular conditions are not met. The
// assert module must conform to the following interface.
var assert = module.exports = ok;
// 2. The AssertionError is defined in assert.
2010-12-02 00:36:23 +00:00
// new assert.AssertionError({ message: message,
// actual: actual,
// expected: expected })
2010-12-02 00:36:23 +00:00
assert.AssertionError = function AssertionError(options) {
this.name = 'AssertionError';
2009-12-29 18:37:40 +00:00
this.actual = options.actual;
this.expected = options.expected;
this.operator = options.operator;
if (options.message) {
this.message = options.message;
this.generatedMessage = false;
} else {
this.message = getMessage(this);
this.generatedMessage = true;
}
var stackStartFunction = options.stackStartFunction || fail;
2013-04-03 16:43:17 +00:00
Error.captureStackTrace(this, stackStartFunction);
};
// assert.AssertionError instanceof Error
util.inherits(assert.AssertionError, Error);
function replacer(key, value) {
if (util.isUndefined(value)) {
return '' + value;
}
if (util.isNumber(value) && !isFinite(value)) {
return value.toString();
}
if (util.isFunction(value) || util.isRegExp(value)) {
return value.toString();
}
return value;
}
function truncate(s, n) {
if (util.isString(s)) {
return s.length < n ? s : s.slice(0, n);
} else {
return s;
}
}
function getMessage(self) {
return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
self.operator + ' ' +
truncate(JSON.stringify(self.expected, replacer), 128);
2013-04-03 16:43:17 +00:00
}
// At present only the three keys mentioned above are used and
// understood by the spec. Implementations or sub modules can pass
// other keys to the AssertionError's constructor - they will be
// ignored.
// 3. All of the following functions must throw an AssertionError
// when a corresponding condition is not met, with a message that
// may be undefined if not provided. All assertion methods provide
// both the actual and expected values to the assertion error for
// display purposes.
function fail(actual, expected, message, operator, stackStartFunction) {
2009-12-29 18:37:40 +00:00
throw new assert.AssertionError({
message: message,
actual: actual,
expected: expected,
operator: operator,
stackStartFunction: stackStartFunction
2009-12-29 18:37:40 +00:00
});
}
// EXTENSION! allows for well behaved errors defined elsewhere.
assert.fail = fail;
// 4. Pure assertion tests whether a value is truthy, as determined
// by !!guard.
// assert.ok(guard, message_opt);
// This statement is equivalent to assert.equal(true, !!guard,
// message_opt);. To test strictly for the value true, use
// assert.strictEqual(true, guard, message_opt);.
function ok(value, message) {
if (!value) fail(value, true, message, '==', assert.ok);
}
assert.ok = ok;
// 5. The equality assertion tests shallow, coercive equality with
// ==.
// assert.equal(actual, expected, message_opt);
assert.equal = function equal(actual, expected, message) {
2010-12-02 00:36:23 +00:00
if (actual != expected) fail(actual, expected, message, '==', assert.equal);
};
// 6. The non-equality assertion tests for whether two objects are not equal
// with != assert.notEqual(actual, expected, message_opt);
assert.notEqual = function notEqual(actual, expected, message) {
2010-04-11 20:46:24 +00:00
if (actual == expected) {
2010-12-02 00:36:23 +00:00
fail(actual, expected, message, '!=', assert.notEqual);
}
};
// 7. The equivalence assertion tests a deep equality relation.
// assert.deepEqual(actual, expected, message_opt);
assert.deepEqual = function deepEqual(actual, expected, message) {
2009-12-29 18:37:40 +00:00
if (!_deepEqual(actual, expected)) {
2010-12-02 00:36:23 +00:00
fail(actual, expected, message, 'deepEqual', assert.deepEqual);
2009-12-29 18:37:40 +00:00
}
};
function _deepEqual(actual, expected) {
2009-12-29 18:37:40 +00:00
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
} else if (util.isBuffer(actual) && util.isBuffer(expected)) {
2010-09-07 23:28:49 +00:00
if (actual.length != expected.length) return false;
for (var i = 0; i < actual.length; i++) {
if (actual[i] !== expected[i]) return false;
}
return true;
2009-12-29 18:37:40 +00:00
// 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.
} else if (util.isDate(actual) && util.isDate(expected)) {
2009-12-29 18:37:40 +00:00
return actual.getTime() === expected.getTime();
// 7.3 If the expected value is a RegExp object, the actual value is
// equivalent if it is also a RegExp object with the same source and
// properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
} else if (util.isRegExp(actual) && util.isRegExp(expected)) {
return actual.source === expected.source &&
actual.global === expected.global &&
actual.multiline === expected.multiline &&
actual.lastIndex === expected.lastIndex &&
actual.ignoreCase === expected.ignoreCase;
// 7.4. Other pairs that do not both pass typeof value == 'object',
2009-12-29 18:37:40 +00:00
// equivalence is determined by ==.
} else if (!util.isObject(actual) && !util.isObject(expected)) {
2009-12-29 18:37:40 +00:00
return actual == expected;
// 7.5 For all other Object pairs, including Array objects, equivalence is
2009-12-29 18:37:40 +00:00
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values for every
2010-12-02 00:36:23 +00:00
// corresponding key, and an identical 'prototype' property. Note: this
2009-12-29 18:37:40 +00:00
// accounts for both named and indexed properties on Arrays.
} else {
return objEquiv(actual, expected);
}
}
2010-12-02 00:36:23 +00:00
function isArguments(object) {
2009-12-29 18:37:40 +00:00
return Object.prototype.toString.call(object) == '[object Arguments]';
}
2010-12-02 00:36:23 +00:00
function objEquiv(a, b) {
if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b))
2009-12-29 18:37:40 +00:00
return false;
2010-12-02 00:36:23 +00:00
// an identical 'prototype' property.
2009-12-29 18:37:40 +00:00
if (a.prototype !== b.prototype) return false;
// if one is a primitive, the other must be same
if (util.isPrimitive(a) || util.isPrimitive(b))
return a === b;
var aIsArgs = isArguments(a),
bIsArgs = isArguments(b);
if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))
return false;
if (aIsArgs) {
2009-12-29 18:37:40 +00:00
a = pSlice.call(a);
b = pSlice.call(b);
return _deepEqual(a, b);
}
var ka = Object.keys(a),
kb = Object.keys(b),
key, i;
2010-12-02 00:36:23 +00:00
// having the same number of owned properties (keys incorporates
// hasOwnProperty)
2009-12-29 18:37:40 +00:00
if (ka.length != kb.length)
return false;
//the same set of keys (although not necessarily the same order),
ka.sort();
kb.sort();
//~~~cheap key test
for (i = ka.length - 1; i >= 0; i--) {
if (ka[i] != kb[i])
return false;
}
//equivalent values for every corresponding key, and
//~~~possibly expensive deep test
for (i = ka.length - 1; i >= 0; i--) {
key = ka[i];
2010-12-02 00:36:23 +00:00
if (!_deepEqual(a[key], b[key])) return false;
2009-12-29 18:37:40 +00:00
}
return true;
}
// 8. The non-equivalence assertion tests for any deep inequality.
// assert.notDeepEqual(actual, expected, message_opt);
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
2009-12-29 18:37:40 +00:00
if (_deepEqual(actual, expected)) {
2010-12-02 00:36:23 +00:00
fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
2009-12-29 18:37:40 +00:00
}
};
// 9. The strict equality assertion tests strict equality, as determined by ===.
// assert.strictEqual(actual, expected, message_opt);
assert.strictEqual = function strictEqual(actual, expected, message) {
if (actual !== expected) {
2010-12-02 00:36:23 +00:00
fail(actual, expected, message, '===', assert.strictEqual);
}
};
2010-12-02 00:36:23 +00:00
// 10. The strict non-equality assertion tests for strict inequality, as
// determined by !==. assert.notStrictEqual(actual, expected, message_opt);
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
if (actual === expected) {
2010-12-02 00:36:23 +00:00
fail(actual, expected, message, '!==', assert.notStrictEqual);
}
};
function expectedException(actual, expected) {
if (!actual || !expected) {
return false;
}
if (Object.prototype.toString.call(expected) == '[object RegExp]') {
2010-12-21 17:42:52 +00:00
return expected.test(actual);
} else if (actual instanceof expected) {
return true;
2011-01-07 00:06:27 +00:00
} else if (expected.call({}, actual) === true) {
return true;
}
2010-12-21 17:42:52 +00:00
return false;
}
2010-12-02 00:36:23 +00:00
function _throws(shouldThrow, block, expected, message) {
var actual;
if (util.isString(expected)) {
message = expected;
expected = null;
2009-12-29 18:37:40 +00:00
}
try {
block();
} catch (e) {
actual = e;
2009-12-29 18:37:40 +00:00
}
2010-12-02 00:36:23 +00:00
message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
(message ? ' ' + message : '.');
if (shouldThrow && !actual) {
fail(actual, expected, 'Missing expected exception' + message);
2009-12-29 18:37:40 +00:00
}
if (!shouldThrow && expectedException(actual, expected)) {
fail(actual, expected, 'Got unwanted exception' + message);
2009-12-29 18:37:40 +00:00
}
2010-12-02 00:36:23 +00:00
if ((shouldThrow && actual && expected &&
!expectedException(actual, expected)) || (!shouldThrow && actual)) {
throw actual;
2009-12-29 18:37:40 +00:00
}
}
2009-12-29 18:37:40 +00:00
// 11. Expected to throw an error:
// assert.throws(block, Error_opt, message_opt);
2009-12-29 18:37:40 +00:00
assert.throws = function(block, /*optional*/error, /*optional*/message) {
_throws.apply(this, [true].concat(pSlice.call(arguments)));
};
2009-12-29 18:37:40 +00:00
// EXTENSION! This is annoying to write outside this module.
assert.doesNotThrow = function(block, /*optional*/message) {
2009-12-29 18:37:40 +00:00
_throws.apply(this, [false].concat(pSlice.call(arguments)));
};
2010-12-02 00:36:23 +00:00
assert.ifError = function(err) { if (err) {throw err;}};