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

Provide a non-strict property check #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules/
/tmp/
*.tgz
.idea
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
14 changes: 8 additions & 6 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -566,7 +566,7 @@ Assert object is an.. object.
```

<a name="Must.prototype.own" />
### Must.prototype.own(property, [value])
### Must.prototype.own(property, [value, strict])
Alias of [`ownProperty`](#Must.prototype.ownProperty).

<a name="Must.prototype.ownKeys" />
Expand All @@ -583,9 +583,10 @@ Pass an array of strings as `keys`.
```

<a name="Must.prototype.ownProperty" />
### 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).
Expand All @@ -610,9 +611,10 @@ Elements are checked with strict equals (`===`).
```

<a name="Must.prototype.property" />
### 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).
Expand Down
14 changes: 10 additions & 4 deletions lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -703,20 +704,23 @@ exports.frozen = function() {
* @method property
* @param property
* @param [value]
* @param [strict_equals]
*/
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)
if (arguments.length > 1) msg += " equal to " + inspect(expected) + ", but is " + inspect(this.actual[property])
insist.call(this, ok, msg)
}

/**
* 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).
Expand All @@ -727,14 +731,16 @@ exports.property = function(property, expected) {
* @method ownProperty
* @param property
* @param [value]
* @param [strict_equals]
*/
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)
}

Expand Down
4 changes: 2 additions & 2 deletions test/assertions_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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"
})
})
})
Expand Down