Skip to content

Commit 8a38000

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

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

assert.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,52 @@ 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, fnName) {
602+
if (!isRegExp(regexp)) {
603+
throw new ERR_INVALID_ARG_TYPE(
604+
'regexp', 'RegExp', regexp
605+
);
606+
}
607+
const match = fnName === 'match';
608+
const test = require('call-bind/callBound')('RegExp.prototype.test');
609+
if (typeof string !== 'string' ||
610+
test(regexp, string) !== match) {
611+
if (message instanceof Error) {
612+
throw message;
613+
}
614+
615+
const generatedMessage = !message;
616+
617+
// 'The input was expected to not match the regular expression ' +
618+
message = message || (typeof string !== 'string' ?
619+
'The "string" argument must be of type string. Received type ' +
620+
`${typeof string} (${inspect(string)})` :
621+
(match ?
622+
'The input did not match the regular expression ' :
623+
'The input was expected to not match the regular expression ') +
624+
`${inspect(regexp)}. Input:\n\n${inspect(string)}\n`);
625+
const err = new AssertionError({
626+
actual: string,
627+
expected: regexp,
628+
message,
629+
operator: fnName,
630+
stackStartFn: fn
631+
});
632+
err.generatedMessage = generatedMessage;
633+
throw err;
634+
}
635+
}
636+
637+
assert.match = function match(string, regexp, message) {
638+
internalMatch(string, regexp, message, match, 'match');
639+
};
640+
641+
assert.doesNotMatch = function doesNotMatch(string, regexp, message) {
642+
internalMatch(string, regexp, message, doesNotMatch, 'doesNotMatch');
643+
};
644+
599645
// Expose a strict only variant of assert
600646
function strict(...args) {
601647
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)