2009-11-30 02:20:37 +00:00
|
|
|
// 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
|
2010-03-15 15:00:19 +00:00
|
|
|
// of this software and associated documentation files (the 'Software'), to
|
2009-11-30 02:20:37 +00:00
|
|
|
// 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
|
2009-11-30 02:20:37 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-10-06 04:27:46 +00:00
|
|
|
const { isDeepEqual, isDeepStrictEqual } =
|
|
|
|
require('internal/util/comparisons');
|
2017-06-28 18:35:16 +00:00
|
|
|
const errors = require('internal/errors');
|
2017-04-25 20:18:25 +00:00
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// The assert module provides functions that throw
|
2009-11-30 02:20:37 +00:00
|
|
|
// AssertionError's when particular conditions are not met. The
|
|
|
|
// assert module must conform to the following interface.
|
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const assert = module.exports = ok;
|
2009-11-28 17:26:59 +00:00
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// All of the following functions must throw an AssertionError
|
2009-11-28 17:26:59 +00:00
|
|
|
// when a corresponding condition is not met, with a message that
|
2017-06-27 04:00:35 +00:00
|
|
|
// may be undefined if not provided. All assertion methods provide
|
2009-11-28 17:26:59 +00:00
|
|
|
// both the actual and expected values to the assertion error for
|
|
|
|
// display purposes.
|
|
|
|
|
2017-06-27 04:00:35 +00:00
|
|
|
function innerFail(actual, expected, message, operator, stackStartFunction) {
|
2017-09-10 01:36:47 +00:00
|
|
|
if (message instanceof Error) throw message;
|
|
|
|
|
2017-06-27 04:00:35 +00:00
|
|
|
throw new errors.AssertionError({
|
|
|
|
message,
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
operator,
|
|
|
|
stackStartFunction
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-12-29 19:14:58 +00:00
|
|
|
function fail(actual, expected, message, operator, stackStartFunction) {
|
2017-07-02 20:48:47 +00:00
|
|
|
const argsLen = arguments.length;
|
|
|
|
|
|
|
|
if (argsLen === 0) {
|
2017-06-28 23:57:02 +00:00
|
|
|
message = 'Failed';
|
2017-07-02 20:48:47 +00:00
|
|
|
} else if (argsLen === 1) {
|
2017-04-09 17:36:14 +00:00
|
|
|
message = actual;
|
2017-06-28 23:57:02 +00:00
|
|
|
actual = undefined;
|
2017-07-02 20:48:47 +00:00
|
|
|
} else if (argsLen === 2) {
|
2017-04-09 17:36:14 +00:00
|
|
|
operator = '!=';
|
2017-06-27 04:00:35 +00:00
|
|
|
}
|
2017-07-02 20:48:47 +00:00
|
|
|
|
2017-06-27 04:00:35 +00:00
|
|
|
innerFail(actual, expected, message, operator, stackStartFunction || fail);
|
2009-11-28 17:26:59 +00:00
|
|
|
}
|
2017-07-02 20:48:47 +00:00
|
|
|
|
2009-11-30 02:20:37 +00:00
|
|
|
assert.fail = fail;
|
|
|
|
|
2017-05-08 20:10:51 +00:00
|
|
|
// The AssertionError is defined in internal/error.
|
|
|
|
// new assert.AssertionError({ message: message,
|
|
|
|
// actual: actual,
|
|
|
|
// expected: expected });
|
2017-06-28 18:35:16 +00:00
|
|
|
assert.AssertionError = errors.AssertionError;
|
2017-05-08 20:10:51 +00:00
|
|
|
|
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// Pure assertion tests whether a value is truthy, as determined
|
2017-06-27 04:00:35 +00:00
|
|
|
// by !!value.
|
2011-10-01 11:42:42 +00:00
|
|
|
function ok(value, message) {
|
2017-06-27 04:00:35 +00:00
|
|
|
if (!value) innerFail(value, true, message, '==', ok);
|
2011-10-04 22:08:18 +00:00
|
|
|
}
|
2011-10-01 11:42:42 +00:00
|
|
|
assert.ok = ok;
|
2009-11-28 17:26:59 +00:00
|
|
|
|
2017-06-27 04:00:35 +00:00
|
|
|
// The equality assertion tests shallow, coercive equality with ==.
|
2017-01-08 23:23:34 +00:00
|
|
|
/* eslint-disable no-restricted-properties */
|
2009-11-30 02:20:37 +00:00
|
|
|
assert.equal = function equal(actual, expected, message) {
|
2017-04-16 18:29:35 +00:00
|
|
|
// eslint-disable-next-line eqeqeq
|
2017-06-27 04:00:35 +00:00
|
|
|
if (actual != expected) innerFail(actual, expected, message, '==', equal);
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// The non-equality assertion tests for whether two objects are not
|
|
|
|
// equal with !=.
|
2009-11-30 02:20:37 +00:00
|
|
|
assert.notEqual = function notEqual(actual, expected, message) {
|
2017-04-16 18:29:35 +00:00
|
|
|
// eslint-disable-next-line eqeqeq
|
2010-04-11 20:46:24 +00:00
|
|
|
if (actual == expected) {
|
2017-06-27 04:00:35 +00:00
|
|
|
innerFail(actual, expected, message, '!=', notEqual);
|
2009-12-29 19:14:58 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// The equivalence assertion tests a deep equality relation.
|
2009-12-29 19:14:58 +00:00
|
|
|
assert.deepEqual = function deepEqual(actual, expected, message) {
|
2017-10-06 04:27:46 +00:00
|
|
|
if (!isDeepEqual(actual, expected)) {
|
2017-06-27 04:00:35 +00:00
|
|
|
innerFail(actual, expected, message, 'deepEqual', deepEqual);
|
2009-12-29 18:37:40 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
2016-09-10 03:37:32 +00:00
|
|
|
/* eslint-enable */
|
2009-11-28 17:26:59 +00:00
|
|
|
|
2015-01-28 16:48:56 +00:00
|
|
|
assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
|
2017-10-06 04:27:46 +00:00
|
|
|
if (!isDeepStrictEqual(actual, expected)) {
|
2017-06-27 04:00:35 +00:00
|
|
|
innerFail(actual, expected, message, 'deepStrictEqual', deepStrictEqual);
|
2015-01-28 16:48:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// The non-equivalence assertion tests for any deep inequality.
|
2009-12-29 19:14:58 +00:00
|
|
|
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
|
2017-10-06 04:27:46 +00:00
|
|
|
if (isDeepEqual(actual, expected)) {
|
2017-06-27 04:00:35 +00:00
|
|
|
innerFail(actual, expected, message, 'notDeepEqual', notDeepEqual);
|
2009-12-29 18:37:40 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
|
|
|
|
2015-01-28 16:48:56 +00:00
|
|
|
assert.notDeepStrictEqual = notDeepStrictEqual;
|
|
|
|
function notDeepStrictEqual(actual, expected, message) {
|
2017-10-06 04:27:46 +00:00
|
|
|
if (isDeepStrictEqual(actual, expected)) {
|
2017-06-27 04:00:35 +00:00
|
|
|
innerFail(actual, expected, message, 'notDeepStrictEqual',
|
|
|
|
notDeepStrictEqual);
|
2015-01-28 16:48:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// The strict equality assertion tests strict equality, as determined by ===.
|
2009-11-30 02:20:37 +00:00
|
|
|
assert.strictEqual = function strictEqual(actual, expected, message) {
|
2009-12-29 19:14:58 +00:00
|
|
|
if (actual !== expected) {
|
2017-06-27 04:00:35 +00:00
|
|
|
innerFail(actual, expected, message, '===', strictEqual);
|
2009-12-29 19:14:58 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// The strict non-equality assertion tests for strict inequality, as
|
|
|
|
// determined by !==.
|
2009-11-30 02:20:37 +00:00
|
|
|
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
|
2009-12-29 19:14:58 +00:00
|
|
|
if (actual === expected) {
|
2017-06-27 04:00:35 +00:00
|
|
|
innerFail(actual, expected, message, '!==', notStrictEqual);
|
2009-12-29 19:14:58 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
|
|
|
|
2010-11-26 23:03:31 +00:00
|
|
|
function expectedException(actual, expected) {
|
2017-06-28 18:35:16 +00:00
|
|
|
if (typeof expected !== 'function') {
|
|
|
|
// Should be a RegExp, if not fail hard
|
2010-12-21 17:42:52 +00:00
|
|
|
return expected.test(actual);
|
2010-11-26 23:03:31 +00:00
|
|
|
}
|
2017-06-28 18:35:16 +00:00
|
|
|
// Guard instanceof against arrow functions as they don't have a prototype.
|
|
|
|
if (expected.prototype !== undefined && actual instanceof expected) {
|
|
|
|
return true;
|
2015-10-08 10:25:03 +00:00
|
|
|
}
|
2015-12-06 01:29:28 +00:00
|
|
|
if (Error.isPrototypeOf(expected)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-08 10:25:03 +00:00
|
|
|
return expected.call({}, actual) === true;
|
2010-11-26 23:03:31 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 04:00:35 +00:00
|
|
|
function tryBlock(block) {
|
2015-12-20 07:01:34 +00:00
|
|
|
try {
|
|
|
|
block();
|
|
|
|
} catch (e) {
|
2017-06-28 18:35:16 +00:00
|
|
|
return e;
|
2015-12-20 07:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 04:00:35 +00:00
|
|
|
function innerThrows(shouldThrow, block, expected, message) {
|
2017-06-28 18:35:16 +00:00
|
|
|
var details = '';
|
2010-11-26 23:03:31 +00:00
|
|
|
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof block !== 'function') {
|
2017-10-28 09:39:55 +00:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'Function',
|
2017-06-30 19:30:23 +00:00
|
|
|
block);
|
2015-01-12 16:13:18 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof expected === 'string') {
|
2010-11-26 23:03:31 +00:00
|
|
|
message = expected;
|
|
|
|
expected = null;
|
2009-12-29 18:37:40 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 18:35:16 +00:00
|
|
|
const actual = tryBlock(block);
|
2010-11-26 23:03:31 +00:00
|
|
|
|
2017-06-28 18:35:16 +00:00
|
|
|
if (shouldThrow === true) {
|
|
|
|
if (actual === undefined) {
|
|
|
|
if (expected && expected.name) {
|
|
|
|
details += ` (${expected.name})`;
|
|
|
|
}
|
|
|
|
details += message ? `: ${message}` : '.';
|
|
|
|
fail(actual, expected, `Missing expected exception${details}`, fail);
|
|
|
|
}
|
|
|
|
if (expected && expectedException(actual, expected) === false) {
|
|
|
|
throw actual;
|
|
|
|
}
|
|
|
|
} else if (actual !== undefined) {
|
|
|
|
if (!expected || expectedException(actual, expected)) {
|
|
|
|
details = message ? `: ${message}` : '.';
|
2017-04-02 17:11:42 +00:00
|
|
|
fail(actual,
|
|
|
|
expected,
|
|
|
|
`Got unwanted exception${details}\n${actual.message}`,
|
|
|
|
fail);
|
2017-06-28 18:35:16 +00:00
|
|
|
}
|
2010-11-26 23:03:31 +00:00
|
|
|
throw actual;
|
2009-12-29 18:37:40 +00:00
|
|
|
}
|
2010-11-26 23:03:31 +00:00
|
|
|
}
|
2009-11-30 02:20:37 +00:00
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// Expected to throw an error.
|
2017-06-27 04:00:35 +00:00
|
|
|
assert.throws = function throws(block, error, message) {
|
|
|
|
innerThrows(true, block, error, message);
|
2009-12-29 18:37:40 +00:00
|
|
|
};
|
2009-11-30 02:20:37 +00:00
|
|
|
|
2017-06-27 04:00:35 +00:00
|
|
|
assert.doesNotThrow = function doesNotThrow(block, error, message) {
|
|
|
|
innerThrows(false, block, error, message);
|
|
|
|
};
|
2009-12-29 19:14:58 +00:00
|
|
|
|
2016-10-12 07:37:44 +00:00
|
|
|
assert.ifError = function ifError(err) { if (err) throw err; };
|