diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index a1664297..646b68ad 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -2246,6 +2246,49 @@ function assertMatch(re, msg) { Assertion.addMethod('match', assertMatch); Assertion.addMethod('matches', assertMatch); +/** + * ### .deepMatch(re[, msg]) + * + * Asserts that the target matches the given regular expression `re`. + * + * expect('foobar').to.deepMatch(/^foo/); + * + * Add `.not` earlier in the chain to negate `.deepMatch`. + * + * expect('foobar').to.not.deepMatch(/taco/); + * + * `.deepMatch` accepts an optional `msg` argument which is a custom error message + * to show when the assertion fails. The message can also be given as the + * second argument to `expect`. + * + * expect('foobar').to.deepMatch(/taco/, 'nooo why fail??'); + * expect('foobar', 'nooo why fail??').to.deepMatch(/taco/); + * + * The alias `.deepMatches` can be used interchangeably with `.deepMatch`. + * + * @name deepMatch + * @alias deepMatches + * @param {RegExp} re + * @param {string} msg _optional_ + * @namespace BDD + * @public + */ +function assertDeepMatch(re, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object'); + const matches = re.exec(obj); + this.assert( + matches !== null ? matches[0] == obj : null + , 'expected #{this} to match ' + re + , 'expected #{this} not to match ' + re + ); +} + +Assertion.addMethod('deepMatch', assertDeepMatch); +Assertion.addMethod('deepMatches', assertDeepMatch); + + + /** * ### .string(str[, msg]) * diff --git a/lib/chai/interface/assert.js b/lib/chai/interface/assert.js index c31784f0..5b7a42e4 100644 --- a/lib/chai/interface/assert.js +++ b/lib/chai/interface/assert.js @@ -1152,6 +1152,44 @@ assert.notDeepOwnInclude = function(exp, inc, msg) { .not.deep.own.include(inc); }; + + +/** + * ### .deepMatch(value, regexp, [message]) + * + * Asserts that `value` completely matches the regular expression `regexp`. + * + * assert.deepMatch('foobar', /^foo/, 'regexp matches'); + * + * @name deepMatch + * @param {unknown} exp + * @param {RegExp} re + * @param {string} msg + * @namespace Assert + * @public + */ +assert.deepMatch = function (exp, re, msg) { + new Assertion(exp, msg, assert.deepMatch, true).to.deepMatch(re); +}; + +/** + * ### .notDeepMatch(value, regexp, [message]) + * + * Asserts that `value` does not deep match the regular expression `regexp`. + * + * assert.notDeepMatch('foobar', /^foo/, 'regexp does not match'); + * + * @name notDeepMatch + * @param {unknown} exp + * @param {RegExp} re + * @param {string} msg + * @namespace Assert + * @public + */ +assert.notDeepMatch = function (exp, re, msg) { + new Assertion(exp, msg, assert.notDeepMatch, true).to.not.deepMatch(re); +}; + /** * ### .match(value, regexp, [message]) * diff --git a/test/assert.js b/test/assert.js index c10a6775..69b5d71b 100644 --- a/test/assert.js +++ b/test/assert.js @@ -1450,6 +1450,21 @@ describe('assert', function () { }, "blah: expected 'foobar' not to match /^foo/i"); }); + it("deepMatch", function () { + assert.deepMatch('foo', /^foo/); + assert.notDeepMatch('foobar', /^bar/); + + err(function () { + assert.deepMatch('foobar', /^bar/i, 'blah'); + }, "blah: expected 'foobar' to match /^bar/i"); + + err(function () { + assert.notDeepMatch('foobar', /^foobar/i, 'blah'); + }, "blah: expected 'foobar' not to match /^foobar/i"); + + assert.notDeepMatch("fr33 mon3y", /fr[e3]|[e3]|[e3] money/i); + }); + it('property', function () { var obj = { foo: { bar: 'baz' } }; var simpleObj = { foo: 'bar' };