Skip to content

Commit

Permalink
Replace getAllFlags with transferFlags utility.
Browse files Browse the repository at this point in the history
This will help more directly with #66 and with chaijs/chai-as-promised#5.
  • Loading branch information
domenic committed May 27, 2012
1 parent 0191e76 commit 5455ce4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 37 deletions.
30 changes: 0 additions & 30 deletions lib/utils/getAllFlags.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ exports.inspect = require('./inspect');
exports.flag = require('./flag');

/*!
* getAllFlags utility
* Flag transferring utility
*/

exports.getAllFlags = require('./getAllFlags');
exports.transferFlags = require('./transferFlags');

/*!
* Deep equal utility
Expand Down
44 changes: 44 additions & 0 deletions lib/utils/transferFlags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*!
* Chai - transferFlags utility
* Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
* MIT Licensed
*/

/**
* ### transferFlags(assertion, object, includeAll = true)
*
* Transfer all the flags for `assertion` to `object`. If
* `includeAll` is set to `false`, then the base Chai
* assertion flags (namely `object`, `ssfi`, and `message`)
* will not be transferred.
*
*
* var newAssertion = new Assertion();
* utils.transferFlags(assertion, newAssertion);
*
* var anotherAsseriton = new Assertion(myObj);
* utils.transferFlags(assertion, anotherAssertion, false);
*
* @param {Assertion} assertion the assertion to transfer the flags from
* @param {Object} object the object to transfer the flags too; usually a new assertion
* @param {Boolean} includeAll
* @name getAllFlags
* @api private
*/

module.exports = function (assertion, object, includeAll) {
var flags = assertion.__flags || (assertion.__flags = Object.create(null));

if (!object.__flags) {
object.__flags = Object.create(null);
}

includeAll = arguments.length === 3 ? includeAll : true;

for (var flag in flags) {
if (includeAll ||
(flag !== 'object' && flag !== 'ssfi' && flag != 'message')) {
object.__flags[flag] = flags[flag];
}
}
};
31 changes: 26 additions & 5 deletions test/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,39 @@ suite('utilities', function () {
test.equal(bar);
});

test('getAllFlags', function () {
test('transferFlags', function () {
var foo = 'bar'
, test = expect(foo).not;

chai.use(function (_chai, utils) {
var flags = utils.getAllFlags(test);
expect(flags).to.be.an('object');
expect(flags).to.have.property('object', foo);
expect(flags).to.have.property('negate', true);
var obj = {};
utils.transferFlags(test, obj);
expect(utils.flag(obj, 'object')).to.equal(foo);
expect(utils.flag(obj, 'negate')).to.equal(true);
});
});

test('transferFlags, includeAll = false', function () {
var foo = 'bar';

chai.use(function (_chai, utils) {
var obj = {};

var assertion = new chai.Assertion({}, "message", test);
var flag = {};
utils.flag(obj, 'flagMe', flag);
utils.flag(obj, 'negate', true);
utils.transferFlags(test, obj, false);

expect(utils.flag(obj, 'object')).to.equal(undefined);
expect(utils.flag(obj, 'message')).to.equal(undefined);
expect(utils.flag(obj, 'ssfi')).to.equal(undefined);
expect(utils.flag(obj, 'negate')).to.equal(true);
expect(utils.flag(obj, 'flagMe')).to.equal(flag);
});
});


test('getPathValue', function () {
var object = {
hello: 'universe'
Expand Down

0 comments on commit 5455ce4

Please sign in to comment.