From c7402023d6a3a6b7a03abf2d52d948d0964683c8 Mon Sep 17 00:00:00 2001 From: James Talmage Date: Thu, 26 May 2016 02:24:50 -0400 Subject: [PATCH] add t.notRegex (#879) * add t.notRegex Fixes #877 * add notRegex to power-assert patterns --- lib/assert.js | 4 ++++ lib/enhance-assert.js | 1 + readme.md | 4 ++++ test/assert.js | 12 ++++++++++++ 4 files changed, 21 insertions(+) diff --git a/lib/assert.js b/lib/assert.js index 52b5053fb..19b7b86aa 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -142,6 +142,10 @@ x.regex = function (contents, regex, msg) { test(regex.test(contents), create(regex, contents, '===', msg, x.regex)); }; +x.notRegex = function (contents, regex, msg) { + test(!regex.test(contents), create(regex, contents, '!==', msg, x.notRegex)); +}; + x.ifError = x.error = function (err, msg) { test(!err, create(err, 'Error', '!==', msg, x.ifError)); }; diff --git a/lib/enhance-assert.js b/lib/enhance-assert.js index 9bdc7989f..54210aeff 100644 --- a/lib/enhance-assert.js +++ b/lib/enhance-assert.js @@ -13,6 +13,7 @@ module.exports.PATTERNS = [ 't.deepEqual(value, expected, [message])', 't.notDeepEqual(value, expected, [message])', 't.regex(contents, regex, [message])', + 't.notRegex(contents, regex, [message])', // deprecated apis 't.ok(value, [message])', 't.notOk(value, [message])', diff --git a/readme.md b/readme.md index 39f0d938d..ecea20259 100644 --- a/readme.md +++ b/readme.md @@ -878,6 +878,10 @@ Assert that `function` doesn't throw an `error` or `promise` resolves. Assert that `contents` matches `regex`. +### `.notRegex(contents, regex, [message])` + +Assert that `contents` does not match `regex`. + ### `.ifError(error, [message])` Assert that `error` is falsy. diff --git a/test/assert.js b/test/assert.js index f573dbf75..574964ed4 100644 --- a/test/assert.js +++ b/test/assert.js @@ -345,6 +345,18 @@ test('.regex()', function (t) { t.end(); }); +test('.notRegex()', function (t) { + t.doesNotThrow(function () { + assert.notRegex('abc', /def/); + }); + + t.throws(function () { + assert.notRegex('abc', /abc/); + }); + + t.end(); +}); + test('.ifError()', function (t) { t.throws(function () { assert.ifError(new Error());