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