From 24d7fa3849a172ffc8d09ea1fa068021409bc850 Mon Sep 17 00:00:00 2001 From: Johannes Loewe Date: Mon, 1 May 2017 22:26:14 +0200 Subject: [PATCH] assert interface: add deepNestedInclude and notDeepNestedInclude --- lib/chai/interface/assert.js | 48 ++++++++++++++++++++++++++++++++++-- test/assert.js | 6 ++--- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/lib/chai/interface/assert.js b/lib/chai/interface/assert.js index a90fbe48e..b23a402d6 100644 --- a/lib/chai/interface/assert.js +++ b/lib/chai/interface/assert.js @@ -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. @@ -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. @@ -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]) * diff --git a/test/assert.js b/test/assert.js index 9623c1cb5..edfca52d7 100644 --- a/test/assert.js +++ b/test/assert.js @@ -763,7 +763,7 @@ 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 () { @@ -771,7 +771,7 @@ describe('assert', function () { }, "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'"); }); @@ -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 () {