Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prettier string comparison output #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).
<!--@shields('npm')-->
[![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()`:

Expand All @@ -52,4 +54,4 @@ test('timing test', (t) => {

## License

MIT
[MIT](./LICENSE) © [axross](http://axross.me/)
76 changes: 73 additions & 3 deletions distributions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]> (http://axross.me/)",
"license": "MIT",
Expand All @@ -18,12 +20,15 @@
"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"
},
"devDependencies": {
"babel": "^5.8.34"
"babel": "^5.8.34",
"mos": "^1.3.1",
"mos-plugin-readme": "^1.0.4"
},
"repository": {
"type": "git",
Expand All @@ -40,5 +45,10 @@
"test",
"reporter",
"diff"
]
],
"mos": {
"plugins": [
"readme"
]
}
}
63 changes: 58 additions & 5 deletions sources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down