Skip to content

Commit

Permalink
Merge pull request #570 from jurko-gospodnetic/fix-error-message-stri…
Browse files Browse the repository at this point in the history
…ng-substitutions

Fix error message string substitutions
  • Loading branch information
keithamus committed Dec 14, 2015
2 parents 068dc2b + 568fc1a commit a1e8373
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/chai/utils/getMessage.js
Expand Up @@ -43,9 +43,9 @@ module.exports = function (obj, args) {
if(typeof msg === "function") msg = msg();
msg = msg || '';
msg = msg
.replace(/#{this}/g, objDisplay(val))
.replace(/#{act}/g, objDisplay(actual))
.replace(/#{exp}/g, objDisplay(expected));
.replace(/#{this}/g, function () { return objDisplay(val); })
.replace(/#{act}/g, function () { return objDisplay(actual); })
.replace(/#{exp}/g, function () { return objDisplay(expected); });

return flagMsg ? flagMsg + ': ' + msg : msg;
};
65 changes: 65 additions & 0 deletions test/utilities.js
Expand Up @@ -342,7 +342,11 @@ describe('utilities', function () {
var obj = {};
_.flag(obj, 'message', 'foo');
expect(_.getMessage(obj, [])).to.contain('foo');
});
});

it('getMessage passed message as function', function () {
chai.use(function (_chai, _) {
var obj = {};
var msg = function() { return "expected a to eql b"; }
var negateMsg = function() { return "expected a not to eql b"; }
Expand All @@ -352,6 +356,67 @@ describe('utilities', function () {
});
});

it('getMessage template tag substitution', function () {
chai.use(function (_chai, _) {
var objName = 'trojan horse';
var actualValue = 'an actual value';
var expectedValue = 'an expected value';
[
// known template tags
{
template: 'one #{this} two',
expected: 'one \'' + objName + '\' two'
},
{
template: 'one #{act} two',
expected: 'one \'' + actualValue + '\' two'
},
{
template: 'one #{exp} two',
expected: 'one \'' + expectedValue + '\' two'
},
// unknown template tag
{
template: 'one #{unknown} two',
expected: 'one #{unknown} two'
},
// repeated template tag
{
template: '#{this}#{this}',
expected: '\'' + objName + '\'\'' + objName + '\''
},
// multiple template tags in different order
{
template: '#{this}#{act}#{exp}#{act}#{this}',
expected: '\'' + objName + '\'\'' + actualValue + '\'\'' + expectedValue + '\'\'' + actualValue + '\'\'' + objName + '\''
},
// immune to string.prototype.replace() `$` substitution
{
objName: '-$$-',
template: '#{this}',
expected: '\'-$$-\''
},
{
actualValue: '-$$-',
template: '#{act}',
expected: '\'-$$-\''
},
{
expectedValue: '-$$-',
template: '#{exp}',
expected: '\'-$$-\''
}
].forEach(function (config) {
config.objName = config.objName || objName;
config.actualValue = config.actualValue || actualValue;
config.expectedValue = config.expectedValue || expectedValue;
var obj = {_obj: config.actualValue};
_.flag(obj, 'object', config.objName);
expect(_.getMessage(obj, [null, config.template, null, config.expectedValue])).to.equal(config.expected);
});
});
});

it('inspect with custom object-returning inspect()s', function () {
chai.use(function (_chai, _) {
var obj = {
Expand Down

0 comments on commit a1e8373

Please sign in to comment.