Skip to content

Commit 0330d82

Browse files
committed
[New] add t.match() and t.doesNotMatch(), new in node v13.6
- nodejs/node#30929
1 parent 21ac403 commit 0330d82

File tree

4 files changed

+219
-1
lines changed

4 files changed

+219
-1
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ end_of_line = lf
77
charset = utf-8
88
trim_trailing_whitespace = true
99
insert_final_newline = true
10-
max_line_length = 140
10+
max_line_length = 180
1111
block_comment_start = /*
1212
block_comment = *
1313
block_comment_end = */

lib/test.js

+37
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ var isRegExp = require('is-regex');
88
var trim = require('string.prototype.trim');
99
var bind = require('function-bind');
1010
var forEach = require('for-each');
11+
var inspect = require('object-inspect');
1112
var isEnumerable = bind.call(Function.call, Object.prototype.propertyIsEnumerable);
1213
var toLowerCase = bind.call(Function.call, String.prototype.toLowerCase);
14+
var $test = bind.call(Function.call, RegExp.prototype.test);
1315

1416
module.exports = Test;
1517

@@ -545,6 +547,41 @@ Test.prototype.doesNotThrow = function (fn, expected, msg, extra) {
545547
});
546548
};
547549

550+
Test.prototype.match = function match(string, regexp, msg, extra) {
551+
if (!isRegExp(regexp)) {
552+
throw new TypeError('The "regexp" argument must be an instance of RegExp. Received type ' + typeof regexp + ' (' + inspect(regexp) + ')');
553+
}
554+
if (typeof string !== 'string') {
555+
throw new TypeError('The "string" argument must be of type string. Received type ' + typeof string + ' (' + inspect(string) + ')');
556+
}
557+
558+
var matches = $test(regexp, string);
559+
this._assert(matches, {
560+
message: defined(msg, 'The input did not match the regular expression ' + inspect(regexp) + '. Input: ' + inspect(string)),
561+
operator: 'match',
562+
actual: string,
563+
expected: regexp,
564+
extra: extra
565+
});
566+
};
567+
568+
Test.prototype.doesNotMatch = function doesNotMatch(string, regexp, msg, extra) {
569+
if (!isRegExp(regexp)) {
570+
throw new TypeError('The "regexp" argument must be an instance of RegExp. Received type ' + typeof regexp + ' (' + inspect(regexp) + ')');
571+
}
572+
if (typeof string !== 'string') {
573+
throw new TypeError('The "string" argument must be of type string. Received type ' + typeof string + ' (' + inspect(string) + ')');
574+
}
575+
var matches = $test(regexp, string);
576+
this._assert(!matches, {
577+
message: defined(msg, 'The input was expected to not match the regular expression ' + inspect(regexp) + '. Input: ' + inspect(string)),
578+
operator: 'doesNotMatch',
579+
actual: string,
580+
expected: regexp,
581+
extra: extra
582+
});
583+
};
584+
548585
Test.skip = function (name_, _opts, _cb) {
549586
var args = getTestArgs.apply(null, arguments);
550587
args.opts.skip = true;

readme.markdown

+8
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,14 @@ You may pass the same options that [`test()`](#testname-opts-cb) accepts.
285285

286286
Print a message without breaking the tap output. (Useful when using e.g. `tap-colorize` where output is buffered & `console.log` will print in incorrect order vis-a-vis tap output.)
287287

288+
## t.match(string, regexp, message)
289+
290+
Assert that `string` matches the RegExp `regexp`. Will throw (not just fail) when the first two arguments are the wrong type.
291+
292+
## t.doesNotMatch(string, regexp, message)
293+
294+
Assert that `string` does not match the RegExp `regexp`. Will throw (not just fail) when the first two arguments are the wrong type.
295+
288296
## var htest = test.createHarness()
289297

290298
Create a new test harness instance, which is a function like `test()`, but with

test/match.js

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
'use strict';
2+
3+
var tape = require('../');
4+
var tap = require('tap');
5+
var concat = require('concat-stream');
6+
7+
var stripFullStack = require('./common').stripFullStack;
8+
9+
tap.test('match', function (tt) {
10+
tt.plan(1);
11+
12+
var test = tape.createHarness({ exit: false });
13+
var tc = function (rows) {
14+
tt.same(stripFullStack(rows.toString('utf8')), [
15+
'TAP version 13',
16+
'# match',
17+
'ok 1 regex arg must be a regex',
18+
'ok 2 string arg must be a string',
19+
'not ok 3 The input did not match the regular expression /abc/. Input: \'string\'',
20+
' ---',
21+
' operator: match',
22+
' expected: /abc/',
23+
' actual: \'string\'',
24+
' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
25+
' stack: |-',
26+
' Error: The input did not match the regular expression /abc/. Input: \'string\'',
27+
' [... stack stripped ...]',
28+
' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
29+
' [... stack stripped ...]',
30+
' ...',
31+
'not ok 4 "string" does not match /abc/',
32+
' ---',
33+
' operator: match',
34+
' expected: /abc/',
35+
' actual: \'string\'',
36+
' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
37+
' stack: |-',
38+
' Error: "string" does not match /abc/',
39+
' [... stack stripped ...]',
40+
' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
41+
' [... stack stripped ...]',
42+
' ...',
43+
'ok 5 The input did not match the regular expression /pass$/. Input: \'I will pass\'',
44+
'ok 6 "I will pass" matches /pass$/',
45+
'',
46+
'1..6',
47+
'# tests 6',
48+
'# pass 4',
49+
'# fail 2',
50+
''
51+
].join('\n'));
52+
};
53+
54+
test.createStream().pipe(concat(tc));
55+
56+
test('match', function (t) {
57+
t.plan(6);
58+
59+
t.throws(
60+
function () { t.match(/abc/, 'string'); },
61+
TypeError,
62+
'regex arg must be a regex'
63+
);
64+
65+
t.throws(
66+
function () { t.match({ abc: 123 }, /abc/); },
67+
TypeError,
68+
'string arg must be a string'
69+
);
70+
71+
t.match('string', /abc/);
72+
t.match('string', /abc/, '"string" does not match /abc/');
73+
74+
t.match('I will pass', /pass$/);
75+
t.match('I will pass', /pass$/, '"I will pass" matches /pass$/');
76+
77+
t.end();
78+
});
79+
});
80+
81+
tap.test('doesNotMatch', function (tt) {
82+
tt.plan(1);
83+
84+
var test = tape.createHarness({ exit: false });
85+
var tc = function (rows) {
86+
tt.same(stripFullStack(rows.toString('utf8')), [
87+
'TAP version 13',
88+
'# doesNotMatch',
89+
'ok 1 regex arg must be a regex',
90+
'ok 2 string arg must be a string',
91+
'not ok 3 The input was expected to not match the regular expression /string/. Input: \'string\'',
92+
' ---',
93+
' operator: doesNotMatch',
94+
' expected: /string/',
95+
' actual: \'string\'',
96+
' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
97+
' stack: |-',
98+
' Error: The input was expected to not match the regular expression /string/. Input: \'string\'',
99+
' [... stack stripped ...]',
100+
' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
101+
' [... stack stripped ...]',
102+
' ...',
103+
'not ok 4 "string" should not match /string/',
104+
' ---',
105+
' operator: doesNotMatch',
106+
' expected: /string/',
107+
' actual: \'string\'',
108+
' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
109+
' stack: |-',
110+
' Error: "string" should not match /string/',
111+
' [... stack stripped ...]',
112+
' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
113+
' [... stack stripped ...]',
114+
' ...',
115+
'not ok 5 The input was expected to not match the regular expression /pass$/. Input: \'I will pass\'',
116+
' ---',
117+
' operator: doesNotMatch',
118+
' expected: /pass$/',
119+
' actual: \'I will pass\'',
120+
' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
121+
' stack: |-',
122+
' Error: The input was expected to not match the regular expression /pass$/. Input: \'I will pass\'',
123+
' [... stack stripped ...]',
124+
' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
125+
' [... stack stripped ...]',
126+
' ...',
127+
'not ok 6 "I will pass" should not match /pass$/',
128+
' ---',
129+
' operator: doesNotMatch',
130+
' expected: /pass$/',
131+
' actual: \'I will pass\'',
132+
' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
133+
' stack: |-',
134+
' Error: "I will pass" should not match /pass$/',
135+
' [... stack stripped ...]',
136+
' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
137+
' [... stack stripped ...]',
138+
' ...',
139+
'',
140+
'1..6',
141+
'# tests 6',
142+
'# pass 2',
143+
'# fail 4',
144+
''
145+
].join('\n'));
146+
};
147+
148+
test.createStream().pipe(concat(tc));
149+
150+
test('doesNotMatch', function (t) {
151+
t.plan(6);
152+
153+
t.throws(
154+
function () { t.doesNotMatch(/abc/, 'string'); },
155+
TypeError,
156+
'regex arg must be a regex'
157+
);
158+
159+
t.throws(
160+
function () { t.doesNotMatch({ abc: 123 }, /abc/); },
161+
TypeError,
162+
'string arg must be a string'
163+
);
164+
165+
t.doesNotMatch('string', /string/);
166+
t.doesNotMatch('string', /string/, '"string" should not match /string/');
167+
168+
t.doesNotMatch('I will pass', /pass$/);
169+
t.doesNotMatch('I will pass', /pass$/, '"I will pass" should not match /pass$/');
170+
171+
t.end();
172+
});
173+
});

0 commit comments

Comments
 (0)