Skip to content

Commit

Permalink
Typecheck arguments to t.regex() and t.notRegex()
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn authored and sindresorhus committed Mar 17, 2017
1 parent 0510d80 commit f062981
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,21 @@ function wrapAssertions(callbacks) {
},

regex(string, regex, message) {
if (typeof string !== 'string') {
throw new AssertionError({
assertion: 'regex',
message: '`t.regex()` must be called with a string',
values: [formatAssertError.formatWithLabel('Called with:', string)]
});
}
if (!(regex instanceof RegExp)) {
throw new AssertionError({
assertion: 'regex',
message: '`t.regex()` must be called with a regular expression',
values: [formatAssertError.formatWithLabel('Called with:', regex)]
});
}

if (!regex.test(string)) {
throw new AssertionError({
assertion: 'regex',
Expand All @@ -313,6 +328,21 @@ function wrapAssertions(callbacks) {
},

notRegex(string, regex, message) {
if (typeof string !== 'string') {
throw new AssertionError({
assertion: 'notRegex',
message: '`t.notRegex()` must be called with a string',
values: [formatAssertError.formatWithLabel('Called with:', string)]
});
}
if (!(regex instanceof RegExp)) {
throw new AssertionError({
assertion: 'notRegex',
message: '`t.notRegex()` must be called with a regular expression',
values: [formatAssertError.formatWithLabel('Called with:', regex)]
});
}

if (regex.test(string)) {
throw new AssertionError({
assertion: 'notRegex',
Expand Down
40 changes: 40 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,26 @@ test('.regex()', t => {
t.end();
});

test('.regex() fails if passed a bad value', t => {
failsWith(t, () => {
assertions.regex(42, /foo/);
}, {
assertion: 'regex',
message: '`t.regex()` must be called with a string',
values: [{label: 'Called with:', formatted: /42/}]
});

failsWith(t, () => {
assertions.regex('42', {});
}, {
assertion: 'regex',
message: '`t.regex()` must be called with a regular expression',
values: [{label: 'Called with:', formatted: /Object/}]
});

t.end();
});

test('.notRegex()', t => {
passes(t, () => {
assertions.notRegex('abc', /def/);
Expand Down Expand Up @@ -837,3 +857,23 @@ test('.notRegex()', t => {

t.end();
});

test('.notRegex() fails if passed a bad value', t => {
failsWith(t, () => {
assertions.notRegex(42, /foo/);
}, {
assertion: 'notRegex',
message: '`t.notRegex()` must be called with a string',
values: [{label: 'Called with:', formatted: /42/}]
});

failsWith(t, () => {
assertions.notRegex('42', {});
}, {
assertion: 'notRegex',
message: '`t.notRegex()` must be called with a regular expression',
values: [{label: 'Called with:', formatted: /Object/}]
});

t.end();
});

0 comments on commit f062981

Please sign in to comment.