From c63d19aa6b29a88f253c849391fbe8399547347e Mon Sep 17 00:00:00 2001 From: smlgbl Date: Tue, 8 Jul 2014 11:05:00 +0200 Subject: [PATCH 1/6] Non-strict comparison for properties --- lib/assertions.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/assertions.js b/lib/assertions.js index 72a1058..c60d55d 100644 --- a/lib/assertions.js +++ b/lib/assertions.js @@ -704,10 +704,11 @@ exports.frozen = function() { * @param property * @param [value] */ -exports.property = function(property, expected) { +exports.property = function(property, expected, strict_equals) { var ok = this.actual != null ok = ok && property in Object(this.actual) if (ok && arguments.length > 1) ok = this.actual[property] === expected + if (ok && arguments.length > 2 && strict_equals === false) ok = eql(this.actual[property], expected) var msg = "have property \"" + property + "\"" if (arguments.length > 1) msg += " equal to " + inspect(expected) From aa73c74a66e6a509c4d7d7958d3bbcbb52eef59e Mon Sep 17 00:00:00 2001 From: smlgbl Date: Tue, 8 Jul 2014 11:32:27 +0200 Subject: [PATCH 2/6] Add info about the actual value of the property When comparing big objects, it's hard to spot the actual property to see exactly where the difference is. There's probably a better way of printing the object, but this is a little workaround. --- lib/assertions.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/assertions.js b/lib/assertions.js index c60d55d..7bf0721 100644 --- a/lib/assertions.js +++ b/lib/assertions.js @@ -711,7 +711,7 @@ exports.property = function(property, expected, strict_equals) { if (ok && arguments.length > 2 && strict_equals === false) ok = eql(this.actual[property], expected) var msg = "have property \"" + property + "\"" - if (arguments.length > 1) msg += " equal to " + inspect(expected) + if (arguments.length > 1) msg += " equal to " + inspect(expected) + ", but is " + inspect(this.actual[property]) insist.call(this, ok, msg) } @@ -729,13 +729,14 @@ exports.property = function(property, expected, strict_equals) { * @param property * @param [value] */ -exports.ownProperty = function(property, expected) { +exports.ownProperty = function(property, expected, strict_equals) { var ok = this.actual != null ok = ok && Object.prototype.hasOwnProperty.call(this.actual, property) if (ok && arguments.length > 1) ok = this.actual[property] === expected + if (ok && arguments.length > 2 && strict_equals === false) ok = eql(this.actual[property], expected) var msg = "have own property \"" + property + "\"" - if (arguments.length > 1) msg += " equal to " + inspect(expected) + if (arguments.length > 1) msg += " equal to " + inspect(expected) + ", but is " + inspect(this.actual[property]) insist.call(this, ok, msg) } From 15ba083e77ca28c658c717d526265e30e03b46ae Mon Sep 17 00:00:00 2001 From: smlgbl Date: Tue, 8 Jul 2014 15:04:22 +0200 Subject: [PATCH 3/6] Update inline documentation --- lib/assertions.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/assertions.js b/lib/assertions.js index 7bf0721..80bde2d 100644 --- a/lib/assertions.js +++ b/lib/assertions.js @@ -692,6 +692,7 @@ exports.frozen = function() { /** * Assert that an object has property `property`. * Optionally assert it *equals* (`===`) to `value`. + * For non-strict equality, provide 'false' as third parameter * * Takes **inherited properties** into account. To not do so, see * [`ownProperty`](#Must.prototype.ownProperty). @@ -718,6 +719,7 @@ exports.property = function(property, expected, strict_equals) { /** * Assert that an object has own property `property`. * Optionally assert it *equals* (`===`) to `value`. + * For non-strict equality, provide 'false' as third parameter * * **Does not** take **inherited properties** into account. To do so, see * [`property`](#Must.prototype.property). From 55874c825469d0c41dc12b7721b832bbbeb00106 Mon Sep 17 00:00:00 2001 From: "samuel.gabel" Date: Tue, 8 Jul 2014 15:09:51 +0200 Subject: [PATCH 4/6] Update documentation --- README.md | 6 +++--- doc/API.md | 14 ++++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a5d2fc8..f6c4afa 100644 --- a/README.md +++ b/README.md @@ -230,11 +230,11 @@ Must.js, please see the [Must.js API Documentation][api]. - [null](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.null)() - [number](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.number)() - [object](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.object)() -- [own](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.own)(property, [value]) +- [own](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.own)(property, [value, strict]) - [ownKeys](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.ownKeys)(keys) -- [ownProperty](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.ownProperty)(property, [value]) +- [ownProperty](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.ownProperty)(property, [value, strict]) - [permutationOf](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.permutationOf)(expected) -- [property](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.property)(property, [value]) +- [property](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.property)(property, [value, strict]) - [regexp](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.regexp)() - [string](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.string)() - [throw](https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.throw)([constructor], [expected]) diff --git a/doc/API.md b/doc/API.md index 759d61d..86d0f1f 100644 --- a/doc/API.md +++ b/doc/API.md @@ -44,11 +44,11 @@ Must.js API Documentation - [null](#Must.prototype.null)() - [number](#Must.prototype.number)() - [object](#Must.prototype.object)() -- [own](#Must.prototype.own)(property, [value]) +- [own](#Must.prototype.own)(property, [value, strict]) - [ownKeys](#Must.prototype.ownKeys)(keys) -- [ownProperty](#Must.prototype.ownProperty)(property, [value]) +- [ownProperty](#Must.prototype.ownProperty)(property, [value, strict]) - [permutationOf](#Must.prototype.permutationOf)(expected) -- [property](#Must.prototype.property)(property, [value]) +- [property](#Must.prototype.property)(property, [value, strict]) - [regexp](#Must.prototype.regexp)() - [string](#Must.prototype.string)() - [throw](#Must.prototype.throw)([constructor], [expected]) @@ -566,7 +566,7 @@ Assert object is an.. object. ``` -### Must.prototype.own(property, [value]) +### Must.prototype.own(property, [value, strict]) Alias of [`ownProperty`](#Must.prototype.ownProperty). @@ -583,9 +583,10 @@ Pass an array of strings as `keys`. ``` -### Must.prototype.ownProperty(property, [value]) +### Must.prototype.ownProperty(property, [value, strict]) Assert that an object has own property `property`. Optionally assert it *equals* (`===`) to `value`. +Third parameter `false` makes the equality check non-strict. **Does not** take **inherited properties** into account. To do so, see [`property`](#Must.prototype.property). @@ -610,9 +611,10 @@ Elements are checked with strict equals (`===`). ``` -### Must.prototype.property(property, [value]) +### Must.prototype.property(property, [value, strict]) Assert that an object has property `property`. Optionally assert it *equals* (`===`) to `value`. +Third parameter `false` makes the equality check non-strict. Takes **inherited properties** into account. To not do so, see [`ownProperty`](#Must.prototype.ownProperty). From f7e8e773584d054600bf1a7eb1a7026a5ae16125 Mon Sep 17 00:00:00 2001 From: "samuel.gabel" Date: Tue, 8 Jul 2014 15:32:44 +0200 Subject: [PATCH 5/6] Fix tests for AssertionError output --- lib/assertions.js | 2 ++ test/assertions_test.js | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/assertions.js b/lib/assertions.js index 80bde2d..37e336a 100644 --- a/lib/assertions.js +++ b/lib/assertions.js @@ -704,6 +704,7 @@ exports.frozen = function() { * @method property * @param property * @param [value] + * @param [strict_equals] */ exports.property = function(property, expected, strict_equals) { var ok = this.actual != null @@ -730,6 +731,7 @@ exports.property = function(property, expected, strict_equals) { * @method ownProperty * @param property * @param [value] + * @param [strict_equals] */ exports.ownProperty = function(property, expected, strict_equals) { var ok = this.actual != null diff --git a/test/assertions_test.js b/test/assertions_test.js index 9eed40d..47bfe9c 100644 --- a/test/assertions_test.js +++ b/test/assertions_test.js @@ -2150,7 +2150,7 @@ function mustPassProperty(name, inheritable) { mustThrowAssertionError(function() { Must({}).have[name]("love", 69) }, { actual: {}, - message: "{} must have "+errName+" \"love\" equal to 69" + message: "{} must have "+errName+" \"love\" equal to 69, but is undefined" }) describe(".not", function() { @@ -2162,7 +2162,7 @@ function mustPassProperty(name, inheritable) { mustThrowAssertionError(not, { actual: {love: 69}, - message: "{\"love\":69} must not have "+errName+" \"love\" equal to 69" + message: "{\"love\":69} must not have "+errName+" \"love\" equal to 69, but is 69" }) }) }) From f47992f892529832b2351a652d45186754e30f80 Mon Sep 17 00:00:00 2001 From: "samuel.gabel" Date: Tue, 8 Jul 2014 17:02:55 +0200 Subject: [PATCH 6/6] Ignore IDE specific files/folders --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 97854c5..41f3f21 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /node_modules/ /tmp/ *.tgz +.idea