Skip to content

Commit

Permalink
assert interface: add .ownInclude and .notOwnInclude
Browse files Browse the repository at this point in the history
  • Loading branch information
zetamorph committed May 4, 2017
1 parent 24d7fa3 commit 97b6243
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 19 deletions.
85 changes: 68 additions & 17 deletions lib/chai/interface/assert.js
Expand Up @@ -1013,10 +1013,11 @@ module.exports = function (chai, util) {
};

/**
* ### .nestedInclude(targetObj, nestedObject, [msg])
* ### .nestedInclude(targetObject, nestedObject, [message])
*
* Asserts that 'targetObject' includes 'nestedObject'.
* Enables the use of dot- and bracket-notation for referencing nested properties.
* Enables the use of dot- and bracket-notation for referencing nested
* properties.
* '[]' and '.' in property names can be escaped using double backslashes.
*
* assert.nestedInclude({'.a': {'b': 'x'}}, {'\\.a.[b]': 'x'});
Expand All @@ -1035,10 +1036,11 @@ module.exports = function (chai, util) {
};

/**
* ### .notNestedInclude(targetObj, nestedObj, [msg])
* ### .notNestedInclude(targetObject, nestedObject, [message])
*
* Asserts that 'targetObject' does not include 'nestedObject'.
* Enables the use of dot- and bracket-notation for referencing nested properties.
* Enables the use of dot- and bracket-notation for referencing nested
* properties.
* '[]' and '.' in property names can be escaped using double backslashes.
*
* assert.notNestedInclude({'.a': {'b': 'x'}}, {'\\.a.[b]': 'y'});
Expand All @@ -1054,18 +1056,21 @@ module.exports = function (chai, util) {
*/

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

/**
* ### .deepNestedInclude(targetObject, nestedObject, [msg])
* ### .deepNestedInclude(targetObject, nestedObject, [message])
*
* Asserts that 'targetObj' includes 'nestedObject' while checking for deep equality.
* Enables the user of dot- and bracket-notation for referencing nested properties.
* Asserts that 'targetObject' includes 'nestedObject' while checking for
* deep equality.
* Enables the use 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}});
* 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
Expand All @@ -1076,18 +1081,21 @@ module.exports = function (chai, util) {
*/

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

/**
* ### .notDeepNestedInclude(targetObject, nestedObject, [msg])
* ### .notDeepNestedInclude(targetObject, nestedObject, [message])
*
* Asserts that 'targetObj' does not include 'nestedObject' while checking for deep equality.
* Enables the user of dot- and bracket-notation for referencing nested properties.
* Asserts that 'targetObject' does not include 'nestedObject' while
* checking for deep equality.
* Enables the use 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}});
* 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
Expand All @@ -1098,7 +1106,50 @@ module.exports = function (chai, util) {
*/

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

/**
* ### .ownInclude(targetObject, objectToBeIncluded, [message])
*
* Asserts that 'targetObject' includes 'objectToBeIncluded' while
* ignoring inherited properties.
*
* assert.OwnInclude({ a: 1 }, { a: 1 });
*
* @name ownInclude
* @param {Object} targetObject
* @param {Object} objectToBeIncluded
* @param {String} message
* @namespace Assert
* @api public
*/

assert.ownInclude = function(exp, inc, msg) {
new Assertion(exp, msg, assert.ownInclude, true).own.include(inc);
};

/**
* ### .notOwnInclude(targetObject, objectToNotBeIncluded, [message])
*
* Asserts that 'targetObject' does not include 'objectToNotBeIncluded' while
* ignoring inherited properties.
*
* Object.prototype.b = 2;
*
* assert.notOwnInclude({ a: 1 }, { b: 2 });
*
* @name notOwnInclude
* @param {Object} targetObject
* @param {Object} objectToNotBeIncluded
* @param {String} message
* @namespace Assert
* @api public
*/

assert.notOwnInclude = function(exp, inc, msg) {
new Assertion(exp, msg, assert.notOwnInclude, true).not.own.include(inc);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions test/assert.js
Expand Up @@ -812,7 +812,7 @@ describe('assert', function () {
}, "blah: expected { a: 1 } to have own property 'a' of 3, but got 1");

err(function () {
assert.ownInclude({a: 1}, 'blah', {a: 3});
assert.ownInclude({a: 1}, {a: 3}, 'blah');
}, "blah: expected { a: 1 } to have own property 'a' of 3, but got 1");

err(function () {
Expand All @@ -834,7 +834,7 @@ describe('assert', function () {
}, "blah: expected { a: { b: 2 } } to have deep own property 'a' of { c: 3 }, but got { b: 2 }");

err(function () {
assert.deepOwnInclude({a: {b: 2}}, 'blah', {a: {c: 3}});
assert.deepOwnInclude({a: {b: 2}}, {a: {c: 3}}, 'blah');
}, "blah: expected { a: { b: 2 } } to have deep own property 'a' of { c: 3 }, but got { b: 2 }");

err(function () {
Expand Down

0 comments on commit 97b6243

Please sign in to comment.