Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Add unified diff (close #862)

Romain Prietoeaaace730c6f
diff --git a/bin/_mocha b/bin/_mocha
index f6bf8957..b26d6729 100644
--- a/bin/_mocha
+++ b/bin/_mocha
@@ -85,6 +85,7 @@ program
.option('--interfaces', 'display available interfaces')
.option('--reporters', 'display available reporters')
.option('--compilers <ext>:<module>,...', 'use the given module(s) to compile files', list, [])
+ .option('--inline-diffs', 'display actual/expected differences inline within each string')
program.name = 'mocha';
@@ -211,6 +212,10 @@ if (~process.argv.indexOf('--colors') ||
Base.useColors = true;
}
+// --inline-diffs
+
+if (program.inlineDiffs) Base.inlineDiffs = true;
+
// --slow <ms>
if (program.slow) mocha.suite.slow(program.slow);
diff --git a/lib/reporters/base.js b/lib/reporters/base.js
index b8d9f822..18088558 100644
--- a/lib/reporters/base.js
+++ b/lib/reporters/base.js
@@ -35,6 +35,12 @@ exports = module.exports = Base;
exports.useColors = isatty;
+/**
+ * Inline diffs instead of +/-
+ */
+
+exports.inlineDiffs = false;
+
/**
* Default color map.
*/
@@ -175,31 +181,12 @@ exports.list = function(failures){
// actual / expected diff
if ('string' == typeof actual && 'string' == typeof expected) {
- msg = errorDiff(err, 'WordsWithSpace', escape);
-
- // 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');
+ fmt = color('error title', ' %s) %s:\n%s') + color('error stack', '\n%s\n');
+ if (exports.inlineDiffs) {
+ msg = inlineDiff(err, escape);
+ } else {
+ msg = unifiedDiff(err, escape);
}
-
- // legend
- msg = '\n'
- + color('diff removed', 'actual')
- + ' '
- + color('diff added', 'expected')
- + '\n\n'
- + msg
- + '\n';
-
- // indent
- msg = msg.replace(/^/gm, ' ');
-
- fmt = color('error title', ' %s) %s:\n%s')
- + color('error stack', '\n%s\n');
}
// indent stack trace without msg
@@ -335,6 +322,73 @@ function pad(str, len) {
return Array(len - str.length + 1).join(' ') + str;
}
+
+/**
+ * Returns an inline diff between 2 strings with coloured ANSI output
+ *
+ * @param {Error} Error with actual/expected
+ * @return {String} Diff
+ * @api private
+ */
+
+function inlineDiff(err, escape) {
+ var msg = errorDiff(err, 'WordsWithSpace', escape);
+
+ // 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 2 strings
+ *
+ * @param {Error} Error with actual/expected
+ * @return {String} Diff
+ * @api private
+ */
+
+function unifiedDiff(err, escape) {
+ var indent = ' ';
+ function cleanUp(line) {
+ if (escape) {
+ line = escapeInvisibles(line);
+ }
+ if (line[0] === '+') return indent + colorLines('diff added', line);
+ if (line[0] === '-') return indent + colorLines('diff removed', line);
+ if (line.match(/\@\@/)) return null;
+ if (line.match(/\\ No newline/)) return null;
+ else return indent + line;
+ }
+ function notBlank(line) {
+ return line != null;
+ }
+ msg = diff.createPatch('string', err.actual, err.expected);
+ var lines = msg.split('\n').splice(4);
+ 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`.
*
@@ -344,19 +398,28 @@ function pad(str, len) {
*/
function errorDiff(err, type, escape) {
- return diff['diff' + type](err.actual, err.expected).map(function(str){
- if (escape) {
- str.value = str.value
- .replace(/\t/g, '<tab>')
- .replace(/\r/g, '<CR>')
- .replace(/\n/g, '<LF>\n');
- }
+ var actual = escape ? escapeInvisibles(err.actual) : err.actual;
+ var expected = escape ? escapeInvisibles(err.expected) : err.expected;
+ return diff['diff' + type](actual, 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('');
}
+/**
+ * Returns a string with all invisible characters in plain text
+ *
+ * @param {String} line
+ * @return {String}
+ * @api private
+ */
+function escapeInvisibles(line) {
+ return line.replace(/\t/g, '<tab>')
+ .replace(/\r/g, '<CR>')
+ .replace(/\n/g, '<LF>\n');
+}
+
/**
* Color lines for `str`, using the color `name`.
*
diff --git a/mocha.js b/mocha.js
index 0a04e204..d11f3419 100644
--- a/mocha.js
+++ b/mocha.js
@@ -1833,6 +1833,12 @@ exports = module.exports = Base;
exports.useColors = isatty;
+/**
+ * Inline diffs instead of +/-
+ */
+
+exports.inlineDiffs = false;
+
/**
* Default color map.
*/
@@ -1973,31 +1979,12 @@ exports.list = function(failures){
// actual / expected diff
if ('string' == typeof actual && 'string' == typeof expected) {
- msg = errorDiff(err, 'WordsWithSpace', escape);
-
- // 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');
+ fmt = color('error title', ' %s) %s:\n%s') + color('error stack', '\n%s\n');
+ if (exports.inlineDiffs) {
+ msg = inlineDiff(err, escape);
+ } else {
+ msg = unifiedDiff(err, escape);
}
-
- // legend
- msg = '\n'
- + color('diff removed', 'actual')
- + ' '
- + color('diff added', 'expected')
- + '\n\n'
- + msg
- + '\n';
-
- // indent
- msg = msg.replace(/^/gm, ' ');
-
- fmt = color('error title', ' %s) %s:\n%s')
- + color('error stack', '\n%s\n');
}
// indent stack trace without msg
@@ -2133,6 +2120,73 @@ function pad(str, len) {
return Array(len - str.length + 1).join(' ') + str;
}
+
+/**
+ * Returns an inline diff between 2 strings with coloured ANSI output
+ *
+ * @param {Error} Error with actual/expected
+ * @return {String} Diff
+ * @api private
+ */
+
+function inlineDiff(err) {
+ var msg = errorDiff(err, 'WordsWithSpace', escape);
+
+ // 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 2 strings
+ *
+ * @param {Error} Error with actual/expected
+ * @return {String} Diff
+ * @api private
+ */
+
+function unifiedDiff(err, escape) {
+ var indent = ' ';
+ function cleanUp(line) {
+ if (escape) {
+ line = escapeInvisibles(line);
+ }
+ if (line[0] === '+') return indent + colorLines('diff added', line);
+ if (line[0] === '-') return indent + colorLines('diff removed', line);
+ if (line.match(/\@\@/)) return null;
+ if (line.match(/\\ No newline/)) return null;
+ else return indent + line;
+ }
+ function notBlank(line) {
+ return line != null;
+ }
+ msg = diff.createPatch('string', err.actual, err.expected);
+ var lines = msg.split('\n').splice(4);
+ 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`.
*
@@ -2142,19 +2196,28 @@ function pad(str, len) {
*/
function errorDiff(err, type, escape) {
- return diff['diff' + type](err.actual, err.expected).map(function(str){
- if (escape) {
- str.value = str.value
- .replace(/\t/g, '<tab>')
- .replace(/\r/g, '<CR>')
- .replace(/\n/g, '<LF>\n');
- }
+ var actual = escape ? escapeInvisibles(err.actual) : err.actual;
+ var expected = escape ? escapeInvisibles(err.expected) : err.expected;
+ return diff['diff' + type](actual, 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('');
}
+/**
+ * Returns a string with all invisible characters in plain text
+ *
+ * @param {String} line
+ * @return {String}
+ * @api private
+ */
+function escapeInvisibles(line) {
+ return line.replace(/\t/g, '<tab>')
+ .replace(/\r/g, '<CR>')
+ .replace(/\n/g, '<LF>\n');
+}
+
/**
* Color lines for `str`, using the color `name`.
*
@@ -4402,9 +4465,7 @@ Runner.prototype.checkGlobals = function(test){
if(this.prevGlobalsLength == globals.length) return;
this.prevGlobalsLength = globals.length;
- //console.time('filterLeaksTime');
leaks = filterLeaks(ok, globals);
- //console.timeEnd('filterLeaksTime');
this._globals = this._globals.concat(leaks);
if (leaks.length > 1) {
diff --git a/test/acceptance/diffs.js b/test/acceptance/diffs.js
index 1ce02c15..7058aa06 100644
--- a/test/acceptance/diffs.js
+++ b/test/acceptance/diffs.js
@@ -4,23 +4,54 @@ var fs = require('fs')
, cssout = fs.readFileSync('test/acceptance/fixtures/css.out', 'ascii');
describe('diffs', function(){
- it('should display a word diff for small strings', function(){
- var a = 'foo bar baz'
- , b = 'foo rar baz';
+ // uncomment the assertions, and run with different params to check the output
+ // ex: --color, --no-color, --unified-diff
- // a.should.equal(b);
- })
+ it('should display a diff for small strings', function(){
+ var expected = 'foo bar baz'
+ , actual = 'foo rar baz';
- it('should display a word diff for medium strings', function(){
- var a = 'foo bar baz\nfoo bar baz\nfoo bar baz'
- , b = 'foo bar baz\nfoo rar baz\nfoo bar raz';
+ // expected.should.eql(actual);
+ });
- // a.should.equal(b);
- })
+ it('should display a diff for medium strings', function(){
+ var expected = 'foo bar baz\nfoo bar baz\nfoo bar baz'
+ , actual = 'foo bar baz\nfoo rar baz\nfoo bar raz';
+
+ // expected.should.eql(actual);
+ });
+
+ it('should display a diff for entire object dumps', function(){
+ var expected = { name: 'joe', age: 30, address: {city: 'new york', country: 'us' }}
+ , actual = { name: 'joel', age: 30, address: {city: 'new york', country: 'usa' }};
+
+ // actual.should.eql(expected);
+ });
+
+ it('should display a diff for multi-line strings', function(){
+ var expected = 'one two three\nfour five six\nseven eight nine';
+ var actual = 'one two three\nfour zzzz six\nseven eight nine';
+
+ // actual.should.eql(expected);
+ });
+
+ it('should display a diff for entire object dumps', function(){
+ var expected = { name: 'joe', age: 30, address: {city: 'new york', country: 'us' }}
+ var actual = { name: 'joel', age: 30, address: {city: 'new york', country: 'usa' }};
+
+ // actual.should.eql(expected);
+ });
+
+ it('should display a full-comparison with escaped special characters', function(){
+ var expected = 'one\ttab\ntwo\t\ttabs';
+ var actual = 'one\ttab\ntwo\t\t\ttabs';
+
+ //actual.should.equal(expected);
+ });
it('should display a word diff for large strings', function(){
// cssin.should.equal(cssout);
- })
+ });
it('should work with objects', function(){
var tobi = {
@@ -38,5 +69,5 @@ describe('diffs', function(){
};
// tobi.should.eql(loki);
- })
-})
+ });
+});