Skip to content

Commit 4a02fd2

Browse files
committed
assert: allow arbitrary argument order in match() and doesNotMatch()
This allows assert.match() and assert.doesNotMatch() arguments to be inserted in any order. Signed-off-by: Ruben Bridgewater <[email protected]>
1 parent d9de812 commit 4a02fd2

File tree

3 files changed

+85
-8
lines changed

3 files changed

+85
-8
lines changed

doc/api/assert.md

+11
Original file line numberDiff line numberDiff line change
@@ -890,11 +890,16 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the
890890

891891
## `assert.doesNotMatch(string, regexp[, message])`
892892

893+
## `assert.doesNotMatch(regexp, string[, message])`
894+
893895
<!-- YAML
894896
added:
895897
- v13.6.0
896898
- v12.16.0
897899
changes:
900+
- version: REPLACEME
901+
pr-url: https://github.com/nodejs/node/pull/41007
902+
description: The argument order may now also be: 1) regexp 2) string.
898903
- version: v16.0.0
899904
pr-url: https://github.com/nodejs/node/pull/38111
900905
description: This API is no longer experimental.
@@ -1415,10 +1420,16 @@ let err;
14151420

14161421
## `assert.match(string, regexp[, message])`
14171422

1423+
## `assert.match(regexp, string[, message])`
1424+
14181425
<!-- YAML
14191426
added:
14201427
- v13.6.0
14211428
- v12.16.0
1429+
changes:
1430+
- version: REPLACEME
1431+
pr-url: https://github.com/nodejs/node/pull/41007
1432+
description: The argument order may now also be: 1) regexp 2) string.
14221433
changes:
14231434
- version: v16.0.0
14241435
pr-url: https://github.com/nodejs/node/pull/38111

lib/assert.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -989,10 +989,17 @@ assert.ifError = function ifError(err) {
989989

990990
function internalMatch(string, regexp, message, fn) {
991991
if (!isRegExp(regexp)) {
992-
throw new ERR_INVALID_ARG_TYPE(
993-
'regexp', 'RegExp', regexp
994-
);
992+
if (isRegExp(string)) {
993+
const tmp = string;
994+
string = regexp;
995+
regexp = tmp;
996+
} else {
997+
throw new ERR_INVALID_ARG_TYPE(
998+
'regexp', 'RegExp', regexp
999+
);
1000+
}
9951001
}
1002+
9961003
const match = fn.name === 'match';
9971004
if (typeof string !== 'string' ||
9981005
RegExpPrototypeTest(regexp, string) !== match) {
@@ -1002,8 +1009,21 @@ function internalMatch(string, regexp, message, fn) {
10021009

10031010
const generatedMessage = !message;
10041011

1005-
// 'The input was expected to not match the regular expression ' +
1006-
message = message || (typeof string !== 'string' ?
1012+
if (!message) {
1013+
if (typeof string !== 'string') {
1014+
message = 'The "string" argument must be of type string. ' +
1015+
`Received type ${typeof string} (${inspect(string)})`;
1016+
} else {
1017+
if (match) {
1018+
message = 'The input did not match the regular expression';
1019+
} else {
1020+
message = 'The input was expected to not match the regular ' +
1021+
'expression';
1022+
}
1023+
message += ` ${inspect(regexp)}. Input:\n\n${inspect(string)}\n`;
1024+
}
1025+
}
1026+
message ||= (typeof string !== 'string' ?
10071027
'The "string" argument must be of type string. Received type ' +
10081028
`${typeof string} (${inspect(string)})` :
10091029
(match ?

test/parallel/test-assert.js

+49-3
Original file line numberDiff line numberDiff line change
@@ -1372,11 +1372,34 @@ assert.throws(
13721372
// Multiple assert.match() tests.
13731373
{
13741374
assert.throws(
1375-
() => assert.match(/abc/, 'string'),
1375+
() => assert.match(/abc/, 5),
13761376
{
1377+
actual: 5,
1378+
expected: /abc/,
1379+
operator: 'match',
1380+
message: 'The "string" argument must be of type string. ' +
1381+
'Received type number (5)',
1382+
generatedMessage: true
1383+
}
1384+
);
1385+
assert.throws(
1386+
() => assert.match(5, 'string'),
1387+
{
1388+
name: 'TypeError',
13771389
code: 'ERR_INVALID_ARG_TYPE',
13781390
message: 'The "regexp" argument must be an instance of RegExp. ' +
1379-
"Received type string ('string')"
1391+
"Received type string ('string')",
1392+
}
1393+
);
1394+
assert.throws(
1395+
() => assert.match(/abc/, 'string'),
1396+
{
1397+
actual: 'string',
1398+
expected: /abc/,
1399+
operator: 'match',
1400+
message: 'The input did not match the regular expression /abc/. ' +
1401+
"Input:\n\n'string'\n",
1402+
generatedMessage: true
13801403
}
13811404
);
13821405
assert.throws(
@@ -1422,8 +1445,31 @@ assert.throws(
14221445
// Multiple assert.doesNotMatch() tests.
14231446
{
14241447
assert.throws(
1425-
() => assert.doesNotMatch(/abc/, 'string'),
1448+
() => assert.doesNotMatch(/string/, 5),
1449+
{
1450+
actual: 5,
1451+
expected: /string/,
1452+
operator: 'doesNotMatch',
1453+
message: 'The "string" argument must be of type string. ' +
1454+
'Received type number (5)',
1455+
generatedMessage: true
1456+
}
1457+
);
1458+
assert.throws(
1459+
() => assert.doesNotMatch(/string/, 'string'),
1460+
{
1461+
actual: 'string',
1462+
expected: /string/,
1463+
operator: 'doesNotMatch',
1464+
message: 'The input was expected to not match the regular expression ' +
1465+
"/string/. Input:\n\n'string'\n",
1466+
generatedMessage: true
1467+
}
1468+
);
1469+
assert.throws(
1470+
() => assert.doesNotMatch(5, 'string'),
14261471
{
1472+
name: 'TypeError',
14271473
code: 'ERR_INVALID_ARG_TYPE',
14281474
message: 'The "regexp" argument must be an instance of RegExp. ' +
14291475
"Received type string ('string')"

0 commit comments

Comments
 (0)