diff --git a/lib/reporters/base.js b/lib/reporters/base.js index f3a4e67ca3..b552e09756 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -166,6 +166,25 @@ function stringifyDiffObjs (err) { } } +/** + * Returns a diff between 2 strings with coloured ANSI output. + * + * The diff will be either inline or unified dependant on the value + * of `Base.inlineDiff`. + * + * @api private + * @param {String} actual + * @param {String} expected + * @return {string} Diff + */ +var generateDiff = exports.generateDiff = function (actual, expected) { + if (exports.inlineDiffs) { + return inlineDiff(actual, expected); + } else { + return unifiedDiff(actual, expected); + } +}; + /** * Output the given `failures` as a list. * @@ -215,11 +234,7 @@ exports.list = function (failures) { var match = message.match(/^([^:]+): expected/); msg = '\n ' + color('error message', match ? match[1] : msg); - if (exports.inlineDiffs) { - msg += inlineDiff(err); - } else { - msg += unifiedDiff(err); - } + msg += generateDiff(err.actual, err.expected); } // indent stack trace @@ -368,14 +383,15 @@ function pad (str, len) { } /** - * Returns an inline diff between 2 strings with coloured ANSI output + * Returns an inline diff between 2 strings with coloured ANSI output. * * @api private - * @param {Error} err with actual/expected + * @param {String} actual + * @param {String} expected * @return {string} Diff */ -function inlineDiff (err) { - var msg = errorDiff(err); +function inlineDiff (actual, expected) { + var msg = errorDiff(actual, expected); // linenos var lines = msg.split('\n'); @@ -401,13 +417,14 @@ function inlineDiff (err) { } /** - * Returns a unified diff between two strings. + * Returns a unified diff between two strings with coloured ANSI output. * * @api private - * @param {Error} err with actual/expected + * @param {String} actual + * @param {String} expected * @return {string} The diff. */ -function unifiedDiff (err) { +function unifiedDiff (actual, expected) { var indent = ' '; function cleanUp (line) { if (line[0] === '+') { @@ -427,7 +444,7 @@ function unifiedDiff (err) { function notBlank (line) { return typeof line !== 'undefined' && line !== null; } - var msg = diff.createPatch('string', err.actual, err.expected); + var msg = diff.createPatch('string', actual, expected); var lines = msg.split('\n').splice(5); return '\n ' + colorLines('diff added', '+ expected') + ' ' + @@ -440,11 +457,12 @@ function unifiedDiff (err) { * Return a character diff for `err`. * * @api private - * @param {Error} err - * @return {string} + * @param {String} actual + * @param {String} expected + * @return {string} the diff */ -function errorDiff (err) { - return diff.diffWordsWithSpace(err.actual, err.expected).map(function (str) { +function errorDiff (actual, expected) { + return diff.diffWordsWithSpace(actual, expected).map(function (str) { if (str.added) { return colorLines('diff added', str.value); }