improve diff table output

This commit is contained in:
Emil Tin 2017-02-07 14:10:26 +01:00 committed by Patrick Niklaus
parent 2a5ebf84bc
commit ef4d32a492

View File

@ -17,73 +17,107 @@ String.prototype.padRight = function(char, length) {
module.exports = function (expected, actual) { module.exports = function (expected, actual) {
let headers = expected.raw()[0]; let headers = expected.raw()[0];
let diff = []; let expectedRows = expected.hashes();
let tableError = false; let tableError = false;
let statusRows = [];
let columnStatus = {}
expected.hashes().forEach((expectedRow, i) => { expectedRows.forEach((expectedRow, i) => {
var rowError = false; var rowError = false;
statusRows[i] = {};
var statusRow = statusRows[i];
for (var key in expectedRow) { for (var key in expectedRow) {
var actualRow = actual[i] var actualRow = actual[i]
var row var row
if (unescapeStr(expectedRow[key]) != actualRow[key]) { if (unescapeStr(expectedRow[key]) != actualRow[key]) {
statusRow[key] = false;
row = Object.assign({}, expectedRow, {diff_status: 'expected'})
diff.push(row);
row = Object.assign({}, actualRow, {diff_status: 'actual'})
diff.push(row);
tableError = true; tableError = true;
rowError = true; columnStatus[key] = false;
break;
} }
} }
if( !rowError ) diff.push(expectedRow); });
if (!tableError) return null;
// determine column widths
var widths = {};
var wantStr = '(-) ';
var gotStr = '(+) ';
var okStr = ' ';
headers.forEach( (key) => {
widths[key] = key.length;
}); });
if (!tableError) return null; expectedRows.forEach((row,i) => {
// 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) => {
var cells = [] var cells = []
headers.forEach( (key,i) => { headers.forEach( (key) => {
var s = row[key] var content = row[key]
var length = s.length; var length = content.length;
if(widths[i]==null || length > widths[i]) if(widths[key]==null || length > widths[key])
widths[i] = length; widths[key] = length;
}); });
}); });
// format // format
var lines = ['Tables were not identical:']; var lines = [chalk.red('Tables were not identical:')];
diff.forEach((row) => { var cells;
var cells = []
headers.forEach( (key,i) => { // header row
var s cells = []
if( row[key] ) headers.forEach( (key) => {
s = row[key].padRight(' ', widths[i] ); var content = key.padRight(' ', widths[key] );
else if (columnStatus[key] == false )
s = ' '.padRight(' ', widths[i] ); content = okStr + content;
cells.push( chalk.white( content ) );
if(row.diff_status == 'expected') });
cells.push( chalk.yellow('(-) ' + s) ); lines.push( '| ' + cells.join(' | ') + ' |');
else if(row.diff_status == 'actual')
cells.push( chalk.red( '(+) ' + s) ); // content rows
else expectedRows.forEach((row,i) => {
cells.push( chalk.green( ' ' + s) ); var cells;
var rowError = Object.keys(statusRows[i]).length > 0;
// expected row
cells = []
headers.forEach( (key) => {
var content = row[key].padRight(' ', widths[key] );
if (statusRows[i][key] == false)
cells.push( chalk.yellow( wantStr + content) );
else {
if (rowError) {
if (columnStatus[key]==false)
content = okStr + content
cells.push( chalk.yellow( content) );
}
else {
if (columnStatus[key]==false)
content = okStr + content
cells.push( chalk.green( content) );
}
}
}); });
lines.push('| ' + cells.join(' | ') + ' |'); lines.push('| ' + cells.join(' | ') + ' |');
// if error in row, insert extra row showing actual result
if (rowError) {
cells = []
headers.forEach( (key) => {
var content = actual[i][key].padRight(' ', widths[key] );
if (statusRows[i][key] == false)
cells.push( chalk.red( gotStr + content) );
else {
if (columnStatus[key]==false)
cells.push( chalk.red( okStr + content) );
else
cells.push( chalk.red( content) );
}
});
lines.push('| ' + cells.join(' | ') + ' |');
}
}); });
return lines.join('\n'); return lines.join('\n');
}; };