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';
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
const {
|
2020-11-19 22:28:20 +00:00
|
|
|
ArrayPrototypeIndexOf,
|
|
|
|
ArrayPrototypeJoin,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeSlice,
|
2020-01-02 18:19:02 +00:00
|
|
|
Error,
|
2020-11-19 22:28:20 +00:00
|
|
|
NumberIsNaN,
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectAssign,
|
|
|
|
ObjectIs,
|
|
|
|
ObjectKeys,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2020-11-19 22:28:20 +00:00
|
|
|
ReflectApply,
|
2022-06-27 15:16:06 +00:00
|
|
|
RegExpPrototypeExec,
|
2020-01-06 02:55:31 +00:00
|
|
|
String,
|
2020-11-19 22:28:20 +00:00
|
|
|
StringPrototypeIndexOf,
|
2020-10-04 08:26:20 +00:00
|
|
|
StringPrototypeSlice,
|
2020-11-19 22:28:20 +00:00
|
|
|
StringPrototypeSplit,
|
2019-11-22 17:04:46 +00:00
|
|
|
} = primordials;
|
2019-04-09 07:55:53 +00:00
|
|
|
|
2019-09-16 20:32:15 +00:00
|
|
|
const {
|
|
|
|
codes: {
|
|
|
|
ERR_AMBIGUOUS_ARGUMENT,
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_INVALID_ARG_VALUE,
|
|
|
|
ERR_INVALID_RETURN_VALUE,
|
|
|
|
ERR_MISSING_ARGS,
|
|
|
|
},
|
|
|
|
} = require('internal/errors');
|
2019-02-06 04:46:35 +00:00
|
|
|
const AssertionError = require('internal/assert/assertion_error');
|
2019-03-19 00:05:06 +00:00
|
|
|
const { inspect } = require('internal/util/inspect');
|
|
|
|
const { isPromise, isRegExp } = require('internal/util/types');
|
2023-04-29 06:57:08 +00:00
|
|
|
const { isError, deprecate } = require('internal/util');
|
2024-09-15 15:53:18 +00:00
|
|
|
const { innerOk } = require('internal/assert/utils');
|
2017-12-09 22:20:07 +00:00
|
|
|
|
2020-02-27 11:41:05 +00:00
|
|
|
const CallTracker = require('internal/assert/calltracker');
|
2021-01-24 07:46:24 +00:00
|
|
|
const {
|
|
|
|
validateFunction,
|
|
|
|
} = require('internal/validators');
|
2018-10-07 01:27:54 +00:00
|
|
|
|
2018-05-07 01:53:02 +00:00
|
|
|
let isDeepEqual;
|
|
|
|
let isDeepStrictEqual;
|
|
|
|
|
|
|
|
function lazyLoadComparison() {
|
|
|
|
const comparison = require('internal/util/comparisons');
|
|
|
|
isDeepEqual = comparison.isDeepEqual;
|
|
|
|
isDeepStrictEqual = comparison.isDeepStrictEqual;
|
|
|
|
}
|
|
|
|
|
2018-01-28 11:07:18 +00:00
|
|
|
let warned = false;
|
|
|
|
|
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
|
|
|
|
2018-01-07 22:17:29 +00:00
|
|
|
const NO_EXCEPTION_SENTINEL = {};
|
|
|
|
|
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-12-09 19:15:19 +00:00
|
|
|
function innerFail(obj) {
|
|
|
|
if (obj.message instanceof Error) throw obj.message;
|
2017-09-10 01:36:47 +00:00
|
|
|
|
2018-01-22 23:11:22 +00:00
|
|
|
throw new AssertionError(obj);
|
2017-06-27 04:00:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* @param {any} actual
|
|
|
|
* @param {any} expected
|
|
|
|
* @param {string | Error} [message]
|
|
|
|
* @param {string} [operator]
|
|
|
|
* @param {Function} [stackStartFn]
|
|
|
|
*/
|
2017-12-09 19:15:19 +00:00
|
|
|
function fail(actual, expected, message, operator, stackStartFn) {
|
2017-07-02 20:48:47 +00:00
|
|
|
const argsLen = arguments.length;
|
|
|
|
|
2019-05-01 20:49:04 +00:00
|
|
|
let internalMessage = false;
|
|
|
|
if (actual == null && argsLen <= 1) {
|
|
|
|
internalMessage = true;
|
|
|
|
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;
|
2018-01-28 11:07:18 +00:00
|
|
|
} else {
|
|
|
|
if (warned === false) {
|
|
|
|
warned = true;
|
|
|
|
process.emitWarning(
|
|
|
|
'assert.fail() with more than one argument is deprecated. ' +
|
|
|
|
'Please use assert.strictEqual() instead or only pass a message.',
|
|
|
|
'DeprecationWarning',
|
2023-02-14 17:45:16 +00:00
|
|
|
'DEP0094',
|
2018-01-28 11:07:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (argsLen === 2)
|
|
|
|
operator = '!=';
|
2017-06-27 04:00:35 +00:00
|
|
|
}
|
2017-07-02 20:48:47 +00:00
|
|
|
|
2018-09-04 13:18:44 +00:00
|
|
|
if (message instanceof Error) throw message;
|
|
|
|
|
|
|
|
const errArgs = {
|
2017-12-09 19:15:19 +00:00
|
|
|
actual,
|
|
|
|
expected,
|
2018-09-04 13:18:44 +00:00
|
|
|
operator: operator === undefined ? 'fail' : operator,
|
2019-05-01 20:49:04 +00:00
|
|
|
stackStartFn: stackStartFn || fail,
|
2023-02-12 18:26:21 +00:00
|
|
|
message,
|
2018-09-04 13:18:44 +00:00
|
|
|
};
|
|
|
|
const err = new AssertionError(errArgs);
|
|
|
|
if (internalMessage) {
|
|
|
|
err.generatedMessage = true;
|
|
|
|
}
|
|
|
|
throw err;
|
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.
|
2018-01-22 23:11:22 +00:00
|
|
|
assert.AssertionError = AssertionError;
|
2017-05-08 20:10:51 +00:00
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* Pure assertion tests whether a value is truthy, as determined
|
|
|
|
* by !!value.
|
|
|
|
* @param {...any} args
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-12-09 22:20:07 +00:00
|
|
|
function ok(...args) {
|
2018-03-12 03:48:01 +00:00
|
|
|
innerOk(ok, args.length, ...args);
|
2017-12-09 22:20:07 +00:00
|
|
|
}
|
2011-10-01 11:42:42 +00:00
|
|
|
assert.ok = ok;
|
2009-11-28 17:26:59 +00:00
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* The equality assertion tests shallow, coercive equality with ==.
|
|
|
|
* @param {any} actual
|
|
|
|
* @param {any} expected
|
|
|
|
* @param {string | Error} [message]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
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) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2017-04-16 18:29:35 +00:00
|
|
|
// eslint-disable-next-line eqeqeq
|
2019-12-02 12:12:52 +00:00
|
|
|
if (actual != expected && (!NumberIsNaN(actual) || !NumberIsNaN(expected))) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: '==',
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: equal,
|
2017-12-09 19:15:19 +00:00
|
|
|
});
|
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* The non-equality assertion tests for whether two objects are not
|
|
|
|
* equal with !=.
|
|
|
|
* @param {any} actual
|
|
|
|
* @param {any} expected
|
|
|
|
* @param {string | Error} [message]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2009-11-30 02:20:37 +00:00
|
|
|
assert.notEqual = function notEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2017-04-16 18:29:35 +00:00
|
|
|
// eslint-disable-next-line eqeqeq
|
2019-12-02 12:12:52 +00:00
|
|
|
if (actual == expected || (NumberIsNaN(actual) && NumberIsNaN(expected))) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: '!=',
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: notEqual,
|
2017-12-09 19:15:19 +00:00
|
|
|
});
|
2009-12-29 19:14:58 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* The deep equivalence assertion tests a deep equality relation.
|
|
|
|
* @param {any} actual
|
|
|
|
* @param {any} expected
|
|
|
|
* @param {string | Error} [message]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2009-12-29 19:14:58 +00:00
|
|
|
assert.deepEqual = function deepEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2018-05-07 01:53:02 +00:00
|
|
|
if (isDeepEqual === undefined) lazyLoadComparison();
|
2017-10-06 04:27:46 +00:00
|
|
|
if (!isDeepEqual(actual, expected)) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: 'deepEqual',
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: deepEqual,
|
2017-12-09 19:15:19 +00:00
|
|
|
});
|
2009-12-29 18:37:40 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
2015-01-28 16:48:56 +00:00
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* The deep non-equivalence assertion tests for any deep inequality.
|
|
|
|
* @param {any} actual
|
|
|
|
* @param {any} expected
|
|
|
|
* @param {string | Error} [message]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2009-12-29 19:14:58 +00:00
|
|
|
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2018-05-07 01:53:02 +00:00
|
|
|
if (isDeepEqual === undefined) lazyLoadComparison();
|
2017-10-06 04:27:46 +00:00
|
|
|
if (isDeepEqual(actual, expected)) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: 'notDeepEqual',
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: notDeepEqual,
|
2017-12-09 19:15:19 +00:00
|
|
|
});
|
2009-12-29 18:37:40 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
2017-10-19 19:39:29 +00:00
|
|
|
/* eslint-enable */
|
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* The deep strict equivalence assertion tests a deep strict equality
|
|
|
|
* relation.
|
|
|
|
* @param {any} actual
|
|
|
|
* @param {any} expected
|
|
|
|
* @param {string | Error} [message]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-10-19 19:39:29 +00:00
|
|
|
assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2018-05-07 01:53:02 +00:00
|
|
|
if (isDeepEqual === undefined) lazyLoadComparison();
|
2017-10-19 19:39:29 +00:00
|
|
|
if (!isDeepStrictEqual(actual, expected)) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: 'deepStrictEqual',
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: deepStrictEqual,
|
2017-12-09 19:15:19 +00:00
|
|
|
});
|
2017-10-19 19:39:29 +00:00
|
|
|
}
|
|
|
|
};
|
2009-11-28 17:26:59 +00:00
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* The deep strict non-equivalence assertion tests for any deep strict
|
|
|
|
* inequality.
|
|
|
|
* @param {any} actual
|
|
|
|
* @param {any} expected
|
|
|
|
* @param {string | Error} [message]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2015-01-28 16:48:56 +00:00
|
|
|
assert.notDeepStrictEqual = notDeepStrictEqual;
|
|
|
|
function notDeepStrictEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2018-05-07 01:53:02 +00:00
|
|
|
if (isDeepEqual === undefined) lazyLoadComparison();
|
2017-10-06 04:27:46 +00:00
|
|
|
if (isDeepStrictEqual(actual, expected)) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: 'notDeepStrictEqual',
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: notDeepStrictEqual,
|
2017-12-09 19:15:19 +00:00
|
|
|
});
|
2015-01-28 16:48:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* The strict equivalence assertion tests a strict equality relation.
|
|
|
|
* @param {any} actual
|
|
|
|
* @param {any} expected
|
|
|
|
* @param {string | Error} [message]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2009-11-30 02:20:37 +00:00
|
|
|
assert.strictEqual = function strictEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2019-11-22 17:04:46 +00:00
|
|
|
if (!ObjectIs(actual, expected)) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: 'strictEqual',
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: strictEqual,
|
2017-12-09 19:15:19 +00:00
|
|
|
});
|
2009-12-29 19:14:58 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* The strict non-equivalence assertion tests for any strict inequality.
|
|
|
|
* @param {any} actual
|
|
|
|
* @param {any} expected
|
|
|
|
* @param {string | Error} [message]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2009-11-30 02:20:37 +00:00
|
|
|
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2019-11-22 17:04:46 +00:00
|
|
|
if (ObjectIs(actual, expected)) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: 'notStrictEqual',
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: notStrictEqual,
|
2017-12-09 19:15:19 +00:00
|
|
|
});
|
2009-12-29 19:14:58 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
|
|
|
|
2018-03-20 00:49:29 +00:00
|
|
|
class Comparison {
|
2018-05-02 17:26:32 +00:00
|
|
|
constructor(obj, keys, actual) {
|
2018-03-20 00:49:29 +00:00
|
|
|
for (const key of keys) {
|
2018-05-02 17:26:32 +00:00
|
|
|
if (key in obj) {
|
|
|
|
if (actual !== undefined &&
|
|
|
|
typeof actual[key] === 'string' &&
|
|
|
|
isRegExp(obj[key]) &&
|
2022-06-27 15:16:06 +00:00
|
|
|
RegExpPrototypeExec(obj[key], actual[key]) !== null) {
|
2018-05-02 17:26:32 +00:00
|
|
|
this[key] = actual[key];
|
|
|
|
} else {
|
|
|
|
this[key] = obj[key];
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 00:49:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 01:12:42 +00:00
|
|
|
function compareExceptionKey(actual, expected, key, message, keys, fn) {
|
2018-03-20 00:49:29 +00:00
|
|
|
if (!(key in actual) || !isDeepStrictEqual(actual[key], expected[key])) {
|
|
|
|
if (!message) {
|
|
|
|
// Create placeholder objects to create a nice output.
|
|
|
|
const a = new Comparison(actual, keys);
|
2018-05-02 17:26:32 +00:00
|
|
|
const b = new Comparison(expected, keys, actual);
|
2018-03-20 00:49:29 +00:00
|
|
|
|
|
|
|
const err = new AssertionError({
|
|
|
|
actual: a,
|
|
|
|
expected: b,
|
|
|
|
operator: 'deepStrictEqual',
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: fn,
|
2018-03-20 00:49:29 +00:00
|
|
|
});
|
2018-05-02 14:59:49 +00:00
|
|
|
err.actual = actual;
|
|
|
|
err.expected = expected;
|
2019-04-02 01:12:42 +00:00
|
|
|
err.operator = fn.name;
|
2018-05-02 14:59:49 +00:00
|
|
|
throw err;
|
2018-03-20 00:49:29 +00:00
|
|
|
}
|
2018-02-06 10:08:23 +00:00
|
|
|
innerFail({
|
2018-03-20 00:49:29 +00:00
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
2019-04-02 01:12:42 +00:00
|
|
|
operator: fn.name,
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: fn,
|
2018-02-06 10:08:23 +00:00
|
|
|
});
|
|
|
|
}
|
2017-12-10 00:54:44 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 21:23:18 +00:00
|
|
|
function expectedException(actual, expected, message, fn) {
|
2019-06-13 22:24:31 +00:00
|
|
|
let generatedMessage = false;
|
2019-06-14 14:29:25 +00:00
|
|
|
let throwError = false;
|
2019-06-13 22:24:31 +00:00
|
|
|
|
2017-06-28 18:35:16 +00:00
|
|
|
if (typeof expected !== 'function') {
|
2019-05-19 21:23:18 +00:00
|
|
|
// Handle regular expressions.
|
|
|
|
if (isRegExp(expected)) {
|
|
|
|
const str = String(actual);
|
2022-06-27 15:16:06 +00:00
|
|
|
if (RegExpPrototypeExec(expected, str) !== null)
|
2019-05-19 21:23:18 +00:00
|
|
|
return;
|
|
|
|
|
2019-06-14 08:49:20 +00:00
|
|
|
if (!message) {
|
|
|
|
generatedMessage = true;
|
|
|
|
message = 'The input did not match the regular expression ' +
|
|
|
|
`${inspect(expected)}. Input:\n\n${inspect(str)}\n`;
|
|
|
|
}
|
2019-06-14 14:29:25 +00:00
|
|
|
throwError = true;
|
|
|
|
// Handle primitives properly.
|
|
|
|
} else if (typeof actual !== 'object' || actual === null) {
|
2018-05-02 14:59:49 +00:00
|
|
|
const err = new AssertionError({
|
|
|
|
actual,
|
|
|
|
expected,
|
2019-05-19 21:23:18 +00:00
|
|
|
message,
|
2018-05-02 14:59:49 +00:00
|
|
|
operator: 'deepStrictEqual',
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: fn,
|
2018-05-02 14:59:49 +00:00
|
|
|
});
|
2019-04-02 01:12:42 +00:00
|
|
|
err.operator = fn.name;
|
2018-05-02 14:59:49 +00:00
|
|
|
throw err;
|
2019-06-14 14:29:25 +00:00
|
|
|
} else {
|
|
|
|
// Handle validation objects.
|
2019-11-22 17:04:46 +00:00
|
|
|
const keys = ObjectKeys(expected);
|
2019-06-14 14:29:25 +00:00
|
|
|
// Special handle errors to make sure the name and the message are
|
|
|
|
// compared as well.
|
|
|
|
if (expected instanceof Error) {
|
2020-11-19 22:28:20 +00:00
|
|
|
ArrayPrototypePush(keys, 'name', 'message');
|
2019-06-14 14:29:25 +00:00
|
|
|
} else if (keys.length === 0) {
|
|
|
|
throw new ERR_INVALID_ARG_VALUE('error',
|
|
|
|
expected, 'may not be an empty object');
|
2018-05-02 17:26:32 +00:00
|
|
|
}
|
2019-06-14 14:29:25 +00:00
|
|
|
if (isDeepEqual === undefined) lazyLoadComparison();
|
|
|
|
for (const key of keys) {
|
|
|
|
if (typeof actual[key] === 'string' &&
|
|
|
|
isRegExp(expected[key]) &&
|
2022-06-27 15:16:06 +00:00
|
|
|
RegExpPrototypeExec(expected[key], actual[key]) !== null) {
|
2019-06-14 14:29:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
compareExceptionKey(actual, expected, key, message, keys, fn);
|
|
|
|
}
|
|
|
|
return;
|
2017-12-10 00:54:44 +00:00
|
|
|
}
|
2017-06-28 18:35:16 +00:00
|
|
|
// Guard instanceof against arrow functions as they don't have a prototype.
|
2019-05-19 21:23:18 +00:00
|
|
|
// Check for matching Error classes.
|
2019-06-14 14:29:25 +00:00
|
|
|
} else if (expected.prototype !== undefined && actual instanceof expected) {
|
2019-05-19 21:23:18 +00:00
|
|
|
return;
|
2019-11-22 17:04:46 +00:00
|
|
|
} else if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
|
2019-06-13 22:24:31 +00:00
|
|
|
if (!message) {
|
|
|
|
generatedMessage = true;
|
|
|
|
message = 'The error is expected to be an instance of ' +
|
|
|
|
`"${expected.name}". Received `;
|
|
|
|
if (isError(actual)) {
|
2024-09-24 19:48:15 +00:00
|
|
|
const name = (actual.constructor?.name) ||
|
2019-06-15 11:54:50 +00:00
|
|
|
actual.name;
|
|
|
|
if (expected.name === name) {
|
|
|
|
message += 'an error with identical name but a different prototype.';
|
|
|
|
} else {
|
|
|
|
message += `"${name}"`;
|
|
|
|
}
|
2019-06-15 11:27:21 +00:00
|
|
|
if (actual.message) {
|
|
|
|
message += `\n\nError message:\n\n${actual.message}`;
|
|
|
|
}
|
2019-06-13 22:24:31 +00:00
|
|
|
} else {
|
|
|
|
message += `"${inspect(actual, { depth: -1 })}"`;
|
|
|
|
}
|
|
|
|
}
|
2019-06-14 14:29:25 +00:00
|
|
|
throwError = true;
|
|
|
|
} else {
|
|
|
|
// Check validation functions return value.
|
2020-11-19 22:28:20 +00:00
|
|
|
const res = ReflectApply(expected, {}, [actual]);
|
2019-06-14 14:29:25 +00:00
|
|
|
if (res !== true) {
|
|
|
|
if (!message) {
|
|
|
|
generatedMessage = true;
|
|
|
|
const name = expected.name ? `"${expected.name}" ` : '';
|
|
|
|
message = `The ${name}validation function is expected to return` +
|
|
|
|
` "true". Received ${inspect(res)}`;
|
2019-06-15 11:27:21 +00:00
|
|
|
|
|
|
|
if (isError(actual)) {
|
|
|
|
message += `\n\nCaught error:\n\n${actual}`;
|
|
|
|
}
|
2019-06-14 14:29:25 +00:00
|
|
|
}
|
|
|
|
throwError = true;
|
|
|
|
}
|
2019-05-19 21:23:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 14:29:25 +00:00
|
|
|
if (throwError) {
|
2019-06-14 14:14:50 +00:00
|
|
|
const err = new AssertionError({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: fn.name,
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: fn,
|
2019-06-14 14:14:50 +00:00
|
|
|
});
|
|
|
|
err.generatedMessage = generatedMessage;
|
|
|
|
throw err;
|
2015-12-06 01:29:28 +00:00
|
|
|
}
|
2010-11-26 23:03:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 00:30:07 +00:00
|
|
|
function getActual(fn) {
|
2021-01-24 07:46:24 +00:00
|
|
|
validateFunction(fn, 'fn');
|
2015-12-20 07:01:34 +00:00
|
|
|
try {
|
2018-09-08 00:30:07 +00:00
|
|
|
fn();
|
2015-12-20 07:01:34 +00:00
|
|
|
} catch (e) {
|
2017-06-28 18:35:16 +00:00
|
|
|
return e;
|
2015-12-20 07:01:34 +00:00
|
|
|
}
|
2018-01-07 22:17:29 +00:00
|
|
|
return NO_EXCEPTION_SENTINEL;
|
2015-12-20 07:01:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 21:28:30 +00:00
|
|
|
function checkIsPromise(obj) {
|
|
|
|
// Accept native ES6 promises and promises that are implemented in a similar
|
|
|
|
// way. Do not accept thenables that use a function as `obj` and that have no
|
|
|
|
// `catch` handler.
|
|
|
|
return isPromise(obj) ||
|
2019-10-03 16:35:41 +00:00
|
|
|
(obj !== null && typeof obj === 'object' &&
|
2018-04-08 21:28:30 +00:00
|
|
|
typeof obj.then === 'function' &&
|
2019-10-03 16:35:41 +00:00
|
|
|
typeof obj.catch === 'function');
|
2018-04-08 21:28:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 00:30:07 +00:00
|
|
|
async function waitForActual(promiseFn) {
|
2018-04-08 21:28:30 +00:00
|
|
|
let resultPromise;
|
2018-09-08 00:30:07 +00:00
|
|
|
if (typeof promiseFn === 'function') {
|
|
|
|
// Return a rejected promise if `promiseFn` throws synchronously.
|
|
|
|
resultPromise = promiseFn();
|
2018-04-08 22:38:41 +00:00
|
|
|
// Fail in case no promise is returned.
|
|
|
|
if (!checkIsPromise(resultPromise)) {
|
|
|
|
throw new ERR_INVALID_RETURN_VALUE('instance of Promise',
|
2018-09-08 00:30:07 +00:00
|
|
|
'promiseFn', resultPromise);
|
2018-04-08 22:38:41 +00:00
|
|
|
}
|
2018-09-08 00:30:07 +00:00
|
|
|
} else if (checkIsPromise(promiseFn)) {
|
|
|
|
resultPromise = promiseFn;
|
2018-04-08 21:28:30 +00:00
|
|
|
} else {
|
2018-09-08 00:30:07 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'promiseFn', ['Function', 'Promise'], promiseFn);
|
2018-01-11 23:16:41 +00:00
|
|
|
}
|
2018-03-28 05:21:31 +00:00
|
|
|
|
2018-01-11 23:16:41 +00:00
|
|
|
try {
|
2018-03-28 05:21:31 +00:00
|
|
|
await resultPromise;
|
2018-01-11 23:16:41 +00:00
|
|
|
} catch (e) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
return NO_EXCEPTION_SENTINEL;
|
|
|
|
}
|
2010-11-26 23:03:31 +00:00
|
|
|
|
2018-01-11 23:16:41 +00:00
|
|
|
function expectsError(stackStartFn, actual, error, message) {
|
2017-12-09 23:47:49 +00:00
|
|
|
if (typeof error === 'string') {
|
2018-01-11 23:16:41 +00:00
|
|
|
if (arguments.length === 4) {
|
2018-04-07 12:20:43 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('error',
|
|
|
|
['Object', 'Error', 'Function', 'RegExp'],
|
|
|
|
error);
|
2018-01-11 23:16:41 +00:00
|
|
|
}
|
2018-04-07 12:22:29 +00:00
|
|
|
if (typeof actual === 'object' && actual !== null) {
|
|
|
|
if (actual.message === error) {
|
|
|
|
throw new ERR_AMBIGUOUS_ARGUMENT(
|
|
|
|
'error/message',
|
2023-02-14 17:45:16 +00:00
|
|
|
`The error message "${actual.message}" is identical to the message.`,
|
2018-04-07 12:22:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (actual === error) {
|
|
|
|
throw new ERR_AMBIGUOUS_ARGUMENT(
|
|
|
|
'error/message',
|
2023-02-14 17:45:16 +00:00
|
|
|
`The error "${actual}" is identical to the message.`,
|
2018-04-07 12:22:29 +00:00
|
|
|
);
|
|
|
|
}
|
2017-12-09 23:47:49 +00:00
|
|
|
message = error;
|
2018-04-11 01:28:35 +00:00
|
|
|
error = undefined;
|
2018-05-02 22:23:47 +00:00
|
|
|
} else if (error != null &&
|
|
|
|
typeof error !== 'object' &&
|
|
|
|
typeof error !== 'function') {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('error',
|
|
|
|
['Object', 'Error', 'Function', 'RegExp'],
|
|
|
|
error);
|
2009-12-29 18:37:40 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 22:17:29 +00:00
|
|
|
if (actual === NO_EXCEPTION_SENTINEL) {
|
2017-12-09 23:47:49 +00:00
|
|
|
let details = '';
|
2024-09-24 19:48:15 +00:00
|
|
|
if (error?.name) {
|
2017-12-09 23:47:49 +00:00
|
|
|
details += ` (${error.name})`;
|
2017-06-28 18:35:16 +00:00
|
|
|
}
|
2017-12-09 23:47:49 +00:00
|
|
|
details += message ? `: ${message}` : '.';
|
2021-12-14 14:58:47 +00:00
|
|
|
const fnType = stackStartFn === assert.rejects ? 'rejection' : 'exception';
|
2017-12-09 23:47:49 +00:00
|
|
|
innerFail({
|
2018-04-11 01:28:35 +00:00
|
|
|
actual: undefined,
|
2017-12-09 23:47:49 +00:00
|
|
|
expected: error,
|
2018-01-11 23:16:41 +00:00
|
|
|
operator: stackStartFn.name,
|
|
|
|
message: `Missing expected ${fnType}${details}`,
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn,
|
2017-12-09 23:47:49 +00:00
|
|
|
});
|
|
|
|
}
|
2019-05-19 21:23:18 +00:00
|
|
|
|
|
|
|
if (!error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
expectedException(actual, error, message, stackStartFn);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasMatchingError(actual, expected) {
|
|
|
|
if (typeof expected !== 'function') {
|
|
|
|
if (isRegExp(expected)) {
|
|
|
|
const str = String(actual);
|
2022-06-27 15:16:06 +00:00
|
|
|
return RegExpPrototypeExec(expected, str) !== null;
|
2019-05-19 21:23:18 +00:00
|
|
|
}
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
2023-02-14 17:45:16 +00:00
|
|
|
'expected', ['Function', 'RegExp'], expected,
|
2019-05-19 21:23:18 +00:00
|
|
|
);
|
2009-12-29 18:37:40 +00:00
|
|
|
}
|
2019-05-19 21:23:18 +00:00
|
|
|
// Guard instanceof against arrow functions as they don't have a prototype.
|
|
|
|
if (expected.prototype !== undefined && actual instanceof expected) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-27 19:41:34 +00:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
|
2019-05-19 21:23:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-11-19 22:28:20 +00:00
|
|
|
return ReflectApply(expected, {}, [actual]) === true;
|
2018-01-11 23:16:41 +00:00
|
|
|
}
|
2009-11-30 02:20:37 +00:00
|
|
|
|
2018-01-11 23:16:41 +00:00
|
|
|
function expectsNoError(stackStartFn, actual, error, message) {
|
2018-01-07 22:17:29 +00:00
|
|
|
if (actual === NO_EXCEPTION_SENTINEL)
|
2017-12-09 23:47:49 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (typeof error === 'string') {
|
|
|
|
message = error;
|
2018-04-11 01:28:35 +00:00
|
|
|
error = undefined;
|
2017-12-09 23:47:49 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 21:23:18 +00:00
|
|
|
if (!error || hasMatchingError(actual, error)) {
|
2017-12-09 23:47:49 +00:00
|
|
|
const details = message ? `: ${message}` : '.';
|
2021-12-14 14:58:47 +00:00
|
|
|
const fnType = stackStartFn === assert.doesNotReject ?
|
2018-04-08 21:28:30 +00:00
|
|
|
'rejection' : 'exception';
|
2017-12-09 23:47:49 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected: error,
|
2018-01-11 23:16:41 +00:00
|
|
|
operator: stackStartFn.name,
|
2018-04-09 01:57:40 +00:00
|
|
|
message: `Got unwanted ${fnType}${details}\n` +
|
2024-09-24 19:48:15 +00:00
|
|
|
`Actual message: "${actual?.message}"`,
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn,
|
2017-12-09 23:47:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
throw actual;
|
2018-01-11 23:16:41 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* Expects the function `promiseFn` to throw an error.
|
|
|
|
* @param {() => any} promiseFn
|
|
|
|
* @param {...any} [args]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-09-08 00:30:07 +00:00
|
|
|
assert.throws = function throws(promiseFn, ...args) {
|
|
|
|
expectsError(throws, getActual(promiseFn), ...args);
|
2018-04-08 21:28:30 +00:00
|
|
|
};
|
2018-01-11 23:16:41 +00:00
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* Expects `promiseFn` function or its value to reject.
|
|
|
|
* @param {() => Promise<any>} promiseFn
|
|
|
|
* @param {...any} [args]
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2018-09-08 00:30:07 +00:00
|
|
|
assert.rejects = async function rejects(promiseFn, ...args) {
|
|
|
|
expectsError(rejects, await waitForActual(promiseFn), ...args);
|
2018-04-08 21:28:30 +00:00
|
|
|
};
|
2018-01-11 23:16:41 +00:00
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* Asserts that the function `fn` does not throw an error.
|
|
|
|
* @param {() => any} fn
|
|
|
|
* @param {...any} [args]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-09-08 00:30:07 +00:00
|
|
|
assert.doesNotThrow = function doesNotThrow(fn, ...args) {
|
|
|
|
expectsNoError(doesNotThrow, getActual(fn), ...args);
|
2018-04-08 21:28:30 +00:00
|
|
|
};
|
2018-01-11 23:16:41 +00:00
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* Expects `fn` or its value to not reject.
|
|
|
|
* @param {() => Promise<any>} fn
|
|
|
|
* @param {...any} [args]
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2018-09-08 00:30:07 +00:00
|
|
|
assert.doesNotReject = async function doesNotReject(fn, ...args) {
|
|
|
|
expectsNoError(doesNotReject, await waitForActual(fn), ...args);
|
2018-04-08 21:28:30 +00:00
|
|
|
};
|
2009-12-29 19:14:58 +00:00
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* Throws `value` if the value is not `null` or `undefined`.
|
|
|
|
* @param {any} err
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-01-19 09:35:39 +00:00
|
|
|
assert.ifError = function ifError(err) {
|
2018-01-23 13:07:18 +00:00
|
|
|
if (err !== null && err !== undefined) {
|
2018-01-19 09:35:39 +00:00
|
|
|
let message = 'ifError got unwanted exception: ';
|
|
|
|
if (typeof err === 'object' && typeof err.message === 'string') {
|
|
|
|
if (err.message.length === 0 && err.constructor) {
|
|
|
|
message += err.constructor.name;
|
|
|
|
} else {
|
|
|
|
message += err.message;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
message += inspect(err);
|
|
|
|
}
|
|
|
|
|
2018-01-22 23:11:22 +00:00
|
|
|
const newErr = new AssertionError({
|
2018-01-19 09:35:39 +00:00
|
|
|
actual: err,
|
|
|
|
expected: null,
|
|
|
|
operator: 'ifError',
|
|
|
|
message,
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: ifError,
|
2018-01-19 09:35:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Make sure we actually have a stack trace!
|
|
|
|
const origStack = err.stack;
|
|
|
|
|
|
|
|
if (typeof origStack === 'string') {
|
|
|
|
// This will remove any duplicated frames from the error frames taken
|
|
|
|
// from within `ifError` and add the original error frames to the newly
|
|
|
|
// created ones.
|
2022-03-15 00:17:12 +00:00
|
|
|
const origStackStart = StringPrototypeIndexOf(origStack, '\n at');
|
2021-12-06 17:38:26 +00:00
|
|
|
if (origStackStart !== -1) {
|
|
|
|
const originalFrames = StringPrototypeSplit(
|
2022-03-15 00:17:12 +00:00
|
|
|
StringPrototypeSlice(origStack, origStackStart + 1),
|
2023-02-14 17:45:16 +00:00
|
|
|
'\n',
|
2021-12-06 17:38:26 +00:00
|
|
|
);
|
|
|
|
// Filter all frames existing in err.stack.
|
|
|
|
let newFrames = StringPrototypeSplit(newErr.stack, '\n');
|
|
|
|
for (const errFrame of originalFrames) {
|
|
|
|
// Find the first occurrence of the frame.
|
|
|
|
const pos = ArrayPrototypeIndexOf(newFrames, errFrame);
|
|
|
|
if (pos !== -1) {
|
|
|
|
// Only keep new frames.
|
|
|
|
newFrames = ArrayPrototypeSlice(newFrames, 0, pos);
|
|
|
|
break;
|
|
|
|
}
|
2018-01-19 09:35:39 +00:00
|
|
|
}
|
2021-12-06 17:38:26 +00:00
|
|
|
const stackStart = ArrayPrototypeJoin(newFrames, '\n');
|
|
|
|
const stackEnd = ArrayPrototypeJoin(originalFrames, '\n');
|
|
|
|
newErr.stack = `${stackStart}\n${stackEnd}`;
|
2018-01-19 09:35:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw newErr;
|
|
|
|
}
|
|
|
|
};
|
2017-11-13 20:15:24 +00:00
|
|
|
|
2019-12-12 21:51:43 +00:00
|
|
|
function internalMatch(string, regexp, message, fn) {
|
|
|
|
if (!isRegExp(regexp)) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
2023-02-14 17:45:16 +00:00
|
|
|
'regexp', 'RegExp', regexp,
|
2019-12-12 21:51:43 +00:00
|
|
|
);
|
|
|
|
}
|
2021-12-14 14:58:47 +00:00
|
|
|
const match = fn === assert.match;
|
2019-12-12 21:51:43 +00:00
|
|
|
if (typeof string !== 'string' ||
|
2022-06-27 15:16:06 +00:00
|
|
|
RegExpPrototypeExec(regexp, string) !== null !== match) {
|
2019-12-12 21:51:43 +00:00
|
|
|
if (message instanceof Error) {
|
|
|
|
throw message;
|
|
|
|
}
|
|
|
|
|
|
|
|
const generatedMessage = !message;
|
|
|
|
|
|
|
|
// 'The input was expected to not match the regular expression ' +
|
2024-10-09 06:42:16 +00:00
|
|
|
message ||= (typeof string !== 'string' ?
|
2019-12-12 21:51:43 +00:00
|
|
|
'The "string" argument must be of type string. Received type ' +
|
|
|
|
`${typeof string} (${inspect(string)})` :
|
|
|
|
(match ?
|
|
|
|
'The input did not match the regular expression ' :
|
|
|
|
'The input was expected to not match the regular expression ') +
|
|
|
|
`${inspect(regexp)}. Input:\n\n${inspect(string)}\n`);
|
|
|
|
const err = new AssertionError({
|
|
|
|
actual: string,
|
|
|
|
expected: regexp,
|
|
|
|
message,
|
|
|
|
operator: fn.name,
|
2023-02-12 18:26:21 +00:00
|
|
|
stackStartFn: fn,
|
2019-12-12 21:51:43 +00:00
|
|
|
});
|
|
|
|
err.generatedMessage = generatedMessage;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* Expects the `string` input to match the regular expression.
|
|
|
|
* @param {string} string
|
|
|
|
* @param {RegExp} regexp
|
|
|
|
* @param {string | Error} [message]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-12-12 21:51:43 +00:00
|
|
|
assert.match = function match(string, regexp, message) {
|
|
|
|
internalMatch(string, regexp, message, match);
|
|
|
|
};
|
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* Expects the `string` input not to match the regular expression.
|
|
|
|
* @param {string} string
|
|
|
|
* @param {RegExp} regexp
|
|
|
|
* @param {string | Error} [message]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-12-12 21:51:43 +00:00
|
|
|
assert.doesNotMatch = function doesNotMatch(string, regexp, message) {
|
|
|
|
internalMatch(string, regexp, message, doesNotMatch);
|
|
|
|
};
|
|
|
|
|
2023-04-29 06:57:08 +00:00
|
|
|
assert.CallTracker = deprecate(CallTracker, 'assert.CallTracker is deprecated.', 'DEP0173');
|
2020-02-27 11:41:05 +00:00
|
|
|
|
2021-04-10 18:43:52 +00:00
|
|
|
/**
|
|
|
|
* Expose a strict only variant of assert.
|
|
|
|
* @param {...any} args
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-12-09 22:20:07 +00:00
|
|
|
function strict(...args) {
|
2018-03-12 03:48:01 +00:00
|
|
|
innerOk(strict, args.length, ...args);
|
2017-11-13 20:15:24 +00:00
|
|
|
}
|
2021-04-10 18:43:52 +00:00
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
assert.strict = ObjectAssign(strict, assert, {
|
2017-11-13 20:15:24 +00:00
|
|
|
equal: assert.strictEqual,
|
|
|
|
deepEqual: assert.deepStrictEqual,
|
|
|
|
notEqual: assert.notStrictEqual,
|
2023-02-12 18:26:21 +00:00
|
|
|
notDeepEqual: assert.notDeepStrictEqual,
|
2017-11-13 20:15:24 +00:00
|
|
|
});
|
2021-04-10 18:43:52 +00:00
|
|
|
|
2017-11-13 20:15:24 +00:00
|
|
|
assert.strict.strict = assert.strict;
|