Allow messages to be functions

This commit is contained in:
charlierudolph 2014-06-08 14:00:42 -07:00
parent ab999d8917
commit 216ad26aa1
4 changed files with 14 additions and 2 deletions

View file

@ -88,8 +88,8 @@ module.exports = function (_chai, util) {
*
* @name assert
* @param {Philosophical} expression to be tested
* @param {String} message to display if fails
* @param {String} negatedMessage to display if negated expression fails
* @param {String or Function} message or function that returns message to display if fails
* @param {String or Function} negatedMessage or function that returns negatedMessage to display if negated expression fails
* @param {Mixed} expected value (remember to check for negation)
* @param {Mixed} actual (optional) will default to `this.obj`
* @api private

View file

@ -39,6 +39,7 @@ module.exports = function (obj, args) {
, msg = negate ? args[2] : args[1]
, flagMsg = flag(obj, 'message');
if(typeof msg === "function") msg = msg();
msg = msg || '';
msg = msg
.replace(/#{this}/g, objDisplay(val))

View file

@ -8,6 +8,10 @@ describe('assert', function () {
err(function () {
assert(foo == 'baz', "expected foo to equal `bar`");
}, "expected foo to equal `bar`");
err(function () {
assert(foo == 'baz', function() { return "expected foo to equal `bar`"; });
}, "expected foo to equal `bar`");
});
it('fail', function () {

View file

@ -215,6 +215,13 @@ describe('utilities', function () {
var obj = {};
_.flag(obj, 'message', 'foo');
expect(_.getMessage(obj, [])).to.contain('foo');
var obj = {};
msg = function() { return "expected a to eql b"; }
negateMsg = function() { return "expected a not to eql b"; }
expect(_.getMessage(obj, [null, msg, negateMsg])).to.equal("expected a to eql b");
_.flag(obj, 'negate', true);
expect(_.getMessage(obj, [null, msg, negateMsg])).to.equal("expected a not to eql b");
});
});