Skip to content

Commit

Permalink
utils.diffStrings: Take an options object and make it configurable wh…
Browse files Browse the repository at this point in the history
…ether to highlight special characters.
  • Loading branch information
papandreou committed Sep 18, 2014
1 parent ce2ebd9 commit 4f69e53
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ module.exports = function (expect) {
},
diff: function (actual, expected, output, diff, inspect) {
var result = {
diff: utils.diffStrings(this.hexDump(actual), this.hexDump(expected), output, 'Chars')
diff: utils.diffStrings(this.hexDump(actual), this.hexDump(expected), output, {type: 'Chars', markUpSpecialCharacters: false})
};
for (var i = 0 ; i < result.diff.output.length ; i += 1) {
var isInAsciiChars = false;
Expand Down Expand Up @@ -724,7 +724,7 @@ module.exports = function (expect) {
diff: output,
inline: false
};
utils.diffStrings(actual, expected, output, 'WordsWithSpace');
utils.diffStrings(actual, expected, output, {type: 'WordsWithSpace', markUpSpecialCharacters: true});
return result;
}
});
Expand Down
37 changes: 21 additions & 16 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,24 @@ var utils = module.exports = {
}
},

markUpSpecialCharacters: function (output, text, specialCharStyle, baseStyle) {
text.split(specialCharRegexp).forEach(function (part) {
if (specialCharRegexp.test(part)) {
output.write(specialCharStyle, utils.escapeChar(part));
diffStrings: function (actual, expected, output, options) {
options = options || {};
var type = options.type || 'WordsWithSpace';

function addStringToOutput(output, text, baseStyle, specialCharStyle) {
if (options.markUpSpecialCharacters) {
text.split(specialCharRegexp).forEach(function (part) {
if (specialCharRegexp.test(part)) {
output.write(specialCharStyle || baseStyle, utils.escapeChar(part));
} else {
output.write(baseStyle, part);
}
});
return output;
} else {
output.write(baseStyle, part);
output.write(baseStyle, text);
}
});
return output;
},

diffStrings: function (actual, expected, output, diffAlgorithmName) {
diffAlgorithmName = diffAlgorithmName || 'WordsWithSpace';
}

var diffLines = [];
var lastPart;
Expand Down Expand Up @@ -245,11 +250,11 @@ var utils = module.exports = {
newValue = newValue.slice(0, -1);
}

stringDiff['diff' + diffAlgorithmName](oldValue, newValue).forEach(function (part) {
stringDiff['diff' + type](oldValue, newValue).forEach(function (part) {
if (part.added) {
utils.markUpSpecialCharacters(newLine, part.value, 'diffAddedSpecialChar', 'diffAddedHighlight');
addStringToOutput(newLine, part.value, 'diffAddedHighlight', 'diffAddedSpecialChar');
} else if (part.removed) {
utils.markUpSpecialCharacters(oldLine, part.value, 'diffRemovedSpecialChar', 'diffRemovedHighlight');
addStringToOutput(oldLine, part.value, 'diffRemovedHighlight', 'diffRemovedSpecialChar');
} else {
newLine.diffAddedLine(part.value);
oldLine.diffRemovedLine(part.value);
Expand All @@ -276,7 +281,7 @@ var utils = module.exports = {
part.value;

output.append(function () {
utils.markUpSpecialCharacters(this, value, 'diffAddedLine', 'diffAddedLine').prependLinesWith(function () {
addStringToOutput(this, value, 'diffAddedLine').prependLinesWith(function () {
this.diffAddedLine('+');
});
});
Expand All @@ -290,7 +295,7 @@ var utils = module.exports = {
part.value;

output.append(function () {
utils.markUpSpecialCharacters(this, value, 'diffRemovedLine', 'diffRemovedLine').prependLinesWith(function () {
addStringToOutput(this, value, 'diffRemovedLine').prependLinesWith(function () {
this.diffRemovedLine('-');
});
});
Expand Down

0 comments on commit 4f69e53

Please sign in to comment.