Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extract common formatter methods (#59, #63)

  • Loading branch information...
commit 5b45be405b11a34e1d953a7cd12041960e993da7 1 parent bf89e7b
@jbpros jbpros authored
View
1  lib/cucumber/listener.js
@@ -33,6 +33,7 @@ var Listener = function () {
Listener.EVENT_HANDLER_NAME_PREFIX = 'handle';
Listener.EVENT_HANDLER_NAME_SUFFIX = 'Event';
+Listener.Formatter = require('./listener/formatter');
Listener.ProgressFormatter = require('./listener/progress_formatter');
Listener.StatsJournal = require('./listener/stats_journal');
Listener.Summarizer = require('./listener/summarizer');
View
27 lib/cucumber/listener/formatter.js
@@ -0,0 +1,27 @@
+var Formatter = function (options) {
+ var Cucumber = require('../../cucumber');
+
+ if (!options)
+ options = {};
+ if (options['logToConsole'] == undefined)
+ options['logToConsole'] = true;
+
+ var logs = "";
+
+ var self = Cucumber.Listener();
+
+ self.log = function log(string) {
+ logs += string;
+ if (options['logToConsole'])
+ process.stdout.write(string);
+ if (typeof(options['logToFunction']) == 'function')
+ options['logToFunction'](string);
+ };
+
+ self.getLogs = function getLogs() {
+ return logs;
+ };
+
+ return self;
+};
+module.exports = Formatter;
View
18 lib/cucumber/listener/progress_formatter.js
@@ -1,14 +1,10 @@
var ProgressFormatter = function(options) {
var Cucumber = require('../../cucumber');
- var logs = "";
-
if (!options)
options = {};
- if (options['logToConsole'] == undefined)
- options['logToConsole'] = true;
- var self = Cucumber.Listener();
+ var self = Cucumber.Listener.Formatter(options);
var summaryLogger = Cucumber.Listener.Summarizer();
var parentHear = self.hear;
@@ -18,18 +14,6 @@ var ProgressFormatter = function(options) {
});
};
- self.log = function log(string) {
- logs += string;
- if (options['logToConsole'])
- process.stdout.write(string);
- if (typeof(options['logToFunction']) == 'function')
- options['logToFunction'](string);
- };
-
- self.getLogs = function getLogs() {
- return logs;
- };
-
self.handleStepResultEvent = function handleStepResult(event, callback) {
var stepResult = event.getPayloadItem('stepResult');
if (stepResult.isSuccessful())
View
98 spec/cucumber/listener/formatter_spec.js
@@ -0,0 +1,98 @@
+require('../../support/spec_helper');
+
+describe("Cucumber.Listener.Formatter", function () {
+ var Cucumber = requireLib('cucumber');
+ var formatter, listener;
+
+ beforeEach(function () {
+ var Formatter = Cucumber.Listener.Formatter;
+ listener = createSpy("listener");
+ spyOn(Cucumber, 'Listener').andReturn(listener);
+ Cucumber.Listener.Formatter = Formatter;
+ formatter = Cucumber.Listener.Formatter();
+ });
+
+ describe("constructor", function () {
+ it("creates a listener", function () {
+ expect(Cucumber.Listener).toHaveBeenCalled();
+ });
+
+ it("extends the formatter", function () {
+ expect(formatter).toBe(listener);
+ });
+ });
+
+ describe("log()", function () {
+ var logged, alsoLogged, loggedBuffer;
+
+ beforeEach(function () {
+ logged = "this was logged";
+ alsoLogged = "this was also logged";
+ loggedBuffer = logged + alsoLogged;
+ spyOn(process.stdout, 'write');
+ });
+
+ it("records logged strings", function () {
+ formatter.log(logged);
+ formatter.log(alsoLogged);
+ expect(formatter.getLogs()).toBe(loggedBuffer);
+ });
+
+ it("outputs the logged string to STDOUT by default", function () {
+ formatter.log(logged);
+ expect(process.stdout.write).toHaveBeenCalledWith(logged);
+ });
+
+ describe("when asked to output to STDOUT", function () {
+ beforeEach(function () {
+ formatter = Cucumber.Listener.Formatter({logToConsole: true});
+ });
+
+ it("outputs the logged string to STDOUT", function () {
+ formatter.log(logged);
+ expect(process.stdout.write).toHaveBeenCalledWith(logged);
+ });
+ });
+
+ describe("when asked to not output to STDOUT", function () {
+ beforeEach(function () {
+ formatter = Cucumber.Listener.Formatter({logToConsole: false});
+ });
+
+ it("does not output anything to STDOUT", function () {
+ formatter.log(logged);
+ expect(process.stdout.write).not.toHaveBeenCalledWith(logged);
+ });
+ });
+
+ describe("when asked to output to a function", function () {
+ var userFunction;
+
+ beforeEach(function () {
+ userFunction = createSpy("output user function");
+ formatter = Cucumber.Listener.Formatter({logToFunction: userFunction});
+ });
+
+ it("calls the function with the logged string", function () {
+ formatter.log(logged);
+ expect(userFunction).toHaveBeenCalledWith(logged);
+ });
+ });
+ });
+
+ describe("getLogs()", function () {
+ it("returns the logged buffer", function () {
+ var logged = "this was logged";
+ var alsoLogged = "this was also logged";
+ var loggedBuffer = logged + alsoLogged;
+ spyOn(process.stdout, 'write'); // prevent actual output during spec execution
+ formatter.log(logged);
+ formatter.log(alsoLogged);
+ expect(formatter.getLogs()).toBe(loggedBuffer);
+ });
+
+ it("returns an empty string when the progress formatter did not log anything yet", function () {
+ expect(formatter.getLogs()).toBe("");
+ });
+ });
+});
View
118 spec/cucumber/listener/progress_formatter_spec.js
@@ -2,26 +2,25 @@ require('../../support/spec_helper');
describe("Cucumber.Listener.ProgressFormatter", function () {
var Cucumber = requireLib('cucumber');
- var listener, listenerHearMethod, summarizer, progressFormatter;
+ var formatter, formatterHearMethod, summarizer, progressFormatter, options;
beforeEach(function () {
- var ProgressFormatter = Cucumber.Listener.ProgressFormatter;
- listener = createSpy("listener");
- listenerHearMethod = spyOnStub(listener, 'hear');
- summarizer = createSpy("summarizer");
- spyOn(Cucumber, 'Listener').andReturn(listener);
- spyOnStub(Cucumber.Listener, 'Summarizer').andReturn(summarizer);
- Cucumber.Listener.ProgressFormatter = ProgressFormatter;
- progressFormatter = Cucumber.Listener.ProgressFormatter();
+ options = createSpy(options);
+ formatter = createSpyWithStubs("formatter", {log: null});
+ formatterHearMethod = spyOnStub(formatter, 'hear');
+ summarizer = createSpy("summarizer");
+ spyOn(Cucumber.Listener, 'Formatter').andReturn(formatter);
+ spyOn(Cucumber.Listener, 'Summarizer').andReturn(summarizer);
+ progressFormatter = Cucumber.Listener.ProgressFormatter(options);
});
describe("constructor", function () {
- it("creates a listener", function() {
- expect(Cucumber.Listener).toHaveBeenCalled();
+ it("creates a formatter", function() {
+ expect(Cucumber.Listener.Formatter).toHaveBeenCalledWith(options);
});
- it("extends the listener", function () {
- expect(progressFormatter).toBe(listener);
+ it("extends the formatter", function () {
+ expect(progressFormatter).toBe(formatter);
});
it("creates a summarizer", function () {
@@ -53,87 +52,13 @@ describe("Cucumber.Listener.ProgressFormatter", function () {
summarizerCallback = summarizer.hear.mostRecentCall.args[1];
});
- it("tells the listener to listen to the event", function () {
+ it("tells the formatter to listen to the event", function () {
summarizerCallback();
- expect(listenerHearMethod).toHaveBeenCalledWith(event, callback);
+ expect(formatterHearMethod).toHaveBeenCalledWith(event, callback);
});
});
});
- describe("log()", function () {
- var logged, alsoLogged, loggedBuffer;
-
- beforeEach(function () {
- logged = "this was logged";
- alsoLogged = "this was also logged";
- loggedBuffer = logged + alsoLogged;
- spyOn(process.stdout, 'write');
- });
-
- it("records logged strings", function () {
- progressFormatter.log(logged);
- progressFormatter.log(alsoLogged);
- expect(progressFormatter.getLogs()).toBe(loggedBuffer);
- });
-
- it("outputs the logged string to STDOUT by default", function () {
- progressFormatter.log(logged);
- expect(process.stdout.write).toHaveBeenCalledWith(logged);
- });
-
- describe("when asked to output to STDOUT", function () {
- beforeEach(function () {
- progressFormatter = Cucumber.Listener.ProgressFormatter({logToConsole: true});
- });
-
- it("outputs the logged string to STDOUT", function () {
- progressFormatter.log(logged);
- expect(process.stdout.write).toHaveBeenCalledWith(logged);
- });
- });
-
- describe("when asked to not output to STDOUT", function () {
- beforeEach(function () {
- progressFormatter = Cucumber.Listener.ProgressFormatter({logToConsole: false});
- });
-
- it("does not output anything to STDOUT", function () {
- progressFormatter.log(logged);
- expect(process.stdout.write).not.toHaveBeenCalledWith(logged);
- });
- });
-
- describe("when asked to output to a function", function () {
- var userFunction;
-
- beforeEach(function () {
- userFunction = createSpy("output user function");
- progressFormatter = Cucumber.Listener.ProgressFormatter({logToFunction: userFunction});
- });
-
- it("calls the function with the logged string", function () {
- progressFormatter.log(logged);
- expect(userFunction).toHaveBeenCalledWith(logged);
- });
- });
- });
-
- describe("getLogs()", function () {
- it("returns the logged buffer", function () {
- var logged = "this was logged";
- var alsoLogged = "this was also logged";
- var loggedBuffer = logged + alsoLogged;
- spyOn(process.stdout, 'write'); // prevent actual output during spec execution
- progressFormatter.log(logged);
- progressFormatter.log(alsoLogged);
- expect(progressFormatter.getLogs()).toBe(loggedBuffer);
- });
-
- it("returns an empty string when the progress formatter did not log anything yet", function () {
- expect(progressFormatter.getLogs()).toBe("");
- });
- });
-
describe("handleStepResultEvent()", function () {
var event, callback, stepResult;
@@ -283,10 +208,6 @@ describe("Cucumber.Listener.ProgressFormatter", function () {
});
describe("handleSuccessfulStepResult()", function () {
- beforeEach(function () {
- spyOn(progressFormatter, 'log');
- });
-
it("logs the passing step character", function () {
progressFormatter.handleSuccessfulStepResult();
expect(progressFormatter.log).toHaveBeenCalledWith(Cucumber.Listener.ProgressFormatter.PASSED_STEP_CHARACTER);
@@ -294,10 +215,6 @@ describe("Cucumber.Listener.ProgressFormatter", function () {
});
describe("handlePendingStepResult()", function () {
- beforeEach(function () {
- spyOn(progressFormatter, 'log')
- });
-
it("logs the pending step character", function () {
progressFormatter.handlePendingStepResult();
expect(progressFormatter.log).toHaveBeenCalledWith(Cucumber.Listener.ProgressFormatter.PENDING_STEP_CHARACTER);
@@ -305,10 +222,6 @@ describe("Cucumber.Listener.ProgressFormatter", function () {
});
describe("handleSkippedStepResult()", function () {
- beforeEach(function () {
- spyOn(progressFormatter, 'log');
- });
-
it("logs the skipped step character", function () {
progressFormatter.handleSkippedStepResult();
expect(progressFormatter.log).toHaveBeenCalledWith(Cucumber.Listener.ProgressFormatter.SKIPPED_STEP_CHARACTER);
@@ -320,7 +233,6 @@ describe("Cucumber.Listener.ProgressFormatter", function () {
beforeEach(function () {
step = createSpy("step");
- spyOn(progressFormatter, 'log');
});
it("logs the undefined step character", function () {
@@ -334,7 +246,6 @@ describe("Cucumber.Listener.ProgressFormatter", function () {
beforeEach(function () {
stepResult = createSpy("failed step result");
- spyOn(progressFormatter, 'log');
});
it("logs the failed step character", function () {
@@ -351,7 +262,6 @@ describe("Cucumber.Listener.ProgressFormatter", function () {
callback = createSpy("callback");
summaryLogs = createSpy("summary logs");
spyOnStub(summarizer, 'getLogs').andReturn(summaryLogs);
- spyOn(progressFormatter, 'log');
});
it("gets the summary", function () {
Please sign in to comment.
Something went wrong with that request. Please try again.