Skip to content

Commit 8bd0390

Browse files
committed
Implement assert.match() and assert.doesNotMatch()
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
1 parent bba838e commit 8bd0390

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

assert.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,51 @@ assert.ifError = function ifError(err) {
596596
}
597597
};
598598

599+
// Currently in sync with Node.js lib/assert.js
600+
// https://github.com/nodejs/node/commit/2a871df3dfb8ea663ef5e1f8f62701ec51384ecb
601+
function internalMatch(string, regexp, message, fn) {
602+
if (!isRegExp(regexp)) {
603+
throw new ERR_INVALID_ARG_TYPE(
604+
'regexp', 'RegExp', regexp
605+
);
606+
}
607+
const match = fn.name === 'match';
608+
if (typeof string !== 'string' ||
609+
regexp.test(string) !== match) {
610+
if (message instanceof Error) {
611+
throw message;
612+
}
613+
614+
const generatedMessage = !message;
615+
616+
// 'The input was expected to not match the regular expression ' +
617+
message = message || (typeof string !== 'string' ?
618+
'The "string" argument must be of type string. Received type ' +
619+
`${typeof string} (${inspect(string)})` :
620+
(match ?
621+
'The input did not match the regular expression ' :
622+
'The input was expected to not match the regular expression ') +
623+
`${inspect(regexp)}. Input:\n\n${inspect(string)}\n`);
624+
const err = new AssertionError({
625+
actual: string,
626+
expected: regexp,
627+
message,
628+
operator: fn.name,
629+
stackStartFn: fn
630+
});
631+
err.generatedMessage = generatedMessage;
632+
throw err;
633+
}
634+
}
635+
636+
assert.match = function match(string, regexp, message) {
637+
internalMatch(string, regexp, message, match);
638+
};
639+
640+
assert.doesNotMatch = function doesNotMatch(string, regexp, message) {
641+
internalMatch(string, regexp, message, doesNotMatch);
642+
};
643+
599644
// Expose a strict only variant of assert
600645
function strict(...args) {
601646
innerOk(strict, args.length, ...args);

test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const testPaths = [
1313
['test-assert-fail-deprecation.js', () => require('./test/parallel/test-assert-fail-deprecation.js')],
1414
['test-assert-fail.js', () => require('./test/parallel/test-assert-fail.js')],
1515
['test-assert-if-error.js', () => require('./test/parallel/test-assert-if-error.js')],
16+
['test-assert-match.js', () => require('./test/parallel/test-assert-match.js')],
1617
['test-assert-typedarray-deepequal.js', () => require('./test/parallel/test-assert-typedarray-deepequal.js')],
1718
['test-assert.js', () => require('./test/parallel/test-assert.js')],
1819
['test-assert-colors.js', () => require('./test/pseudo-tty/test-assert-colors.js')],

test/parallel/test-assert-match.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Currently in sync with Node.js test/parallel/test-assert.js
2+
// https://github.com/nodejs/node/commit/2a871df3dfb8ea663ef5e1f8f62701ec51384ecb
3+
4+
'use strict';
5+
6+
require('../common');
7+
const assert = require('assert');
8+
9+
// Multiple assert.match() tests.
10+
{
11+
assert.throws(
12+
() => assert.match(/abc/, 'string'),
13+
{
14+
code: 'ERR_INVALID_ARG_TYPE',
15+
message: 'The "regexp" argument must be an instance of RegExp. ' +
16+
"Received type string ('string')"
17+
}
18+
);
19+
assert.throws(
20+
() => assert.match('string', /abc/),
21+
{
22+
actual: 'string',
23+
expected: /abc/,
24+
operator: 'match',
25+
message: 'The input did not match the regular expression /abc/. ' +
26+
"Input:\n\n'string'\n",
27+
generatedMessage: true
28+
}
29+
);
30+
assert.throws(
31+
() => assert.match('string', /abc/, 'foobar'),
32+
{
33+
actual: 'string',
34+
expected: /abc/,
35+
operator: 'match',
36+
message: 'foobar',
37+
generatedMessage: false
38+
}
39+
);
40+
const errorMessage = new RangeError('foobar');
41+
assert.throws(
42+
() => assert.match('string', /abc/, errorMessage),
43+
errorMessage
44+
);
45+
assert.throws(
46+
() => assert.match({ abc: 123 }, /abc/),
47+
{
48+
actual: { abc: 123 },
49+
expected: /abc/,
50+
operator: 'match',
51+
message: 'The "string" argument must be of type string. ' +
52+
'Received type object ({ abc: 123 })',
53+
generatedMessage: true
54+
}
55+
);
56+
assert.match('I will pass', /pass$/);
57+
}
58+
59+
// Multiple assert.doesNotMatch() tests.
60+
{
61+
assert.throws(
62+
() => assert.doesNotMatch(/abc/, 'string'),
63+
{
64+
code: 'ERR_INVALID_ARG_TYPE',
65+
message: 'The "regexp" argument must be an instance of RegExp. ' +
66+
"Received type string ('string')"
67+
}
68+
);
69+
assert.throws(
70+
() => assert.doesNotMatch('string', /string/),
71+
{
72+
actual: 'string',
73+
expected: /string/,
74+
operator: 'doesNotMatch',
75+
message: 'The input was expected to not match the regular expression ' +
76+
"/string/. Input:\n\n'string'\n",
77+
generatedMessage: true
78+
}
79+
);
80+
assert.throws(
81+
() => assert.doesNotMatch('string', /string/, 'foobar'),
82+
{
83+
actual: 'string',
84+
expected: /string/,
85+
operator: 'doesNotMatch',
86+
message: 'foobar',
87+
generatedMessage: false
88+
}
89+
);
90+
const errorMessage = new RangeError('foobar');
91+
assert.throws(
92+
() => assert.doesNotMatch('string', /string/, errorMessage),
93+
errorMessage
94+
);
95+
assert.throws(
96+
() => assert.doesNotMatch({ abc: 123 }, /abc/),
97+
{
98+
actual: { abc: 123 },
99+
expected: /abc/,
100+
operator: 'doesNotMatch',
101+
message: 'The "string" argument must be of type string. ' +
102+
'Received type object ({ abc: 123 })',
103+
generatedMessage: true
104+
}
105+
);
106+
assert.doesNotMatch('I will pass', /different$/);
107+
}

0 commit comments

Comments
 (0)