Skip to content

Commit

Permalink
assert: DRY .throws code
Browse files Browse the repository at this point in the history
This refactors some code for less duplication.

Backport-PR-URL: #31431
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 authored and MylesBorins committed Jan 30, 2020
1 parent e938784 commit 881ebce
Showing 1 changed file with 45 additions and 43 deletions.
88 changes: 45 additions & 43 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
ObjectAssign,
ObjectIs,
ObjectKeys,
ObjectPrototypeIsPrototypeOf,
Map,
} = primordials;

Expand Down Expand Up @@ -571,6 +572,9 @@ 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.
if (isRegExp(expected)) {
Expand All @@ -583,20 +587,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 @@ -606,43 +599,52 @@ function expectedException(actual, expected, message, fn) {
});
err.operator = fn.name;
throw err;
}

// Handle validation objects.
const keys = ObjectKeys(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 = ObjectKeys(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;
}
compareExceptionKey(actual, expected, key, message, keys, fn);
}
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 (Error.isPrototypeOf(expected)) {
} else if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
throw actual;
} else {
// Check validation functions return value.
const res = expected.call({}, actual);
if (res !== true) {
throw actual;
}
}

// Check validation functions return value.
const res = expected.call({}, actual);
if (res !== true) {
throw actual;
if (throwError) {
const err = new AssertionError({
actual,
expected,
message,
operator: fn.name,
stackStartFn: fn
});
err.generatedMessage = generatedMessage;
throw err;
}
}

Expand Down

0 comments on commit 881ebce

Please sign in to comment.