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) {
|
function expectedException(actual, expected, message, fn) {
|
||||||
let generatedMessage = false;
|
let generatedMessage = false;
|
||||||
|
let throwError = false;
|
||||||
|
|
||||||
if (typeof expected !== 'function') {
|
if (typeof expected !== 'function') {
|
||||||
// Handle regular expressions.
|
// Handle regular expressions.
|
||||||
@ -576,20 +577,9 @@ function expectedException(actual, expected, message, fn) {
|
|||||||
message = 'The input did not match the regular expression ' +
|
message = 'The input did not match the regular expression ' +
|
||||||
`${inspect(expected)}. Input:\n\n${inspect(str)}\n`;
|
`${inspect(expected)}. Input:\n\n${inspect(str)}\n`;
|
||||||
}
|
}
|
||||||
|
throwError = true;
|
||||||
const err = new AssertionError({
|
// Handle primitives properly.
|
||||||
actual,
|
} else if (typeof actual !== 'object' || actual === null) {
|
||||||
expected,
|
|
||||||
message,
|
|
||||||
operator: fn.name,
|
|
||||||
stackStartFn: fn
|
|
||||||
});
|
|
||||||
err.generatedMessage = generatedMessage;
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle primitives properly.
|
|
||||||
if (typeof actual !== 'object' || actual === null) {
|
|
||||||
const err = new AssertionError({
|
const err = new AssertionError({
|
||||||
actual,
|
actual,
|
||||||
expected,
|
expected,
|
||||||
@ -599,36 +589,33 @@ function expectedException(actual, expected, message, fn) {
|
|||||||
});
|
});
|
||||||
err.operator = fn.name;
|
err.operator = fn.name;
|
||||||
throw err;
|
throw err;
|
||||||
}
|
} else {
|
||||||
|
// Handle validation objects.
|
||||||
// Handle validation objects.
|
const keys = Object.keys(expected);
|
||||||
const keys = Object.keys(expected);
|
// Special handle errors to make sure the name and the message are
|
||||||
// Special handle errors to make sure the name and the message are compared
|
// compared as well.
|
||||||
// as well.
|
if (expected instanceof Error) {
|
||||||
if (expected instanceof Error) {
|
keys.push('name', 'message');
|
||||||
keys.push('name', 'message');
|
} else if (keys.length === 0) {
|
||||||
} else if (keys.length === 0) {
|
throw new ERR_INVALID_ARG_VALUE('error',
|
||||||
throw new ERR_INVALID_ARG_VALUE('error',
|
expected, 'may not be an empty object');
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
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.
|
// Guard instanceof against arrow functions as they don't have a prototype.
|
||||||
// Check for matching Error classes.
|
// Check for matching Error classes.
|
||||||
if (expected.prototype !== undefined && actual instanceof expected) {
|
} else if (expected.prototype !== undefined && actual instanceof expected) {
|
||||||
return;
|
return;
|
||||||
}
|
} else if (ObjectPrototype.isPrototypeOf(Error, expected)) {
|
||||||
if (ObjectPrototype.isPrototypeOf(Error, expected)) {
|
|
||||||
if (!message) {
|
if (!message) {
|
||||||
generatedMessage = true;
|
generatedMessage = true;
|
||||||
message = 'The error is expected to be an instance of ' +
|
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 })}"`;
|
message += `"${inspect(actual, { depth: -1 })}"`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const err = new AssertionError({
|
throwError = true;
|
||||||
actual,
|
} else {
|
||||||
expected,
|
// Check validation functions return value.
|
||||||
message,
|
const res = expected.call({}, actual);
|
||||||
operator: fn.name,
|
if (res !== true) {
|
||||||
stackStartFn: fn
|
if (!message) {
|
||||||
});
|
generatedMessage = true;
|
||||||
err.generatedMessage = generatedMessage;
|
const name = expected.name ? `"${expected.name}" ` : '';
|
||||||
throw err;
|
message = `The ${name}validation function is expected to return` +
|
||||||
|
` "true". Received ${inspect(res)}`;
|
||||||
|
}
|
||||||
|
throwError = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check validation functions return value.
|
if (throwError) {
|
||||||
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)}`;
|
|
||||||
}
|
|
||||||
const err = new AssertionError({
|
const err = new AssertionError({
|
||||||
actual,
|
actual,
|
||||||
expected,
|
expected,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user