Skip to content

Commit

Permalink
Extract common formatter methods (#59, #63)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbpros committed Jun 20, 2012
1 parent bf89e7b commit 5b45be4
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 121 deletions.
1 change: 1 addition & 0 deletions lib/cucumber/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
27 changes: 27 additions & 0 deletions lib/cucumber/listener/formatter.js
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 1 addition & 17 deletions lib/cucumber/listener/progress_formatter.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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())
Expand Down
98 changes: 98 additions & 0 deletions spec/cucumber/listener/formatter_spec.js
Original file line number Diff line number Diff line change
@@ -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("");
});
});
});
118 changes: 14 additions & 104 deletions spec/cucumber/listener/progress_formatter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -283,32 +208,20 @@ 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);
});
});

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);
});
});

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);
Expand All @@ -320,7 +233,6 @@ describe("Cucumber.Listener.ProgressFormatter", function () {

beforeEach(function () {
step = createSpy("step");
spyOn(progressFormatter, 'log');
});

it("logs the undefined step character", function () {
Expand All @@ -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 () {
Expand All @@ -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 () {
Expand Down

0 comments on commit 5b45be4

Please sign in to comment.