Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add assert deepMatch methods #1644

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
*
Expand Down
38 changes: 38 additions & 0 deletions lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
*
Expand Down
15 changes: 15 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' };
Expand Down