From 5455ce4b5df429e47e060331a7f3f0eec56508d6 Mon Sep 17 00:00:00 2001 From: domenic Date: Sat, 26 May 2012 21:38:10 -0400 Subject: [PATCH] Replace `getAllFlags` with `transferFlags` utility. This will help more directly with #66 and with domenic/chai-as-promised#5. --- lib/utils/getAllFlags.js | 30 -------------------------- lib/utils/index.js | 4 ++-- lib/utils/transferFlags.js | 44 ++++++++++++++++++++++++++++++++++++++ test/utilities.js | 31 ++++++++++++++++++++++----- 4 files changed, 72 insertions(+), 37 deletions(-) delete mode 100644 lib/utils/getAllFlags.js create mode 100644 lib/utils/transferFlags.js diff --git a/lib/utils/getAllFlags.js b/lib/utils/getAllFlags.js deleted file mode 100644 index 3bf45878a..000000000 --- a/lib/utils/getAllFlags.js +++ /dev/null @@ -1,30 +0,0 @@ -/*! - * Chai - getAllFlags utility - * Copyright(c) 2012 Jake Luer - * MIT Licensed - */ - -/** - * ### getAllFlags(object) - * - * Get all the flags for a specific object. When working - * with a chai assertion will have at least: `object`, `ssfi`, - * and `message` if it was provided. Depending on your usage, - * you might want to delete those. - * - * utils.getAllFlags(this); // returns an object - * - * @param {Object} object (constructed Assertion) - * @name getAllFlags - * @api private - */ - -module.exports = function (obj) { - var flags = obj.__flags || (obj.__flags = Object.create(null)) - , res = {}; - - for (var flag in flags) - res[flag] = flags[flag]; - - return res; -}; diff --git a/lib/utils/index.js b/lib/utils/index.js index 0c1e53a49..5d2374f97 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -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 diff --git a/lib/utils/transferFlags.js b/lib/utils/transferFlags.js new file mode 100644 index 000000000..4f53d2876 --- /dev/null +++ b/lib/utils/transferFlags.js @@ -0,0 +1,44 @@ +/*! + * Chai - transferFlags utility + * Copyright(c) 2012 Jake Luer + * 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]; + } + } +}; diff --git a/test/utilities.js b/test/utilities.js index 3d0da0046..b415e79a8 100644 --- a/test/utilities.js +++ b/test/utilities.js @@ -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'