Skip to content

Commit 5b45be4

Browse files
committed
Extract common formatter methods (#59, #63)
1 parent bf89e7b commit 5b45be4

File tree

5 files changed

+141
-121
lines changed

5 files changed

+141
-121
lines changed

lib/cucumber/listener.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var Listener = function () {
3333
Listener.EVENT_HANDLER_NAME_PREFIX = 'handle';
3434
Listener.EVENT_HANDLER_NAME_SUFFIX = 'Event';
3535

36+
Listener.Formatter = require('./listener/formatter');
3637
Listener.ProgressFormatter = require('./listener/progress_formatter');
3738
Listener.StatsJournal = require('./listener/stats_journal');
3839
Listener.Summarizer = require('./listener/summarizer');

lib/cucumber/listener/formatter.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var Formatter = function (options) {
2+
var Cucumber = require('../../cucumber');
3+
4+
if (!options)
5+
options = {};
6+
if (options['logToConsole'] == undefined)
7+
options['logToConsole'] = true;
8+
9+
var logs = "";
10+
11+
var self = Cucumber.Listener();
12+
13+
self.log = function log(string) {
14+
logs += string;
15+
if (options['logToConsole'])
16+
process.stdout.write(string);
17+
if (typeof(options['logToFunction']) == 'function')
18+
options['logToFunction'](string);
19+
};
20+
21+
self.getLogs = function getLogs() {
22+
return logs;
23+
};
24+
25+
return self;
26+
};
27+
module.exports = Formatter;

lib/cucumber/listener/progress_formatter.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
var ProgressFormatter = function(options) {
22
var Cucumber = require('../../cucumber');
33

4-
var logs = "";
5-
64
if (!options)
75
options = {};
8-
if (options['logToConsole'] == undefined)
9-
options['logToConsole'] = true;
106

11-
var self = Cucumber.Listener();
7+
var self = Cucumber.Listener.Formatter(options);
128
var summaryLogger = Cucumber.Listener.Summarizer();
139

1410
var parentHear = self.hear;
@@ -18,18 +14,6 @@ var ProgressFormatter = function(options) {
1814
});
1915
};
2016

21-
self.log = function log(string) {
22-
logs += string;
23-
if (options['logToConsole'])
24-
process.stdout.write(string);
25-
if (typeof(options['logToFunction']) == 'function')
26-
options['logToFunction'](string);
27-
};
28-
29-
self.getLogs = function getLogs() {
30-
return logs;
31-
};
32-
3317
self.handleStepResultEvent = function handleStepResult(event, callback) {
3418
var stepResult = event.getPayloadItem('stepResult');
3519
if (stepResult.isSuccessful())
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
require('../../support/spec_helper');
2+
3+
describe("Cucumber.Listener.Formatter", function () {
4+
var Cucumber = requireLib('cucumber');
5+
var formatter, listener;
6+
7+
beforeEach(function () {
8+
var Formatter = Cucumber.Listener.Formatter;
9+
listener = createSpy("listener");
10+
spyOn(Cucumber, 'Listener').andReturn(listener);
11+
Cucumber.Listener.Formatter = Formatter;
12+
formatter = Cucumber.Listener.Formatter();
13+
});
14+
15+
describe("constructor", function () {
16+
it("creates a listener", function () {
17+
expect(Cucumber.Listener).toHaveBeenCalled();
18+
});
19+
20+
it("extends the formatter", function () {
21+
expect(formatter).toBe(listener);
22+
});
23+
});
24+
25+
describe("log()", function () {
26+
var logged, alsoLogged, loggedBuffer;
27+
28+
beforeEach(function () {
29+
logged = "this was logged";
30+
alsoLogged = "this was also logged";
31+
loggedBuffer = logged + alsoLogged;
32+
spyOn(process.stdout, 'write');
33+
});
34+
35+
it("records logged strings", function () {
36+
formatter.log(logged);
37+
formatter.log(alsoLogged);
38+
expect(formatter.getLogs()).toBe(loggedBuffer);
39+
});
40+
41+
it("outputs the logged string to STDOUT by default", function () {
42+
formatter.log(logged);
43+
expect(process.stdout.write).toHaveBeenCalledWith(logged);
44+
});
45+
46+
describe("when asked to output to STDOUT", function () {
47+
beforeEach(function () {
48+
formatter = Cucumber.Listener.Formatter({logToConsole: true});
49+
});
50+
51+
it("outputs the logged string to STDOUT", function () {
52+
formatter.log(logged);
53+
expect(process.stdout.write).toHaveBeenCalledWith(logged);
54+
});
55+
});
56+
57+
describe("when asked to not output to STDOUT", function () {
58+
beforeEach(function () {
59+
formatter = Cucumber.Listener.Formatter({logToConsole: false});
60+
});
61+
62+
it("does not output anything to STDOUT", function () {
63+
formatter.log(logged);
64+
expect(process.stdout.write).not.toHaveBeenCalledWith(logged);
65+
});
66+
});
67+
68+
describe("when asked to output to a function", function () {
69+
var userFunction;
70+
71+
beforeEach(function () {
72+
userFunction = createSpy("output user function");
73+
formatter = Cucumber.Listener.Formatter({logToFunction: userFunction});
74+
});
75+
76+
it("calls the function with the logged string", function () {
77+
formatter.log(logged);
78+
expect(userFunction).toHaveBeenCalledWith(logged);
79+
});
80+
});
81+
});
82+
83+
describe("getLogs()", function () {
84+
it("returns the logged buffer", function () {
85+
var logged = "this was logged";
86+
var alsoLogged = "this was also logged";
87+
var loggedBuffer = logged + alsoLogged;
88+
spyOn(process.stdout, 'write'); // prevent actual output during spec execution
89+
formatter.log(logged);
90+
formatter.log(alsoLogged);
91+
expect(formatter.getLogs()).toBe(loggedBuffer);
92+
});
93+
94+
it("returns an empty string when the progress formatter did not log anything yet", function () {
95+
expect(formatter.getLogs()).toBe("");
96+
});
97+
});
98+
});

spec/cucumber/listener/progress_formatter_spec.js

Lines changed: 14 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@ require('../../support/spec_helper');
22

33
describe("Cucumber.Listener.ProgressFormatter", function () {
44
var Cucumber = requireLib('cucumber');
5-
var listener, listenerHearMethod, summarizer, progressFormatter;
5+
var formatter, formatterHearMethod, summarizer, progressFormatter, options;
66

77
beforeEach(function () {
8-
var ProgressFormatter = Cucumber.Listener.ProgressFormatter;
9-
listener = createSpy("listener");
10-
listenerHearMethod = spyOnStub(listener, 'hear');
11-
summarizer = createSpy("summarizer");
12-
spyOn(Cucumber, 'Listener').andReturn(listener);
13-
spyOnStub(Cucumber.Listener, 'Summarizer').andReturn(summarizer);
14-
Cucumber.Listener.ProgressFormatter = ProgressFormatter;
15-
progressFormatter = Cucumber.Listener.ProgressFormatter();
8+
options = createSpy(options);
9+
formatter = createSpyWithStubs("formatter", {log: null});
10+
formatterHearMethod = spyOnStub(formatter, 'hear');
11+
summarizer = createSpy("summarizer");
12+
spyOn(Cucumber.Listener, 'Formatter').andReturn(formatter);
13+
spyOn(Cucumber.Listener, 'Summarizer').andReturn(summarizer);
14+
progressFormatter = Cucumber.Listener.ProgressFormatter(options);
1615
});
1716

1817
describe("constructor", function () {
19-
it("creates a listener", function() {
20-
expect(Cucumber.Listener).toHaveBeenCalled();
18+
it("creates a formatter", function() {
19+
expect(Cucumber.Listener.Formatter).toHaveBeenCalledWith(options);
2120
});
2221

23-
it("extends the listener", function () {
24-
expect(progressFormatter).toBe(listener);
22+
it("extends the formatter", function () {
23+
expect(progressFormatter).toBe(formatter);
2524
});
2625

2726
it("creates a summarizer", function () {
@@ -53,87 +52,13 @@ describe("Cucumber.Listener.ProgressFormatter", function () {
5352
summarizerCallback = summarizer.hear.mostRecentCall.args[1];
5453
});
5554

56-
it("tells the listener to listen to the event", function () {
55+
it("tells the formatter to listen to the event", function () {
5756
summarizerCallback();
58-
expect(listenerHearMethod).toHaveBeenCalledWith(event, callback);
57+
expect(formatterHearMethod).toHaveBeenCalledWith(event, callback);
5958
});
6059
});
6160
});
6261

63-
describe("log()", function () {
64-
var logged, alsoLogged, loggedBuffer;
65-
66-
beforeEach(function () {
67-
logged = "this was logged";
68-
alsoLogged = "this was also logged";
69-
loggedBuffer = logged + alsoLogged;
70-
spyOn(process.stdout, 'write');
71-
});
72-
73-
it("records logged strings", function () {
74-
progressFormatter.log(logged);
75-
progressFormatter.log(alsoLogged);
76-
expect(progressFormatter.getLogs()).toBe(loggedBuffer);
77-
});
78-
79-
it("outputs the logged string to STDOUT by default", function () {
80-
progressFormatter.log(logged);
81-
expect(process.stdout.write).toHaveBeenCalledWith(logged);
82-
});
83-
84-
describe("when asked to output to STDOUT", function () {
85-
beforeEach(function () {
86-
progressFormatter = Cucumber.Listener.ProgressFormatter({logToConsole: true});
87-
});
88-
89-
it("outputs the logged string to STDOUT", function () {
90-
progressFormatter.log(logged);
91-
expect(process.stdout.write).toHaveBeenCalledWith(logged);
92-
});
93-
});
94-
95-
describe("when asked to not output to STDOUT", function () {
96-
beforeEach(function () {
97-
progressFormatter = Cucumber.Listener.ProgressFormatter({logToConsole: false});
98-
});
99-
100-
it("does not output anything to STDOUT", function () {
101-
progressFormatter.log(logged);
102-
expect(process.stdout.write).not.toHaveBeenCalledWith(logged);
103-
});
104-
});
105-
106-
describe("when asked to output to a function", function () {
107-
var userFunction;
108-
109-
beforeEach(function () {
110-
userFunction = createSpy("output user function");
111-
progressFormatter = Cucumber.Listener.ProgressFormatter({logToFunction: userFunction});
112-
});
113-
114-
it("calls the function with the logged string", function () {
115-
progressFormatter.log(logged);
116-
expect(userFunction).toHaveBeenCalledWith(logged);
117-
});
118-
});
119-
});
120-
121-
describe("getLogs()", function () {
122-
it("returns the logged buffer", function () {
123-
var logged = "this was logged";
124-
var alsoLogged = "this was also logged";
125-
var loggedBuffer = logged + alsoLogged;
126-
spyOn(process.stdout, 'write'); // prevent actual output during spec execution
127-
progressFormatter.log(logged);
128-
progressFormatter.log(alsoLogged);
129-
expect(progressFormatter.getLogs()).toBe(loggedBuffer);
130-
});
131-
132-
it("returns an empty string when the progress formatter did not log anything yet", function () {
133-
expect(progressFormatter.getLogs()).toBe("");
134-
});
135-
});
136-
13762
describe("handleStepResultEvent()", function () {
13863
var event, callback, stepResult;
13964

@@ -283,32 +208,20 @@ describe("Cucumber.Listener.ProgressFormatter", function () {
283208
});
284209

285210
describe("handleSuccessfulStepResult()", function () {
286-
beforeEach(function () {
287-
spyOn(progressFormatter, 'log');
288-
});
289-
290211
it("logs the passing step character", function () {
291212
progressFormatter.handleSuccessfulStepResult();
292213
expect(progressFormatter.log).toHaveBeenCalledWith(Cucumber.Listener.ProgressFormatter.PASSED_STEP_CHARACTER);
293214
});
294215
});
295216

296217
describe("handlePendingStepResult()", function () {
297-
beforeEach(function () {
298-
spyOn(progressFormatter, 'log')
299-
});
300-
301218
it("logs the pending step character", function () {
302219
progressFormatter.handlePendingStepResult();
303220
expect(progressFormatter.log).toHaveBeenCalledWith(Cucumber.Listener.ProgressFormatter.PENDING_STEP_CHARACTER);
304221
});
305222
});
306223

307224
describe("handleSkippedStepResult()", function () {
308-
beforeEach(function () {
309-
spyOn(progressFormatter, 'log');
310-
});
311-
312225
it("logs the skipped step character", function () {
313226
progressFormatter.handleSkippedStepResult();
314227
expect(progressFormatter.log).toHaveBeenCalledWith(Cucumber.Listener.ProgressFormatter.SKIPPED_STEP_CHARACTER);
@@ -320,7 +233,6 @@ describe("Cucumber.Listener.ProgressFormatter", function () {
320233

321234
beforeEach(function () {
322235
step = createSpy("step");
323-
spyOn(progressFormatter, 'log');
324236
});
325237

326238
it("logs the undefined step character", function () {
@@ -334,7 +246,6 @@ describe("Cucumber.Listener.ProgressFormatter", function () {
334246

335247
beforeEach(function () {
336248
stepResult = createSpy("failed step result");
337-
spyOn(progressFormatter, 'log');
338249
});
339250

340251
it("logs the failed step character", function () {
@@ -351,7 +262,6 @@ describe("Cucumber.Listener.ProgressFormatter", function () {
351262
callback = createSpy("callback");
352263
summaryLogs = createSpy("summary logs");
353264
spyOnStub(summarizer, 'getLogs').andReturn(summaryLogs);
354-
spyOn(progressFormatter, 'log');
355265
});
356266

357267
it("gets the summary", function () {

0 commit comments

Comments
 (0)