assert: do not repeat .throws() code
This refactors some code for less duplication. PR-URL: https://github.com/nodejs/node/pull/28263 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
d47b6786c9
commit
5700cd17dd
@ -563,6 +563,7 @@ function compareExceptionKey(actual, expected, key, message, keys, fn) {
|
||||
|
||||
function expectedException(actual, expected, message, fn) {
|
||||
let generatedMessage = false;
|
||||
let throwError = false;
|
||||
|
||||
if (typeof expected !== 'function') {
|
||||
// Handle regular expressions.
|
||||
@ -576,20 +577,9 @@ function expectedException(actual, expected, message, fn) {
|
||||
message = 'The input did not match the regular expression ' +
|
||||
`${inspect(expected)}. Input:\n\n${inspect(str)}\n`;
|
||||
}
|
||||
|
||||
const err = new AssertionError({
|
||||
actual,
|
||||
expected,
|
||||
message,
|
||||
operator: fn.name,
|
||||
stackStartFn: fn
|
||||
});
|
||||
err.generatedMessage = generatedMessage;
|
||||
throw err;
|
||||
}
|
||||
|
||||
// Handle primitives properly.
|
||||
if (typeof actual !== 'object' || actual === null) {
|
||||
throwError = true;
|
||||
// Handle primitives properly.
|
||||
} else if (typeof actual !== 'object' || actual === null) {
|
||||
const err = new AssertionError({
|
||||
actual,
|
||||
expected,
|
||||
@ -599,36 +589,33 @@ function expectedException(actual, expected, message, fn) {
|
||||
});
|
||||
err.operator = fn.name;
|
||||
throw err;
|
||||
}
|
||||
|
||||
// Handle validation objects.
|
||||
const keys = Object.keys(expected);
|
||||
// Special handle errors to make sure the name and the message are compared
|
||||
// as well.
|
||||
if (expected instanceof Error) {
|
||||
keys.push('name', 'message');
|
||||
} else if (keys.length === 0) {
|
||||
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]) &&
|
||||
expected[key].test(actual[key])) {
|
||||
continue;
|
||||
} else {
|
||||
// Handle validation objects.
|
||||
const keys = Object.keys(expected);
|
||||
// Special handle errors to make sure the name and the message are
|
||||
// compared as well.
|
||||
if (expected instanceof Error) {
|
||||
keys.push('name', 'message');
|
||||
} else if (keys.length === 0) {
|
||||
throw new ERR_INVALID_ARG_VALUE('error',
|
||||
expected, 'may not be an empty object');
|
||||
}
|
||||
compareExceptionKey(actual, expected, key, message, keys, fn);
|
||||
if (isDeepEqual === undefined) lazyLoadComparison();
|
||||
for (const key of keys) {
|
||||
if (typeof actual[key] === 'string' &&
|
||||
isRegExp(expected[key]) &&
|
||||
expected[key].test(actual[key])) {
|
||||
continue;
|
||||
}
|
||||
compareExceptionKey(actual, expected, key, message, keys, fn);
|
||||
}
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Guard instanceof against arrow functions as they don't have a prototype.
|
||||
// Check for matching Error classes.
|
||||
if (expected.prototype !== undefined && actual instanceof expected) {
|
||||
} else if (expected.prototype !== undefined && actual instanceof expected) {
|
||||
return;
|
||||
}
|
||||
if (ObjectPrototype.isPrototypeOf(Error, expected)) {
|
||||
} else if (ObjectPrototype.isPrototypeOf(Error, expected)) {
|
||||
if (!message) {
|
||||
generatedMessage = true;
|
||||
message = 'The error is expected to be an instance of ' +
|
||||
@ -639,26 +626,22 @@ function expectedException(actual, expected, message, fn) {
|
||||
message += `"${inspect(actual, { depth: -1 })}"`;
|
||||
}
|
||||
}
|
||||
const err = new AssertionError({
|
||||
actual,
|
||||
expected,
|
||||
message,
|
||||
operator: fn.name,
|
||||
stackStartFn: fn
|
||||
});
|
||||
err.generatedMessage = generatedMessage;
|
||||
throw err;
|
||||
throwError = true;
|
||||
} else {
|
||||
// Check validation functions return value.
|
||||
const res = expected.call({}, actual);
|
||||
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)}`;
|
||||
}
|
||||
throwError = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Check validation functions return value.
|
||||
const res = expected.call({}, actual);
|
||||
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)}`;
|
||||
}
|
||||
if (throwError) {
|
||||
const err = new AssertionError({
|
||||
actual,
|
||||
expected,
|
||||
|
Loading…
x
Reference in New Issue
Block a user