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

Fix error message string substitutions #570

Merged
merged 2 commits into from
Dec 14, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/chai/utils/getMessage.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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