Skip to content

Commit f062981

Browse files
novemberbornsindresorhus
authored andcommitted
Typecheck arguments to t.regex() and t.notRegex()
1 parent 0510d80 commit f062981

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

lib/assert.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,21 @@ function wrapAssertions(callbacks) {
300300
},
301301

302302
regex(string, regex, message) {
303+
if (typeof string !== 'string') {
304+
throw new AssertionError({
305+
assertion: 'regex',
306+
message: '`t.regex()` must be called with a string',
307+
values: [formatAssertError.formatWithLabel('Called with:', string)]
308+
});
309+
}
310+
if (!(regex instanceof RegExp)) {
311+
throw new AssertionError({
312+
assertion: 'regex',
313+
message: '`t.regex()` must be called with a regular expression',
314+
values: [formatAssertError.formatWithLabel('Called with:', regex)]
315+
});
316+
}
317+
303318
if (!regex.test(string)) {
304319
throw new AssertionError({
305320
assertion: 'regex',
@@ -313,6 +328,21 @@ function wrapAssertions(callbacks) {
313328
},
314329

315330
notRegex(string, regex, message) {
331+
if (typeof string !== 'string') {
332+
throw new AssertionError({
333+
assertion: 'notRegex',
334+
message: '`t.notRegex()` must be called with a string',
335+
values: [formatAssertError.formatWithLabel('Called with:', string)]
336+
});
337+
}
338+
if (!(regex instanceof RegExp)) {
339+
throw new AssertionError({
340+
assertion: 'notRegex',
341+
message: '`t.notRegex()` must be called with a regular expression',
342+
values: [formatAssertError.formatWithLabel('Called with:', regex)]
343+
});
344+
}
345+
316346
if (regex.test(string)) {
317347
throw new AssertionError({
318348
assertion: 'notRegex',

test/assert.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,26 @@ test('.regex()', t => {
808808
t.end();
809809
});
810810

811+
test('.regex() fails if passed a bad value', t => {
812+
failsWith(t, () => {
813+
assertions.regex(42, /foo/);
814+
}, {
815+
assertion: 'regex',
816+
message: '`t.regex()` must be called with a string',
817+
values: [{label: 'Called with:', formatted: /42/}]
818+
});
819+
820+
failsWith(t, () => {
821+
assertions.regex('42', {});
822+
}, {
823+
assertion: 'regex',
824+
message: '`t.regex()` must be called with a regular expression',
825+
values: [{label: 'Called with:', formatted: /Object/}]
826+
});
827+
828+
t.end();
829+
});
830+
811831
test('.notRegex()', t => {
812832
passes(t, () => {
813833
assertions.notRegex('abc', /def/);
@@ -837,3 +857,23 @@ test('.notRegex()', t => {
837857

838858
t.end();
839859
});
860+
861+
test('.notRegex() fails if passed a bad value', t => {
862+
failsWith(t, () => {
863+
assertions.notRegex(42, /foo/);
864+
}, {
865+
assertion: 'notRegex',
866+
message: '`t.notRegex()` must be called with a string',
867+
values: [{label: 'Called with:', formatted: /42/}]
868+
});
869+
870+
failsWith(t, () => {
871+
assertions.notRegex('42', {});
872+
}, {
873+
assertion: 'notRegex',
874+
message: '`t.notRegex()` must be called with a regular expression',
875+
values: [{label: 'Called with:', formatted: /Object/}]
876+
});
877+
878+
t.end();
879+
});

0 commit comments

Comments
 (0)