Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: return boolean result of assert.match method #35115

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ assert.ifError = function ifError(err) {
}
};

function internalMatch(string, regexp, message, fn) {
function internalMatch(string, regexp, message, fn, shouldThrow = true) {
if (!isRegExp(regexp)) {
throw new ERR_INVALID_ARG_TYPE(
'regexp', 'RegExp', regexp
Expand All @@ -896,9 +896,11 @@ function internalMatch(string, regexp, message, fn) {
if (typeof string !== 'string' ||
RegExpPrototypeTest(regexp, string) !== match) {
if (message instanceof Error) {
if (!shouldThrow) return false;
throw message;
}

if (!shouldThrow) return false;
const generatedMessage = !message;

// 'The input was expected to not match the regular expression ' +
Expand All @@ -919,10 +921,14 @@ function internalMatch(string, regexp, message, fn) {
err.generatedMessage = generatedMessage;
throw err;
}
if (!shouldThrow) return true;
}

assert.match = function match(string, regexp, message) {
internalMatch(string, regexp, message, match);
assert.match = function match(string, regexp, message, shouldThrow) {
// Undefined shouldThrow and message as boolean
// means message is shouldThrow.
if (!shouldThrow && typeof message === 'boolean') shouldThrow = message;
return internalMatch(string, regexp, message, match, shouldThrow);
};

assert.doesNotMatch = function doesNotMatch(string, regexp, message) {
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,12 @@ assert.throws(
assert.match('I will pass', /pass$/);
}

// Use the assert.match boolean result.
{
assert(assert.match('meow', /meow$/, false));
assert(!assert.match('meow', /woof$/, false));
}

// Multiple assert.doesNotMatch() tests.
{
assert.throws(
Expand Down