Skip to content

Commit

Permalink
Print data tables in pretty formatter output (close #89)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbpros committed Oct 6, 2012
1 parent efaec18 commit 1b77212
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 2 deletions.
56 changes: 55 additions & 1 deletion lib/cucumber/listener/pretty_formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ var PrettyFormatter = function(options) {
callback();
};

self.handleStepResultEvent = function handleStepResult(event, callback) {
self.handleStepResultEvent = function handleStepResultEvent(event, callback) {
var stepResult = event.getPayloadItem('stepResult');
var step = stepResult.getStep();
var source = step.getKeyword() + step.getName() + "\n";
self.logIndented(source, 2);

if (step.hasDataTable()) {
var dataTable = step.getDataTable();
self.logDataTable(dataTable);
}

stepResult.isFailed();
if (stepResult.isFailed()) {
var failure = stepResult.getFailureException();
Expand All @@ -52,6 +57,29 @@ var PrettyFormatter = function(options) {
callback();
};

self.logDataTable = function logDataTable(dataTable) {
var rows = dataTable.raw();
};

self.logDataTable = function logDataTable(dataTable) {
var rows = dataTable.raw();
var columnWidths = self._determineColumnWidthsFromRows(rows);
var rowCount = rows.length;
var columnCount = columnWidths.length;

for (var rowIndex = 0; rowIndex < rowCount; rowIndex++) {
var cells = rows[rowIndex];
var line = "|";
for (var columnIndex = 0; columnIndex < columnCount; columnIndex++) {
var cell = cells[columnIndex];
var columnWidth = columnWidths[columnIndex];
line += " " + self._pad(cell, columnWidth) + " |"
}
line += "\n";
self.logIndented(line, 3);
}
};

self.logIndented = function logIndented(text, level) {
var indented = self.indent(text, level);
self.log(indented);
Expand All @@ -67,6 +95,32 @@ var PrettyFormatter = function(options) {
return indented;
};

self._determineColumnWidthsFromRows = function _determineColumnWidthsFromRows(rows) {
var columnWidths = [];
var currentColumn;

rows.forEach(function (cells) {
currentColumn = 0;
cells.forEach(function (cell) {
var currentColumnWidth = columnWidths[currentColumn];
var currentCellWidth = cell.length;
if (typeof currentColumnWidth == "undefined" || currentColumnWidth < currentCellWidth)
columnWidths[currentColumn] = currentCellWidth;
currentColumn += 1;
});
});

return columnWidths;
};

self._pad = function _pad(text, width) {
var padded = "" + text;
while (padded.length < width) {
padded += " ";
}
return padded;
};

return self;
};
module.exports = PrettyFormatter;
70 changes: 69 additions & 1 deletion spec/cucumber/listener/pretty_formatter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ describe("Cucumber.Listener.PrettyFormatter", function () {
beforeEach(function () {
keyword = "step-keyword ";
name = "step-name";
step = createSpyWithStubs("step", { getKeyword: keyword, getName: name });
step = createSpyWithStubs("step", { getKeyword: keyword, hasDataTable: null, getDataTable: null, getName: name });
stepResult = createSpyWithStubs("step result", { getStep: step, isFailed: null });
event = createSpyWithStubs("event", { getPayloadItem: stepResult });
spyOn(prettyFormatter, 'logDataTable');
spyOn(prettyFormatter, 'logIndented');
callback = createSpy("callback");
});
Expand Down Expand Up @@ -194,6 +195,47 @@ describe("Cucumber.Listener.PrettyFormatter", function () {
expect(prettyFormatter.logIndented).toHaveBeenCalledWith(text, 2);
});

it("checks whether the step result has a data table or not", function () {
prettyFormatter.handleStepResultEvent(event, callback);
expect(step.hasDataTable).toHaveBeenCalled();
});

describe("when the step has a data table", function () {
var dataTable;

beforeEach(function () {
dataTable = createSpy("data table");
step.hasDataTable.andReturn(true);
step.getDataTable.andReturn(dataTable);
});

it("gets the data table", function () {
prettyFormatter.handleStepResultEvent(event, callback);
expect(step.getDataTable).toHaveBeenCalled();
});

it("logs the data table", function () {
prettyFormatter.handleStepResultEvent(event, callback);
expect(prettyFormatter.logDataTable).toHaveBeenCalledWith(dataTable);
});
});

describe("when the step has no data table", function () {
beforeEach(function () {
step.hasDataTable.andReturn(false);
});

it("does no get the data table", function () {
prettyFormatter.handleStepResultEvent(event, callback);
expect(step.getDataTable).not.toHaveBeenCalled();
});

it("does not log the data table", function () {
prettyFormatter.handleStepResultEvent(event, callback);
expect(prettyFormatter.logDataTable).not.toHaveBeenCalled();
});
});

it("checks whether the step result is failed or not", function () {
prettyFormatter.handleStepResultEvent(event, callback);
expect(stepResult.isFailed).toHaveBeenCalled();
Expand Down Expand Up @@ -267,6 +309,32 @@ describe("Cucumber.Listener.PrettyFormatter", function () {
});
});

describe("logDataTable()", function () {
var dataTable, rows;

beforeEach(function () {
rows = [
["cuk", "cuke", "cukejs"],
["c", "cuke", "cuke.js"],
["cu", "cuke", "cucumber"]
];
dataTable = createSpyWithStubs("data table", {raw: rows});
spyOn(prettyFormatter, "logIndented");
});

it("gets the rows from the table", function () {
prettyFormatter.logDataTable(dataTable);
expect(dataTable.raw).toHaveBeenCalled();
});

it("logs the lines with padding, indented by 3 levels", function () {
prettyFormatter.logDataTable(dataTable);
expect(prettyFormatter.logIndented).toHaveBeenCalledWith("| cuk | cuke | cukejs |\n", 3);
expect(prettyFormatter.logIndented).toHaveBeenCalledWith("| c | cuke | cuke.js |\n", 3);
expect(prettyFormatter.logIndented).toHaveBeenCalledWith("| cu | cuke | cucumber |\n", 3);
});
});

describe("logIndented()", function () {
var text, level, indented;

Expand Down

0 comments on commit 1b77212

Please sign in to comment.