Skip to content

Commit 1cf5e01

Browse files
lib: move hasMatchingError as private method to Assert
1 parent 502add9 commit 1cf5e01

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

lib/assert.js

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,35 @@ const assert = module.exports = ok;
8181

8282
const NO_EXCEPTION_SENTINEL = {};
8383

84+
class Comparison {
85+
constructor(obj, keys, actual) {
86+
for (const key of keys) {
87+
if (key in obj) {
88+
if (actual !== undefined &&
89+
typeof actual[key] === 'string' &&
90+
isRegExp(obj[key]) &&
91+
RegExpPrototypeExec(obj[key], actual[key]) !== null) {
92+
this[key] = actual[key];
93+
} else {
94+
this[key] = obj[key];
95+
}
96+
}
97+
}
98+
}
99+
}
100+
84101
class Assert {
85102
constructor(options = {}) {
86103
this.options = options;
87104
this.AssertionError = AssertionError;
88105
}
89106

107+
// All of the following functions must throw an AssertionError
108+
// when a corresponding condition is not met, with a message that
109+
// may be undefined if not provided. All assertion methods provide
110+
// both the actual and expected values to the assertion error for
111+
// display purposes.
112+
90113
#innerFail(obj) {
91114
if (obj.message instanceof Error) throw obj.message;
92115

@@ -323,7 +346,7 @@ class Assert {
323346
error = undefined;
324347
}
325348

326-
if (!error || hasMatchingError(actual, error)) {
349+
if (!error || this.#hasMatchingError(actual, error)) {
327350
const details = message ? `: ${message}` : '.';
328351
const fnType = stackStartFn === Assert.prototype.doesNotReject ?
329352
'rejection' : 'exception';
@@ -339,6 +362,26 @@ class Assert {
339362
throw actual;
340363
}
341364

365+
#hasMatchingError(actual, expected) {
366+
if (typeof expected !== 'function') {
367+
if (isRegExp(expected)) {
368+
const str = String(actual);
369+
return RegExpPrototypeExec(expected, str) !== null;
370+
}
371+
throw new ERR_INVALID_ARG_TYPE(
372+
'expected', ['Function', 'RegExp'], expected,
373+
);
374+
}
375+
// Guard instanceof against arrow functions as they don't have a prototype.
376+
if (expected.prototype !== undefined && actual instanceof expected) {
377+
return true;
378+
}
379+
if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
380+
return false;
381+
}
382+
return ReflectApply(expected, {}, [actual]) === true;
383+
}
384+
342385
async #waitForActual(promiseFn) {
343386
let resultPromise;
344387
if (typeof promiseFn === 'function') {
@@ -796,48 +839,6 @@ function ok(...args) {
796839
ObjectAssign(assert, assertInstance);
797840
assert.ok = ok;
798841

799-
// All of the following functions must throw an AssertionError
800-
// when a corresponding condition is not met, with a message that
801-
// may be undefined if not provided. All assertion methods provide
802-
// both the actual and expected values to the assertion error for
803-
// display purposes.
804-
class Comparison {
805-
constructor(obj, keys, actual) {
806-
for (const key of keys) {
807-
if (key in obj) {
808-
if (actual !== undefined &&
809-
typeof actual[key] === 'string' &&
810-
isRegExp(obj[key]) &&
811-
RegExpPrototypeExec(obj[key], actual[key]) !== null) {
812-
this[key] = actual[key];
813-
} else {
814-
this[key] = obj[key];
815-
}
816-
}
817-
}
818-
}
819-
}
820-
821-
function hasMatchingError(actual, expected) {
822-
if (typeof expected !== 'function') {
823-
if (isRegExp(expected)) {
824-
const str = String(actual);
825-
return RegExpPrototypeExec(expected, str) !== null;
826-
}
827-
throw new ERR_INVALID_ARG_TYPE(
828-
'expected', ['Function', 'RegExp'], expected,
829-
);
830-
}
831-
// Guard instanceof against arrow functions as they don't have a prototype.
832-
if (expected.prototype !== undefined && actual instanceof expected) {
833-
return true;
834-
}
835-
if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
836-
return false;
837-
}
838-
return ReflectApply(expected, {}, [actual]) === true;
839-
}
840-
841842
/**
842843
* Expose a strict only variant of assert.
843844
* @param {...any} args

0 commit comments

Comments
 (0)