Skip to content

Commit

Permalink
Merge d2c2eb5 into c7730a6
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysarson committed Jan 13, 2018
2 parents c7730a6 + d2c2eb5 commit 9f2da69
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 143 deletions.
41 changes: 41 additions & 0 deletions lib/colorUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

exports.colors = {
pass: 90,
fail: 31,
'bright pass': 92,
'bright fail': 91,
'bright yellow': 93,
pending: 36,
suite: 0,
'error title': 0,
'error message': 31,
'error stack': 90,
checkmark: 32,
fast: 90,
medium: 33,
slow: 31,
green: 32,
light: 90,
'diff gutter': 90,
'diff added': 32,
'diff removed': 31
};

/**
* Color `str` with the given `type`.
*
* **Note:** `str` will only be coloured if `options.useColors === true`.
*
* @param {string} type
* @param {string} str
* @param {object} options
* @return {string}
* @api private
*/
exports.color = function color (type, str, options) {
if (!options.useColors) {
return String(str);
}
return '\u001b[' + exports.colors[type] + 'm' + str + '\u001b[0m';
};
126 changes: 126 additions & 0 deletions lib/formatDiffs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
'use strict';

var colorUtils = require('./colorUtils');
var diff = require('diff');

/**
* Pad the given `str` to `len`.
*
* @api private
* @param {string} str
* @param {string} len
* @return {string}
*/
function pad (str, len) {
str = String(str);
return Array(len - str.length + 1).join(' ') + str;
}

/**
* Returns an inline diff between 2 strings with coloured ANSI output
*
* @api private
* @param {Error} err with actual/expected
* @return {string} Diff
*/
exports.inlineDiff = function inlineDiff (err, options) {
var msg = errorDiff(err, options);

// linenos
var lines = msg.split('\n');
if (lines.length > 4) {
var width = String(lines.length).length;
msg = lines.map(function (str, i) {
return pad(++i, width) + ' |' + ' ' + str;
}).join('\n');
}

// legend
msg = '\n' +
colorUtils.color('diff removed', 'actual', options) +
' ' +
colorUtils.color('diff added', 'expected', options) +
'\n\n' +
msg +
'\n';

// indent
msg = msg.replace(/^/gm, ' ');
return msg;
};

/**
* Returns a unified diff between two strings.
*
* @api private
* @param {Error} err with actual/expected
* @param {object} options
* @param {boolean} options.useColors Should the diff be displayed in color.
* @return {string} The diff.
*/
exports.unifiedDiff = function unifiedDiff (err, options) {
var indent = ' ';
function cleanUp (line) {
if (line[0] === '+') {
return indent + colorLines('diff added', line, options);
}
if (line[0] === '-') {
return indent + colorLines('diff removed', line, options);
}
if (line.match(/@@/)) {
return '--';
}
if (line.match(/\\ No newline/)) {
return null;
}
return indent + line;
}
function notBlank (line) {
return typeof line !== 'undefined' && line !== null;
}
var msg = diff.createPatch('string', err.actual, err.expected);
var lines = msg.split('\n').splice(5);
return '\n ' +
colorLines('diff added', '+ expected', options) + ' ' +
colorLines('diff removed', '- actual', options) +
'\n\n' +
lines.map(cleanUp).filter(notBlank).join('\n');
};

/**
* Return a character diff for `err`.
*
* @api private
* @param {Error} err with actual/expected
* @param {object} options
* @param {boolean} options.useColors Should the diff be displayed in color.
* @return {string}
*/
function errorDiff (err, options) {
return diff.diffWordsWithSpace(err.actual, err.expected).map(function (str) {
if (str.added) {
return colorLines('diff added', str.value, options);
}
if (str.removed) {
return colorLines('diff removed', str.value, options);
}
return str.value;
}).join('');
}

/**
* Color lines for `str`, using the color `name`.
*
* **Note:** lines in `str` will only be coloured if `options.useColors === true`.
*
* @api private
* @param {string} name
* @param {string} str
* @param {object} options
* @return {string}
*/
function colorLines (name, str, options) {
return str.split('\n').map(function (str) {
return colorUtils.color(name, str, options);
}).join('\n');
}
149 changes: 6 additions & 143 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
*/

var tty = require('tty');
var diff = require('diff');
var ms = require('../ms');
var utils = require('../utils');
var colorUtils = require('../colorUtils');
var formatError = require('../formatDiffs');
var supportsColor = process.browser ? null : require('supports-color');

/**
Expand Down Expand Up @@ -51,27 +52,7 @@ exports.inlineDiffs = false;
* Default color map.
*/

exports.colors = {
pass: 90,
fail: 31,
'bright pass': 92,
'bright fail': 91,
'bright yellow': 93,
pending: 36,
suite: 0,
'error title': 0,
'error message': 31,
'error stack': 90,
checkmark: 32,
fast: 90,
medium: 33,
slow: 31,
green: 32,
light: 90,
'diff gutter': 90,
'diff added': 32,
'diff removed': 31
};
exports.colors = colorUtils.colors;

/**
* Default symbol map.
Expand Down Expand Up @@ -104,10 +85,7 @@ if (process.platform === 'win32') {
* @api private
*/
var color = exports.color = function (type, str) {
if (!exports.useColors) {
return String(str);
}
return '\u001b[' + exports.colors[type] + 'm' + str + '\u001b[0m';
return colorUtils.color(type, str, exports);
};

/**
Expand Down Expand Up @@ -216,9 +194,9 @@ exports.list = function (failures) {
msg = '\n ' + color('error message', match ? match[1] : msg);

if (exports.inlineDiffs) {
msg += inlineDiff(err);
msg += formatError.inlineDiff(err, { useColors: exports.useColors });
} else {
msg += unifiedDiff(err);
msg += formatError.unifiedDiff(err, { useColors: exports.useColors });
}
}

Expand Down Expand Up @@ -354,121 +332,6 @@ Base.prototype.epilogue = function () {
console.log();
};

/**
* Pad the given `str` to `len`.
*
* @api private
* @param {string} str
* @param {string} len
* @return {string}
*/
function pad (str, len) {
str = String(str);
return Array(len - str.length + 1).join(' ') + str;
}

/**
* Returns an inline diff between 2 strings with coloured ANSI output
*
* @api private
* @param {Error} err with actual/expected
* @return {string} Diff
*/
function inlineDiff (err) {
var msg = errorDiff(err);

// linenos
var lines = msg.split('\n');
if (lines.length > 4) {
var width = String(lines.length).length;
msg = lines.map(function (str, i) {
return pad(++i, width) + ' |' + ' ' + str;
}).join('\n');
}

// legend
msg = '\n' +
color('diff removed', 'actual') +
' ' +
color('diff added', 'expected') +
'\n\n' +
msg +
'\n';

// indent
msg = msg.replace(/^/gm, ' ');
return msg;
}

/**
* Returns a unified diff between two strings.
*
* @api private
* @param {Error} err with actual/expected
* @return {string} The diff.
*/
function unifiedDiff (err) {
var indent = ' ';
function cleanUp (line) {
if (line[0] === '+') {
return indent + colorLines('diff added', line);
}
if (line[0] === '-') {
return indent + colorLines('diff removed', line);
}
if (line.match(/@@/)) {
return '--';
}
if (line.match(/\\ No newline/)) {
return null;
}
return indent + line;
}
function notBlank (line) {
return typeof line !== 'undefined' && line !== null;
}
var msg = diff.createPatch('string', err.actual, err.expected);
var lines = msg.split('\n').splice(5);
return '\n ' +
colorLines('diff added', '+ expected') + ' ' +
colorLines('diff removed', '- actual') +
'\n\n' +
lines.map(cleanUp).filter(notBlank).join('\n');
}

/**
* Return a character diff for `err`.
*
* @api private
* @param {Error} err
* @return {string}
*/
function errorDiff (err) {
return diff.diffWordsWithSpace(err.actual, err.expected).map(function (str) {
if (str.added) {
return colorLines('diff added', str.value);
}
if (str.removed) {
return colorLines('diff removed', str.value);
}
return str.value;
}).join('');
}

/**
* Color lines for `str`, using the color `name`.
*
* @api private
* @param {string} name
* @param {string} str
* @return {string}
*/
function colorLines (name, str) {
return str.split('\n').map(function (str) {
return color(name, str);
}).join('\n');
}

/**
* Object#toString reference.
*/
Expand Down

0 comments on commit 9f2da69

Please sign in to comment.