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
Add propertyDescriptor
assertion
#408
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -888,6 +888,55 @@ module.exports = function (chai, _) { | |
Assertion.addMethod('ownProperty', assertOwnProperty); | ||
Assertion.addMethod('haveOwnProperty', assertOwnProperty); | ||
|
||
/** | ||
* ### .ownPropertyDescriptor(name[, descriptor[, message]]) | ||
* | ||
* Asserts that the target has an own property descriptor `name`, that optionally matches `descriptor`. | ||
* | ||
* expect('test').to.have.ownPropertyDescriptor('length'); | ||
* expect('test').to.have.ownPropertyDescriptor('length', { enumerable: false, configurable: false, writable: false, value: 4 }); | ||
* expect('test').not.to.have.ownPropertyDescriptor('length', { enumerable: false, configurable: false, writable: false, value: 3 }); | ||
* expect('test').ownPropertyDescriptor('length').to.have.property('enumerable', false); | ||
* expect('test').ownPropertyDescriptor('length').to.have.keys('value'); | ||
* | ||
* @name ownPropertyDescriptor | ||
* @alias haveOwnPropertyDescriptor | ||
* @param {String} name | ||
* @param {Object} descriptor _optional_ | ||
* @param {String} message _optional_ | ||
* @api public | ||
*/ | ||
|
||
function assertOwnPropertyDescriptor (name, descriptor, msg) { | ||
if (typeof descriptor === 'string') { | ||
msg = descriptor; | ||
descriptor = null; | ||
} | ||
if (msg) flag(this, 'message', msg); | ||
var obj = flag(this, 'object'); | ||
var actualDescriptor = Object.getOwnPropertyDescriptor(Object(obj), name); | ||
if (actualDescriptor && descriptor) { | ||
this.assert( | ||
_.eql(descriptor, actualDescriptor) | ||
, 'expected the own property descriptor for ' + _.inspect(name) + ' on #{this} to match ' + _.inspect(descriptor) + ', got ' + _.inspect(actualDescriptor) | ||
, 'expected the own property descriptor for ' + _.inspect(name) + ' on #{this} to not match ' + _.inspect(descriptor) | ||
, descriptor | ||
, actualDescriptor | ||
, true | ||
); | ||
} else { | ||
this.assert( | ||
actualDescriptor | ||
, 'expected #{this} to have an own property descriptor for ' + _.inspect(name) | ||
, 'expected #{this} to not have an own property descriptor for ' + _.inspect(name) | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logic could be cleaned up here to avoid the if (descriptor) {
this.assert(
_.eql(descriptor, actualDescriptor)
, 'expected the own property descriptor for ' + _.inspect(name) + ' on #{this} to match ' + _.inspect(descriptor) + ', got ' + _.inspect(actualDescriptor)
, 'expected the own property descriptor for ' + _.inspect(name) + ' on #{this} to not match ' + _.inspect(descriptor)
, descriptor
, actualDescriptor
, true
);
} else {
this.assert(
actualDescriptor
, 'expected #{this} to have an own property descriptor for ' + _.inspect(name)
, 'expected #{this} to not have an own property descriptor for ' + _.inspect(name)
);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm - the way you've changed it, if However, I think I can tweak it to avoid the Fix incoming. |
||
flag(this, 'object', actualDescriptor); | ||
} | ||
|
||
Assertion.addMethod('ownPropertyDescriptor', assertOwnPropertyDescriptor); | ||
Assertion.addMethod('haveOwnPropertyDescriptor', assertOwnPropertyDescriptor); | ||
|
||
/** | ||
* ### .length(value) | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a few more examples here? Specifically I'd like to see the following:
Feel free to adjust those as you see fit - but it should show a couple of examples to demonstrate what's possible (especially for any extra arguments).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, those look great :-)