align cucumber diff tables

This commit is contained in:
Emil Tin 2017-02-07 10:24:31 +01:00 committed by Patrick Niklaus
parent 543f0e5e44
commit ddb60c34e6
2 changed files with 65 additions and 30 deletions

View File

@ -7,48 +7,83 @@ var chalk = require('chalk');
var unescapeStr = (str) => str.replace(/\\\|/g, '\|').replace(/\\\\/g, '\\'); var unescapeStr = (str) => str.replace(/\\\|/g, '\|').replace(/\\\\/g, '\\');
String.prototype.padLeft = function(char, length) {
return char.repeat(Math.max(0, length - this.length)) + this;
}
String.prototype.padRight = function(char, length) {
return this + char.repeat(Math.max(0, length - this.length));
}
module.exports = function (expected, actual) { module.exports = function (expected, actual) {
let headers = expected.raw()[0]; let headers = expected.raw()[0];
let expected_keys = expected.hashes();
let diff = []; let diff = [];
let hasErrors = false; let tableError = false;
var good = 0, bad = 0; expected.hashes().forEach((expectedRow, i) => {
expected_keys.forEach((row, i) => {
var rowError = false; var rowError = false;
for (var j in row) { for (var key in expectedRow) {
if (unescapeStr(row[j]) != actual[i][j]) { var actualRow = actual[i]
var row
if (unescapeStr(expectedRow[key]) != actualRow[key]) {
row = Object.assign({}, expectedRow, {diff_status: 'expected'})
diff.push(row);
row = Object.assign({}, actualRow, {diff_status: 'actual'})
diff.push(row);
tableError = true;
rowError = true; rowError = true;
hasErrors = true;
break; break;
} }
} }
if( !rowError ) diff.push(expectedRow);
if (rowError) {
bad++;
diff.push(Object.assign({}, row, {c_status: 'undefined'}));
diff.push(Object.assign({}, actual[i], {c_status: 'comment'}));
} else {
good++;
diff.push(row);
}
}); });
if (!hasErrors) return null; if (!tableError) return null;
var s = ['Tables were not identical:'];
s.push(headers.map(key => ' ' + key).join(' | ')); // insert a hash of headers where key=value so we can treat
// the header row like other rows in the processing below
var header_hash = {}
headers.forEach( (key,i) => {
header_hash[key] = key;
});
diff.unshift( header_hash )
// determine column widths
var widths = [];
diff.forEach((row) => { diff.forEach((row) => {
var rowString = '| '; var cells = []
headers.forEach((header) => { headers.forEach( (key,i) => {
if (!row.c_status) rowString += chalk.green(' ' + row[header] + ' | '); var s = row[key]
else if (row.c_status === 'undefined') rowString += chalk.yellow('(-) ' + row[header] + ' | '); var length = s.length;
else rowString += chalk.red('(+) ' + row[header] + ' | '); if(widths[i]==null || length > widths[i])
widths[i] = length;
}); });
s.push(rowString);
}); });
return s.join('\n'); // format
var lines = ['Tables were not identical:'];
diff.forEach((row) => {
var cells = []
headers.forEach( (key,i) => {
var s
if( row[key] )
s = row[key].padRight(' ', widths[i] );
else
s = ' '.padRight(' ', widths[i] );
if(row.diff_status == 'expected')
cells.push( chalk.yellow('(-) ' + s) );
else if(row.diff_status == 'actual')
cells.push( chalk.red( '(+) ' + s) );
else
cells.push( chalk.green( ' ' + s) );
});
lines.push('| ' + cells.join(' | ') + ' |');
});
return lines.join('\n');
}; };

View File

@ -63,7 +63,7 @@ module.exports = function () {
} }
if (this.FuzzyMatch.match(outputRow[direction], want)) { if (this.FuzzyMatch.match(outputRow[direction], want)) {
outputRow[direction] = [usingShortcut ? usingShortcut : row[direction]]; outputRow[direction] = usingShortcut ? usingShortcut : row[direction];
} }
}); });