Skip to content

Commit

Permalink
assert interface: add deepNestedInclude and notDeepNestedInclude
Browse files Browse the repository at this point in the history
  • Loading branch information
zetamorph committed May 4, 2017
1 parent 7063b94 commit 24d7fa3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
48 changes: 46 additions & 2 deletions lib/chai/interface/assert.js
Expand Up @@ -1013,7 +1013,7 @@ module.exports = function (chai, util) {
};

/**
* ### .nestedInclude
* ### .nestedInclude(targetObj, nestedObject, [msg])
*
* Asserts that 'targetObject' includes 'nestedObject'.
* Enables the use of dot- and bracket-notation for referencing nested properties.
Expand All @@ -1035,7 +1035,7 @@ module.exports = function (chai, util) {
};

/**
* ### .notNestedInclude
* ### .notNestedInclude(targetObj, nestedObj, [msg])
*
* Asserts that 'targetObject' does not include 'nestedObject'.
* Enables the use of dot- and bracket-notation for referencing nested properties.
Expand All @@ -1057,6 +1057,50 @@ module.exports = function (chai, util) {
new Assertion(exp, msg, assert.notNestedInclude, true).not.nested.include(inc);
};

/**
* ### .deepNestedInclude(targetObject, nestedObject, [msg])
*
* Asserts that 'targetObj' includes 'nestedObject' while checking for deep equality.
* Enables the user of dot- and bracket-notation for referencing nested properties.
* '[]' and '.' in property names can be escaped using double backslashes.
*
* assert.deepNestedInclude({a: {b: [{x: 1}]}}, {'a.b[0]': {x: 1}})
* assert.deepNestedInclude({'.a': {'[b]': {x: 1}}}, {'\\.a.\\[b\\]': {x: 1}});
*
* @name deepNestedInclude
* @param {Object} targetObject
* @param {Object} nestedObject
* @param {String} message
* @namespace Assert
* @api public
*/

assert.deepNestedInclude = function(exp, inc, msg) {
new Assertion(exp, msg, assert.deepNestedInclude, true).deep.nested.include(inc);
};

/**
* ### .notDeepNestedInclude(targetObject, nestedObject, [msg])
*
* Asserts that 'targetObj' does not include 'nestedObject' while checking for deep equality.
* Enables the user of dot- and bracket-notation for referencing nested properties.
* '[]' and '.' in property names can be escaped using double backslashes.
*
* assert.notDeepNestedInclude({a: {b: [{x: 1}]}}, {'a.b[0]': {y: 1}})
* assert.notDeepNestedInclude({'.a': {'[b]': {x: 1}}}, {'\\.a.\\[b\\]': {y: 2}});
*
* @name notDeepNestedInclude
* @param {Object} targetObject
* @param {Object} nestedObject
* @param {String} message
* @namespace Assert
* @api public
*/

assert.notDeepNestedInclude = function(exp, inc, msg) {
new Assertion(exp, msg, assert.notDeepNestedInclude, true).not.deep.nested.include(inc);
};

/**
* ### .match(value, regexp, [message])
*
Expand Down
6 changes: 3 additions & 3 deletions test/assert.js
Expand Up @@ -763,15 +763,15 @@ describe('assert', function () {
}, "blah: expected { a: { b: [ 'x', 'y' ] } } to have nested property 'a.b[1]' of 'x', but got 'y'");

err(function () {
assert.nestedInclude({a: {b: ['x', 'y']}}, 'blah', {'a.b[1]': 'x'});
assert.nestedInclude({a: {b: ['x', 'y']}}, {'a.b[1]': 'x'}, 'blah');
}, "blah: expected { a: { b: [ 'x', 'y' ] } } to have nested property 'a.b[1]' of 'x', but got 'y'");

err(function () {
assert.nestedInclude({a: {b: ['x', 'y']}}, {'a.c': 'y'});
}, "expected { a: { b: [ 'x', 'y' ] } } to have nested property 'a.c'");

err(function () {
assert.nestedInclude({a: {b: ['x', 'y']}}, {'a.b[1]': 'y'});
assert.notNestedInclude({a: {b: ['x', 'y']}}, {'a.b[1]': 'y'});
}, "expected { a: { b: [ 'x', 'y' ] } } to not have nested property 'a.b[1]' of 'y'");
});

Expand All @@ -788,7 +788,7 @@ describe('assert', function () {
}, "blah: expected { a: { b: [ [Object] ] } } to have deep nested property 'a.b[0]' of { y: 2 }, but got { x: 1 }");

err(function () {
assert.deepNestedInclude({a: {b: [{x: 1}]}}, 'blah', {'a.b[0]': {y: 2}});
assert.deepNestedInclude({a: {b: [{x: 1}]}}, {'a.b[0]': {y: 2}}, 'blah');
}, "blah: expected { a: { b: [ [Object] ] } } to have deep nested property 'a.b[0]' of { y: 2 }, but got { x: 1 }");

err(function () {
Expand Down

0 comments on commit 24d7fa3

Please sign in to comment.