assert,util: lazy load comparison functions

PR-URL: https://github.com/nodejs/node/pull/20567
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
This commit is contained in:
Ruben Bridgewater 2018-05-07 03:53:02 +02:00
parent e0c71ca3eb
commit 17d95ea1f0
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
2 changed files with 22 additions and 9 deletions

View File

@ -21,10 +21,6 @@
'use strict';
const { Buffer } = require('buffer');
const {
isDeepEqual,
isDeepStrictEqual
} = require('internal/util/comparisons');
const { codes: {
ERR_AMBIGUOUS_ARGUMENT,
ERR_INVALID_ARG_TYPE,
@ -37,6 +33,15 @@ const { inspect, types: { isPromise, isRegExp } } = require('util');
const { EOL } = require('internal/constants');
const { NativeModule } = require('internal/bootstrap/loaders');
let isDeepEqual;
let isDeepStrictEqual;
function lazyLoadComparison() {
const comparison = require('internal/util/comparisons');
isDeepEqual = comparison.isDeepEqual;
isDeepStrictEqual = comparison.isDeepStrictEqual;
}
// Escape control characters but not \n and \t to keep the line breaks and
// indentation intact.
// eslint-disable-next-line no-control-regex
@ -285,6 +290,7 @@ assert.notEqual = function notEqual(actual, expected, message) {
// The equivalence assertion tests a deep equality relation.
assert.deepEqual = function deepEqual(actual, expected, message) {
if (isDeepEqual === undefined) lazyLoadComparison();
if (!isDeepEqual(actual, expected)) {
innerFail({
actual,
@ -298,6 +304,7 @@ assert.deepEqual = function deepEqual(actual, expected, message) {
// The non-equivalence assertion tests for any deep inequality.
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
if (isDeepEqual === undefined) lazyLoadComparison();
if (isDeepEqual(actual, expected)) {
innerFail({
actual,
@ -311,6 +318,7 @@ assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
/* eslint-enable */
assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
if (isDeepEqual === undefined) lazyLoadComparison();
if (!isDeepStrictEqual(actual, expected)) {
innerFail({
actual,
@ -324,6 +332,7 @@ assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
assert.notDeepStrictEqual = notDeepStrictEqual;
function notDeepStrictEqual(actual, expected, message) {
if (isDeepEqual === undefined) lazyLoadComparison();
if (isDeepStrictEqual(actual, expected)) {
innerFail({
actual,
@ -437,6 +446,7 @@ function expectedException(actual, expected, msg) {
throw new ERR_INVALID_ARG_VALUE('error',
expected, 'may not be an empty object');
}
if (isDeepEqual === undefined) lazyLoadComparison();
for (const key of keys) {
if (typeof actual[key] === 'string' &&
isRegExp(expected[key]) &&

View File

@ -64,10 +64,6 @@ const {
isTypedArray
} = types;
const {
isDeepStrictEqual
} = require('internal/util/comparisons');
const {
customInspectSymbol,
deprecate,
@ -95,6 +91,7 @@ const dateToISOString = Date.prototype.toISOString;
const errorToString = Error.prototype.toString;
let CIRCULAR_ERROR_MESSAGE;
let internalDeepEqual;
/* eslint-disable */
const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/;
@ -1261,7 +1258,13 @@ module.exports = exports = {
isArray: Array.isArray,
isBoolean,
isBuffer,
isDeepStrictEqual,
isDeepStrictEqual(a, b) {
if (internalDeepEqual === undefined) {
internalDeepEqual = require('internal/util/comparisons')
.isDeepStrictEqual;
}
return internalDeepEqual(a, b);
},
isNull,
isNullOrUndefined,
isNumber,