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

Add .deep.property for deep equality comparisons #758

Merged
merged 3 commits into from
Aug 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
112 changes: 73 additions & 39 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,44 @@ module.exports = function (chai, _) {
/**
* ### .deep
*
* Sets the `deep` flag, later used by the `equal` and
* `property` assertions.
* Sets the `deep` flag, later used by the `equal`, `members`, and `property`
* assertions.
*
* const obj = {a: 1};
* expect(obj).to.deep.equal({a: 1});
* expect([obj]).to.have.deep.members([{a: 1}]);
* expect({foo: obj}).to.have.deep.property('foo', {a: 1});
*
* @name deep
* @namespace BDD
* @api public
*/

Assertion.addProperty('deep', function () {
flag(this, 'deep', true);
});

/**
* ### .nested
*
* Sets the `nested` flag, later used by the `property` assertion.
*
* expect(foo).to.deep.equal({ bar: 'baz' });
* expect({ foo: { bar: { baz: 'quux' } } })
* .to.have.deep.property('foo.bar.baz', 'quux');
* .to.have.nested.property('foo.bar.baz', 'quux');
*
* `.deep.property` special characters can be escaped
* by adding two slashes before the `.` or `[]`.
* `.nested.property` special characters can be escaped by adding two slashes
* before the `.` or `[]`.
*
* var deepCss = { '.link': { '[target]': 42 }};
* expect(deepCss).to.have.deep.property('\\.link.\\[target\\]', 42);
* expect(deepCss).to.have.nested.property('\\.link.\\[target\\]', 42);
*
* @name deep
* @name nested
* @namespace BDD
* @api public
*/

Assertion.addProperty('deep', function () {
flag(this, 'deep', true);
Assertion.addProperty('nested', function () {
flag(this, 'nested', true);
});

/**
Expand Down Expand Up @@ -827,26 +845,39 @@ module.exports = function (chai, _) {
* ### .property(name, [value])
*
* Asserts that the target has a property `name`, optionally asserting that
* the value of that property is strictly equal to `value`.
* If the `deep` flag is set, you can use dot- and bracket-notation for deep
* references into objects and arrays.
* the value of that property is strictly equal to `value`.
*
* // simple referencing
* var obj = { foo: 'bar' };
* expect(obj).to.have.property('foo');
* expect(obj).to.have.property('foo', 'bar');
* expect(obj).to.not.have.property('baz');
* expect(obj).to.not.have.property('foo', 'baz');
* expect(obj).to.not.have.property('baz', 'bar');
*
* If the `deep` flag is set, asserts that the value of the property is deeply
* equal to `value`.
*
* var obj = { foo: { bar: 'baz' } };
* expect(obj).to.have.deep.property('foo', { bar: 'baz' });
* expect(obj).to.not.have.deep.property('foo', { bar: 'quux' });
*
* If the `nested` flag is set, you can use dot- and bracket-notation for
* nested references into objects and arrays.
*
* // deep referencing
* var deepObj = {
* green: { tea: 'matcha' }
* , teas: [ 'chai', 'matcha', { tea: 'konacha' } ]
* };
* expect(deepObj).to.have.nested.property('green.tea', 'matcha');
* expect(deepObj).to.have.nested.property('teas[1]', 'matcha');
* expect(deepObj).to.have.nested.property('teas[2].tea', 'konacha');
*
* The `deep` and `nested` flags can be combined.
*
* expect(deepObj).to.have.deep.property('green.tea', 'matcha');
* expect(deepObj).to.have.deep.property('teas[1]', 'matcha');
* expect(deepObj).to.have.deep.property('teas[2].tea', 'konacha');
* expect({ foo: { bar: { baz: 'quux' } } })
* .to.have.deep.nested.property('foo.bar', { baz: 'quux' });
*
* You can also use an array as the starting point of a `deep.property`
* You can also use an array as the starting point of a `nested.property`
* assertion, or traverse nested arrays.
*
* var arr = [
Expand All @@ -855,9 +886,8 @@ module.exports = function (chai, _) {
* , { tea: 'matcha' }
* , { tea: 'konacha' } ]
* ];
*
* expect(arr).to.have.deep.property('[0][1]', 'matcha');
* expect(arr).to.have.deep.property('[1][2].tea', 'konacha');
* expect(arr).to.have.nested.property('[0][1]', 'matcha');
* expect(arr).to.have.nested.property('[1][2].tea', 'konacha');
*
* Furthermore, `property` changes the subject of the assertion
* to be the value of that property from the original object. This
Expand All @@ -870,23 +900,24 @@ module.exports = function (chai, _) {
* .that.deep.equals({ tea: 'matcha' });
* expect(deepObj).to.have.property('teas')
* .that.is.an('array')
* .with.deep.property('[2]')
* .with.nested.property('[2]')
* .that.deep.equals({ tea: 'konacha' });
*
* Note that dots and brackets in `name` must be backslash-escaped when
* the `deep` flag is set, while they must NOT be escaped when the `deep`
* the `nested` flag is set, while they must NOT be escaped when the `nested`
* flag is not set.
*
* // simple referencing
* // without nested referencing
* var css = { '.link[target]': 42 };
* expect(css).to.have.property('.link[target]', 42);
*
* // deep referencing
* // with nested referencing
* var deepCss = { '.link': { '[target]': 42 }};
* expect(deepCss).to.have.deep.property('\\.link.\\[target\\]', 42);
* expect(deepCss).to.have.nested.property('\\.link.\\[target\\]', 42);
*
* @name property
* @alias deep.property
* @alias nested.property
* @param {String} name
* @param {Mixed} value (optional)
* @param {String} message _optional_
Expand All @@ -898,24 +929,27 @@ module.exports = function (chai, _) {
Assertion.addMethod('property', function (name, val, msg) {
if (msg) flag(this, 'message', msg);

var isDeep = !!flag(this, 'deep')
, descriptor = isDeep ? 'deep property ' : 'property '
var isNested = !!flag(this, 'nested')
, isDeep = !!flag(this, 'deep')
, descriptor = (isDeep ? 'deep ' : '')
+ (isNested ? 'nested ' : '')
+ 'property '
, negate = flag(this, 'negate')
, obj = flag(this, 'object')
, pathInfo = isDeep ? _.getPathInfo(name, obj) : null
, hasProperty = isDeep
, pathInfo = isNested ? _.getPathInfo(name, obj) : null
, hasProperty = isNested
? pathInfo.exists
: _.hasProperty(name, obj)
, value = isDeep
, value = isNested
? pathInfo.value
: obj[name];

if (negate && arguments.length > 1) {
if (undefined === value) {
msg = (msg != null) ? msg + ': ' : '';
throw new Error(msg + _.inspect(obj) + ' has no ' + descriptor + _.inspect(name));
}
} else {
// When performing a negated assertion for both name and val, merely having
// a property with the given name isn't enough to cause the assertion to
// fail. It must both have a property with the given name, and the value of
// that property must equal the given val. Therefore, skip this assertion in
// favor of the next.
if (!negate || arguments.length === 1) {
this.assert(
hasProperty
, 'expected #{this} to have a ' + descriptor + _.inspect(name)
Expand All @@ -924,7 +958,7 @@ module.exports = function (chai, _) {

if (arguments.length > 1) {
this.assert(
val === value
hasProperty && (isDeep ? _.eql(val, value) : val === value)
, 'expected #{this} to have a ' + descriptor + _.inspect(name) + ' of #{exp}, but got #{act}'
, 'expected #{this} to not have a ' + descriptor + _.inspect(name) + ' of #{act}'
, val
Expand Down