Skip to content

Commit bf60d2e

Browse files
lib: move hasMatchingError as private method to Assert
1 parent e5e0a83 commit bf60d2e

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

lib/assert.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,35 @@ const assert = module.exports = ok;
7979

8080
const NO_EXCEPTION_SENTINEL = {};
8181

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

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

@@ -321,7 +344,7 @@ class Assert {
321344
error = undefined;
322345
}
323346

324-
if (!error || hasMatchingError(actual, error)) {
347+
if (!error || this.#hasMatchingError(actual, error)) {
325348
const details = message ? `: ${message}` : '.';
326349
const fnType = stackStartFn === Assert.prototype.doesNotReject ?
327350
'rejection' : 'exception';
@@ -337,6 +360,26 @@ class Assert {
337360
throw actual;
338361
}
339362

363+
#hasMatchingError(actual, expected) {
364+
if (typeof expected !== 'function') {
365+
if (isRegExp(expected)) {
366+
const str = String(actual);
367+
return RegExpPrototypeExec(expected, str) !== null;
368+
}
369+
throw new ERR_INVALID_ARG_TYPE(
370+
'expected', ['Function', 'RegExp'], expected,
371+
);
372+
}
373+
// Guard instanceof against arrow functions as they don't have a prototype.
374+
if (expected.prototype !== undefined && actual instanceof expected) {
375+
return true;
376+
}
377+
if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
378+
return false;
379+
}
380+
return ReflectApply(expected, {}, [actual]) === true;
381+
}
382+
340383
async #waitForActual(promiseFn) {
341384
let resultPromise;
342385
if (typeof promiseFn === 'function') {

0 commit comments

Comments
 (0)