From bd422376b7d89cef0b26aec2f2f5c35c9863e8ee Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Tue, 3 May 2016 20:16:11 +0300 Subject: [PATCH 1/2] prettier string comparison output make the comarison between huges strings look better close #13 --- distributions/index.js | 76 ++++++++++++++++++++++++++++++++++++++++-- package.json | 1 + sources/index.js | 63 +++++++++++++++++++++++++++++++--- 3 files changed, 132 insertions(+), 8 deletions(-) diff --git a/distributions/index.js b/distributions/index.js index f01ff59..635a5d7 100644 --- a/distributions/index.js +++ b/distributions/index.js @@ -36,6 +36,10 @@ var _jsondiffpatch = require('jsondiffpatch'); var _jsondiffpatch2 = _interopRequireDefault(_jsondiffpatch); +var _pdiff = require('pdiff'); + +var _pdiff2 = _interopRequireDefault(_pdiff); + var INDENT = ' '; var FIG_TICK = _figures2['default'].tick; var FIG_CROSS = _figures2['default'].cross; @@ -108,6 +112,74 @@ var createReporter = function createReporter() { }); }; + function decodeNewlines(_x3) { + var _again = true; + + _function: while (_again) { + var str = _x3; + _again = false; + + if (str.match(/([^\\])\\n/g)) { + _x3 = str.replace(/([^\\])\\n/g, '$1\n'); + _again = true; + continue _function; + } + return str; + } + } + + var diffStrings = function diffStrings(actual, expected) { + var padding = ' '; + var line = 1; + var diff_ = _pdiff2['default'].addLineNumbers(_pdiff2['default'].diff(decodeNewlines(actual), decodeNewlines(expected))); + var diff = _pdiff2['default'].extractDiff(diff_, line); + var maxLine = diff_.length; + var digit = String(maxLine).length; + var spaces = ''; + for (var i = 0; i < digit - 1; i++) { + spaces += ' '; + }console.log(''); + diff.forEach(function (group, i) { + group.forEach(function (delta) { + var text = padding; + // Add line numbers + if (delta.lineNumberOfLhs != undefined) { + text += _chalk2['default'].magenta((spaces + (delta.lineNumberOfLhs + 1)).substr(-digit)); + } else { + text += spaces + _chalk2['default'].magenta('-'); + } + text += ' '; + if (delta.lineNumberOfRhs != undefined) { + text += _chalk2['default'].magenta((spaces + (delta.lineNumberOfRhs + 1)).substr(-digit)); + } else { + text += spaces + _chalk2['default'].magenta('-'); + } + text += ' '; + + // Add the value of this line + delta.values.forEach(function (value) { + if (value.added) { + text += _chalk2['default'].green.inverse(value.value); + return; + } + if (value.removed) { + text += _chalk2['default'].red.inverse(value.value); + return; + } + text += _chalk2['default'].dim(value.value); + }); + + // Ouput the delta + console.log(text); + }); + + if (i != diff.length - 1) { + console.log(padding + _chalk2['default'].dim('...')); + } + }); + console.log(''); + }; + var _assert$diag = assert.diag; var at = _assert$diag.at; var actual = _assert$diag.actual; @@ -148,9 +220,7 @@ var createReporter = function createReporter() { } else if (expected === 'undefined' && actual === 'undefined') { ; } else if (expected_type === 'string') { - var compared = (0, _diff.diffWords)(actual, expected).map(writeDiff).join(''); - - println(compared, 4); + diffStrings(actual, expected); } else { println(_chalk2['default'].red.inverse(actual) + _chalk2['default'].green.inverse(expected), 4); } diff --git a/package.json b/package.json index 4396ecf..1c974da 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "duplexer": "^0.1.1", "figures": "^1.4.0", "jsondiffpatch": "^0.1.41", + "pdiff": "^2.0.0", "pretty-ms": "^2.1.0", "tap-parser": "^1.2.2", "through2": "^2.0.0" diff --git a/sources/index.js b/sources/index.js index 6cf5c0c..8404f30 100644 --- a/sources/index.js +++ b/sources/index.js @@ -6,6 +6,7 @@ import through2 from 'through2'; import parser from 'tap-parser'; import prettyMs from 'pretty-ms'; import jsondiffpatch from 'jsondiffpatch'; +import pdiff from 'pdiff'; const INDENT = ' '; const FIG_TICK = figures.tick; @@ -64,6 +65,62 @@ const createReporter = () => { return value.replace(/(^\s*)(.*)/g, (m, one, two) => one + style(two)) }; + function decodeNewlines(str) { + if (str.match(/([^\\])\\n/g)) return decodeNewlines(str.replace(/([^\\])\\n/g, '$1\n')); + return str; + } + + const diffStrings = (actual, expected) => { + const padding = ' '; + const line = 1; + const diff_ = pdiff.addLineNumbers(pdiff.diff(decodeNewlines(actual), decodeNewlines(expected))); + const diff = pdiff.extractDiff(diff_, line); + const maxLine = diff_.length; + const digit = String(maxLine).length; + let spaces = ''; + for (let i = 0; i < digit - 1; i++) spaces += ' '; + console.log(''); + diff.forEach((group, i) => { + group.forEach(delta => { + let text = padding; + // Add line numbers + if (delta.lineNumberOfLhs != undefined) { + text += chalk.magenta((spaces + (delta.lineNumberOfLhs + 1)).substr(-digit)); + } else { + text += spaces + chalk.magenta('-'); + } + text += ' '; + if (delta.lineNumberOfRhs != undefined) { + text += chalk.magenta((spaces + (delta.lineNumberOfRhs + 1)).substr(-digit)); + } else { + text += spaces + chalk.magenta('-'); + } + text += ' '; + + // Add the value of this line + delta.values.forEach(value => { + if (value.added) { + text += chalk.green.inverse(value.value); + return; + } + if (value.removed) { + text += chalk.red.inverse(value.value); + return; + } + text += chalk.dim(value.value); + }) + + // Ouput the delta + console.log(text); + }); + + if (i != diff.length - 1) { + console.log(padding + chalk.dim('...')); + } + }); + console.log(''); + }; + let { at, actual, @@ -108,11 +165,7 @@ const createReporter = () => { } else if (expected === 'undefined' && actual === 'undefined') { ; } else if (expected_type === 'string') { - const compared = diffWords(actual, expected) - .map(writeDiff) - .join(''); - - println(compared, 4); + diffStrings(actual, expected) } else { println( chalk.red.inverse(actual) + chalk.green.inverse(expected), From 6357bc0196113128e462bb6e878ee18fbf660496 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Wed, 4 May 2016 22:09:56 +0300 Subject: [PATCH 2/2] enhance readme --- README.md | 24 +++++++++++++----------- package.json | 15 ++++++++++++--- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index c67ec49..b1a0619 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,33 @@ # tap-diff -[![npm version](https://badge.fury.io/js/tap-diff.svg)](http://badge.fury.io/js/tap-diff) +> The most human-friendly TAP reporter -The most human-friendly [TAP reporter](https://github.com/substack/tape#pretty-reporters). + +[![npm version](https://img.shields.io/npm/v/tap-diff.svg)](https://www.npmjs.com/package/tap-diff) + ![Screenshot](screenshot1.png) ![Screenshot](screenshot2.png) +## Installation + +```sh +npm install --global tap-diff +``` + ## How to use You can use tap-diff in the same way as other [TAP reporters](https://github.com/substack/tape#pretty-reporters). -``` -npm install -g tap-diff -``` - -``` +```sh tape ./*.test.js | tap-diff ``` tap-diff uses [chalk](https://www.npmjs.com/package/chalk) for adding color, which automatically detects color terminals. If you're piping the output and want to force color: -``` -FORCE_COLOR=t tape ./*.test.js | tap-diff -``` + FORCE_COLOR=t tape ./*.test.js | tap-diff Or use with `createStream()`: @@ -52,4 +54,4 @@ test('timing test', (t) => { ## License -MIT +[MIT](./LICENSE) © [axross](http://axross.me/) diff --git a/package.json b/package.json index 1c974da..e518eab 100644 --- a/package.json +++ b/package.json @@ -3,12 +3,14 @@ "version": "0.1.1", "description": "The most human-friendly TAP reporter", "main": "./distributions/index.js", + "preferGlobal": true, "bin": { "tap-diff": "./distributions/cli.js" }, "scripts": { "babel": "babel ./sources --out-dir ./distributions", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mos test", + "md": "mos" }, "author": "axross (http://axross.me/)", "license": "MIT", @@ -24,7 +26,9 @@ "through2": "^2.0.0" }, "devDependencies": { - "babel": "^5.8.34" + "babel": "^5.8.34", + "mos": "^1.3.1", + "mos-plugin-readme": "^1.0.4" }, "repository": { "type": "git", @@ -41,5 +45,10 @@ "test", "reporter", "diff" - ] + ], + "mos": { + "plugins": [ + "readme" + ] + } }