From f06298179a21cf74827f14667a97d2bceb798299 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Tue, 14 Mar 2017 16:10:04 +0000 Subject: [PATCH] Typecheck arguments to t.regex() and t.notRegex() --- lib/assert.js | 30 ++++++++++++++++++++++++++++++ test/assert.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/lib/assert.js b/lib/assert.js index 3acecd4..d097e1b 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -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', @@ -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', diff --git a/test/assert.js b/test/assert.js index 79b67d4..b8b832a 100644 --- a/test/assert.js +++ b/test/assert.js @@ -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/); @@ -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(); +});