Skip to content

Commit

Permalink
assert: do not repeat .throws() code
Browse files Browse the repository at this point in the history
This refactors some code for less duplication.

PR-URL: #28263
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR committed Oct 1, 2019
1 parent d47b678 commit 5700cd1
Showing 1 changed file with 40 additions and 57 deletions.
97 changes: 40 additions & 57 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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 ' +
Expand All @@ -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,
Expand Down

0 comments on commit 5700cd1

Please sign in to comment.