Skip to content

Commit 1b77212

Browse files
committed
Print data tables in pretty formatter output (close #89)
1 parent efaec18 commit 1b77212

File tree

2 files changed

+124
-2
lines changed

2 files changed

+124
-2
lines changed

lib/cucumber/listener/pretty_formatter.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ var PrettyFormatter = function(options) {
3030
callback();
3131
};
3232

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

39+
if (step.hasDataTable()) {
40+
var dataTable = step.getDataTable();
41+
self.logDataTable(dataTable);
42+
}
43+
3944
stepResult.isFailed();
4045
if (stepResult.isFailed()) {
4146
var failure = stepResult.getFailureException();
@@ -52,6 +57,29 @@ var PrettyFormatter = function(options) {
5257
callback();
5358
};
5459

60+
self.logDataTable = function logDataTable(dataTable) {
61+
var rows = dataTable.raw();
62+
};
63+
64+
self.logDataTable = function logDataTable(dataTable) {
65+
var rows = dataTable.raw();
66+
var columnWidths = self._determineColumnWidthsFromRows(rows);
67+
var rowCount = rows.length;
68+
var columnCount = columnWidths.length;
69+
70+
for (var rowIndex = 0; rowIndex < rowCount; rowIndex++) {
71+
var cells = rows[rowIndex];
72+
var line = "|";
73+
for (var columnIndex = 0; columnIndex < columnCount; columnIndex++) {
74+
var cell = cells[columnIndex];
75+
var columnWidth = columnWidths[columnIndex];
76+
line += " " + self._pad(cell, columnWidth) + " |"
77+
}
78+
line += "\n";
79+
self.logIndented(line, 3);
80+
}
81+
};
82+
5583
self.logIndented = function logIndented(text, level) {
5684
var indented = self.indent(text, level);
5785
self.log(indented);
@@ -67,6 +95,32 @@ var PrettyFormatter = function(options) {
6795
return indented;
6896
};
6997

98+
self._determineColumnWidthsFromRows = function _determineColumnWidthsFromRows(rows) {
99+
var columnWidths = [];
100+
var currentColumn;
101+
102+
rows.forEach(function (cells) {
103+
currentColumn = 0;
104+
cells.forEach(function (cell) {
105+
var currentColumnWidth = columnWidths[currentColumn];
106+
var currentCellWidth = cell.length;
107+
if (typeof currentColumnWidth == "undefined" || currentColumnWidth < currentCellWidth)
108+
columnWidths[currentColumn] = currentCellWidth;
109+
currentColumn += 1;
110+
});
111+
});
112+
113+
return columnWidths;
114+
};
115+
116+
self._pad = function _pad(text, width) {
117+
var padded = "" + text;
118+
while (padded.length < width) {
119+
padded += " ";
120+
}
121+
return padded;
122+
};
123+
70124
return self;
71125
};
72126
module.exports = PrettyFormatter;

spec/cucumber/listener/pretty_formatter_spec.js

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,10 @@ describe("Cucumber.Listener.PrettyFormatter", function () {
161161
beforeEach(function () {
162162
keyword = "step-keyword ";
163163
name = "step-name";
164-
step = createSpyWithStubs("step", { getKeyword: keyword, getName: name });
164+
step = createSpyWithStubs("step", { getKeyword: keyword, hasDataTable: null, getDataTable: null, getName: name });
165165
stepResult = createSpyWithStubs("step result", { getStep: step, isFailed: null });
166166
event = createSpyWithStubs("event", { getPayloadItem: stepResult });
167+
spyOn(prettyFormatter, 'logDataTable');
167168
spyOn(prettyFormatter, 'logIndented');
168169
callback = createSpy("callback");
169170
});
@@ -194,6 +195,47 @@ describe("Cucumber.Listener.PrettyFormatter", function () {
194195
expect(prettyFormatter.logIndented).toHaveBeenCalledWith(text, 2);
195196
});
196197

198+
it("checks whether the step result has a data table or not", function () {
199+
prettyFormatter.handleStepResultEvent(event, callback);
200+
expect(step.hasDataTable).toHaveBeenCalled();
201+
});
202+
203+
describe("when the step has a data table", function () {
204+
var dataTable;
205+
206+
beforeEach(function () {
207+
dataTable = createSpy("data table");
208+
step.hasDataTable.andReturn(true);
209+
step.getDataTable.andReturn(dataTable);
210+
});
211+
212+
it("gets the data table", function () {
213+
prettyFormatter.handleStepResultEvent(event, callback);
214+
expect(step.getDataTable).toHaveBeenCalled();
215+
});
216+
217+
it("logs the data table", function () {
218+
prettyFormatter.handleStepResultEvent(event, callback);
219+
expect(prettyFormatter.logDataTable).toHaveBeenCalledWith(dataTable);
220+
});
221+
});
222+
223+
describe("when the step has no data table", function () {
224+
beforeEach(function () {
225+
step.hasDataTable.andReturn(false);
226+
});
227+
228+
it("does no get the data table", function () {
229+
prettyFormatter.handleStepResultEvent(event, callback);
230+
expect(step.getDataTable).not.toHaveBeenCalled();
231+
});
232+
233+
it("does not log the data table", function () {
234+
prettyFormatter.handleStepResultEvent(event, callback);
235+
expect(prettyFormatter.logDataTable).not.toHaveBeenCalled();
236+
});
237+
});
238+
197239
it("checks whether the step result is failed or not", function () {
198240
prettyFormatter.handleStepResultEvent(event, callback);
199241
expect(stepResult.isFailed).toHaveBeenCalled();
@@ -267,6 +309,32 @@ describe("Cucumber.Listener.PrettyFormatter", function () {
267309
});
268310
});
269311

312+
describe("logDataTable()", function () {
313+
var dataTable, rows;
314+
315+
beforeEach(function () {
316+
rows = [
317+
["cuk", "cuke", "cukejs"],
318+
["c", "cuke", "cuke.js"],
319+
["cu", "cuke", "cucumber"]
320+
];
321+
dataTable = createSpyWithStubs("data table", {raw: rows});
322+
spyOn(prettyFormatter, "logIndented");
323+
});
324+
325+
it("gets the rows from the table", function () {
326+
prettyFormatter.logDataTable(dataTable);
327+
expect(dataTable.raw).toHaveBeenCalled();
328+
});
329+
330+
it("logs the lines with padding, indented by 3 levels", function () {
331+
prettyFormatter.logDataTable(dataTable);
332+
expect(prettyFormatter.logIndented).toHaveBeenCalledWith("| cuk | cuke | cukejs |\n", 3);
333+
expect(prettyFormatter.logIndented).toHaveBeenCalledWith("| c | cuke | cuke.js |\n", 3);
334+
expect(prettyFormatter.logIndented).toHaveBeenCalledWith("| cu | cuke | cucumber |\n", 3);
335+
});
336+
});
337+
270338
describe("logIndented()", function () {
271339
var text, level, indented;
272340

0 commit comments

Comments
 (0)