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

Assertions to test if objects are extensible. #479

Merged
merged 3 commits into from
Jul 12, 2015
Merged
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
81 changes: 81 additions & 0 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1618,4 +1618,85 @@ module.exports = function (chai, _) {
Assertion.addChainableMethod('decrease', assertDecreases);
Assertion.addChainableMethod('decreases', assertDecreases);

/**
* ### .extensible
*
* Asserts that the target is extensible (can have new properties added to
* it).
*
* var nonExtensibleObject = Object.preventExtensions({});
* var sealedObject = Object.seal({});
* var frozenObject = Object.freeze({});
*
* expect({}).to.be.extensible;
* expect(nonExtensibleObject).to.not.be.extensible;
* expect(sealedObject).to.not.be.extensible;
* expect(frozenObject).to.not.be.extensible;
*
* @name extensible
* @api public
*/

Assertion.addProperty('extensible', function() {
var obj = flag(this, 'object');

this.assert(
Object.isExtensible(obj)
, 'expected #{this} to be extensible'
, 'expected #{this} to not be extensible'
);
});

/**
* ### .sealed
*
* Asserts that the target is sealed (cannot have new properties added to it
* and its existing properties cannot be removed).
*
* var sealedObject = Object.seal({});
* var frozenObject = Object.freeze({});
*
* expect(sealedObject).to.be.sealed;
* expect(frozenObject).to.be.sealed;
* expect({}).to.not.be.sealed;
*
* @name sealed
* @api public
*/

Assertion.addProperty('sealed', function() {
var obj = flag(this, 'object');

this.assert(
Object.isSealed(obj)
, 'expected #{this} to be sealed'
, 'expected #{this} to not be sealed'
);
});

/**
* ### .frozen
*
* Asserts that the target is frozen (cannot have new properties added to it
* and its existing properties cannot be modified).
*
* var frozenObject = Object.freeze({});
*
* expect(frozenObject).to.be.frozen;
* expect({}).to.not.be.frozen;
*
* @name frozen
* @api public
*/

Assertion.addProperty('frozen', function() {
var obj = flag(this, 'object');

this.assert(
Object.isSealed(obj)
, 'expected #{this} to be frozen'
, 'expected #{this} to not be frozen'
);
});

};
115 changes: 115 additions & 0 deletions lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,121 @@ module.exports = function (chai, util) {
}
};

/**
* ### .extensible(object)
*
* Asserts that `object` is extensible (can have new properties added to it).
*
* assert.extensible({});
*
* @name extensible
* @param {Object} object
* @param {String} message _optional_
* @api public
*/

assert.extensible = function (obj, msg) {
new Assertion(obj, msg).to.be.extensible;
};

/**
* ### .notExtensible(object)
*
* Asserts that `object` is _not_ extensible.
*
* var nonExtensibleObject = Object.preventExtensions({});
* var sealedObject = Object.seal({});
* var frozenObject = Object.freese({});
*
* assert.notExtensible(nonExtensibleObject);
* assert.notExtensible(sealedObject);
* assert.notExtensible(frozenObject);
*
* @name notExtensible
* @param {Object} object
* @param {String} message _optional_
* @api public
*/

assert.notExtensible = function (obj, msg) {
new Assertion(obj, msg).to.not.be.extensible;
};

/**
* ### .sealed(object)
*
* Asserts that `object` is sealed (cannot have new properties added to it
* and its existing properties cannot be removed).
*
* var sealedObject = Object.seal({});
* var frozenObject = Object.seal({});
*
* assert.sealed(sealedObject);
* assert.sealed(frozenObject);
*
* @name sealed
* @param {Object} object
* @param {String} message _optional_
* @api public
*/

assert.sealed = function (obj, msg) {
new Assertion(obj, msg).to.be.sealed;
};

/**
* ### .notSealed(object)
*
* Asserts that `object` is _not_ sealed.
*
* assert.notSealed({});
*
* @name notSealed
* @param {Object} object
* @param {String} message _optional_
* @api public
*/

assert.notSealed = function (obj, msg) {
new Assertion(obj, msg).to.not.be.sealed;
};

/**
* ### .frozen(object)
*
* Asserts that `object` is frozen (cannot have new properties added to it
* and its existing properties cannot be modified).
*
* var frozenObject = Object.freeze({});
* assert.frozen(frozenObject);
*
* @name frozen
* @param {Object} object
* @param {String} message _optional_
* @api public
*/

assert.frozen = function (obj, msg) {
new Assertion(obj, msg).to.be.frozen;
};

/**
* ### .notFrozen(object)
*
* Asserts that `object` is _not_ frozen.
*
* assert.notFrozen({});
*
* @name notSealed
* @param {Object} object
* @param {String} message _optional_
* @api public
*/

assert.notFrozen = function (obj, msg) {
new Assertion(obj, msg).to.not.be.frozen;
};

/*!
* Aliases.
*/
Expand Down
60 changes: 60 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,4 +738,64 @@ describe('assert', function () {
assert.doesNotIncrease(smFn, obj, 'value');
});

it('extensible', function() {
var nonExtensibleObject = Object.preventExtensions({});

assert.extensible({});

err(function() {
assert.extensible(nonExtensibleObject);
}, 'expected {} to be extensible');
});

it('notExtensible', function() {
var nonExtensibleObject = Object.preventExtensions({});

assert.notExtensible(nonExtensibleObject);

err(function() {
assert.notExtensible({});
}, 'expected {} to not be extensible');
});

it('sealed', function() {
var sealedObject = Object.seal({});

assert.sealed(sealedObject);

err(function() {
assert.sealed({});
}, 'expected {} to be sealed');
});

it('notSealed', function() {
var sealedObject = Object.seal({});

assert.notSealed({});

err(function() {
assert.notSealed(sealedObject);
}, 'expected {} to not be sealed');
});

it('frozen', function() {
var frozenObject = Object.freeze({});

assert.frozen(frozenObject);

err(function() {
assert.frozen({});
}, 'expected {} to be frozen');
});

it('notFrozen', function() {
var frozenObject = Object.freeze({});

assert.notFrozen({});

err(function() {
assert.notFrozen(frozenObject);
}, 'expected {} to not be frozen');
});

});
44 changes: 44 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1060,5 +1060,49 @@ describe('expect', function () {
expect(decFn).to.decrease(obj, 'value');
});

it('extensible', function() {
var nonExtensibleObject = Object.preventExtensions({});

expect({}).to.be.extensible;
expect(nonExtensibleObject).to.not.be.extensible;

err(function() {
expect(nonExtensibleObject).to.be.extensible;
}, 'expected {} to be extensible');

err(function() {
expect({}).to.not.be.extensible;
}, 'expected {} to not be extensible');
});

it('sealed', function() {
var sealedObject = Object.seal({});

expect(sealedObject).to.be.sealed;
expect({}).to.not.be.sealed;

err(function() {
expect({}).to.be.sealed;
}, 'expected {} to be sealed');

err(function() {
expect(sealedObject).to.not.be.sealed;
}, 'expected {} to not be sealed');
});

it('frozen', function() {
var frozenObject = Object.freeze({});

expect(frozenObject).to.be.frozen;
expect({}).to.not.be.frozen;

err(function() {
expect({}).to.be.frozen;
}, 'expected {} to be frozen');

err(function() {
expect(frozenObject).to.not.be.frozen;
}, 'expected {} to not be frozen');
});

});
45 changes: 45 additions & 0 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -923,4 +923,49 @@ describe('should', function() {
incFn.should.not.decrease(obj, 'value');
decFn.should.decrease(obj, 'value');
});

it('extensible', function() {
var nonExtensibleObject = Object.preventExtensions({});

({}).should.be.extensible;
nonExtensibleObject.should.not.be.extensible;

err(function() {
nonExtensibleObject.should.be.extensible;
}, 'expected {} to be extensible');

err(function() {
({}).should.not.be.extensible;
}, 'expected {} to not be extensible');
});

it('sealed', function() {
var sealedObject = Object.seal({});

sealedObject.should.be.sealed;
({}).should.not.be.sealed;

err(function() {
({}).should.be.sealed;
}, 'expected {} to be sealed');

err(function() {
sealedObject.should.not.be.sealed;
}, 'expected {} to not be sealed');
});

it('frozen', function() {
var frozenObject = Object.freeze({});

frozenObject.should.be.frozen;
({}).should.not.be.frozen;

err(function() {
({}).should.be.frozen;
}, 'expected {} to be frozen');

err(function() {
frozenObject.should.not.be.frozen;
}, 'expected {} to not be frozen');
});
});