Skip to content

Commit

Permalink
exposes generateDiff function from base reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysarson authored and boneskull committed Mar 6, 2018
1 parent f2ee53c commit bdcb3c3
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions lib/reporters/base.js
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand All @@ -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] === '+') {
Expand All @@ -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') + ' ' +
Expand All @@ -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);
}
Expand Down

0 comments on commit bdcb3c3

Please sign in to comment.