Skip to content

Commit

Permalink
Extract summary logger from progress formatter (#59, #63)
Browse files Browse the repository at this point in the history
Feature run summaries can now be reused among formatters.
  • Loading branch information
jbpros committed Jun 19, 2012
1 parent 837c021 commit 3057098
Show file tree
Hide file tree
Showing 5 changed files with 1,291 additions and 1,025 deletions.
1 change: 1 addition & 0 deletions lib/cucumber/listener.js
Expand Up @@ -35,4 +35,5 @@ Listener.EVENT_HANDLER_NAME_SUFFIX = 'Event';

Listener.ProgressFormatter = require('./listener/progress_formatter');
Listener.StatsJournal = require('./listener/stats_journal');
Listener.SummaryLogger = require('./listener/summary_logger');
module.exports = Listener;
147 changes: 9 additions & 138 deletions lib/cucumber/listener/progress_formatter.js
Expand Up @@ -2,22 +2,18 @@ var ProgressFormatter = function(options) {
var Cucumber = require('../../cucumber');

var logs = "";
var failedScenarioLogBuffer = "";
var undefinedStepLogBuffer = "";
var failedStepResults = Cucumber.Type.Collection();
var statsJournal = Cucumber.Listener.StatsJournal();

if (!options)
options = {};
if (options['logToConsole'] == undefined)
options['logToConsole'] = true;

var self = Cucumber.Listener();
var self = Cucumber.Listener();
var summaryLogger = Cucumber.Listener.SummaryLogger();

var parentHear = self.hear;

self.hear = function hear(event, callback) {
statsJournal.hear(event, function () {
summaryLogger.hear(event, function () {
parentHear(event, callback);
});
};
Expand All @@ -43,9 +39,9 @@ var ProgressFormatter = function(options) {
else if (stepResult.isSkipped())
self.handleSkippedStepResult();
else if (stepResult.isUndefined())
self.handleUndefinedStepResult(stepResult);
self.handleUndefinedStepResult();
else
self.handleFailedStepResult(stepResult);
self.handleFailedStepResult();
callback();
};

Expand All @@ -61,145 +57,20 @@ var ProgressFormatter = function(options) {
self.log(ProgressFormatter.SKIPPED_STEP_CHARACTER);
};

self.handleUndefinedStepResult = function handleUndefinedStepResult(stepResult) {
var step = stepResult.getStep();
self.storeUndefinedStep(step);
self.handleUndefinedStepResult = function handleUndefinedStepResult() {
self.log(ProgressFormatter.UNDEFINED_STEP_CHARACTER);
};

self.handleFailedStepResult = function handleFailedStepResult(stepResult) {
self.storeFailedStepResult(stepResult);
self.handleFailedStepResult = function handleFailedStepResult() {
self.log(ProgressFormatter.FAILED_STEP_CHARACTER);
};

self.handleAfterFeaturesEvent = function handleAfterFeaturesEvent(event, callback) {
self.logSummary();
var summaryLogs = summaryLogger.getLogs();
self.log(summaryLogs);
callback();
};

self.handleAfterScenarioEvent = function handleAfterScenarioEvent(event, callback) {
if (statsJournal.isCurrentScenarioFailing()) {
var scenario = event.getPayloadItem('scenario');
self.storeFailedScenario(scenario);
}
callback();
};

self.storeFailedStepResult = function storeFailedStepResult(failedStepResult) {
failedStepResults.add(failedStepResult);
};

self.storeFailedScenario = function storeFailedScenario(failedScenario) {
var name = failedScenario.getName();
var line = failedScenario.getLine();
self.appendStringToFailedScenarioLogBuffer(":" + line + " # Scenario: " + name);
};

self.storeUndefinedStep = function storeUndefinedStep(step) {
var snippetBuilder = Cucumber.SupportCode.StepDefinitionSnippetBuilder(step);
var snippet = snippetBuilder.buildSnippet();
self.appendStringToUndefinedStepLogBuffer(snippet);
};

self.appendStringToFailedScenarioLogBuffer = function appendStringToFailedScenarioLogBuffer(string) {
failedScenarioLogBuffer += string + "\n";
};

self.appendStringToUndefinedStepLogBuffer = function appendStringToUndefinedStepLogBuffer(string) {
if (undefinedStepLogBuffer.indexOf(string) == -1)
undefinedStepLogBuffer += string + "\n";
};

self.getFailedScenarioLogBuffer = function getFailedScenarioLogBuffer() {
return failedScenarioLogBuffer;
};

self.getUndefinedStepLogBuffer = function getUndefinedStepLogBuffer() {
return undefinedStepLogBuffer;
};

self.logSummary = function logSummary() {
self.log("\n\n");
if (statsJournal.witnessedAnyFailedStep())
self.logFailedStepResults();
self.logScenariosSummary();
self.logStepsSummary();
if (statsJournal.witnessedAnyUndefinedStep())
self.logUndefinedStepSnippets();
};

self.logFailedStepResults = function logFailedStepResults() {
self.log("(::) failed steps (::)\n\n");
failedStepResults.syncForEach(function(stepResult) {
self.logFailedStepResult(stepResult);
});
self.log("Failing scenarios:\n");
var failedScenarios = self.getFailedScenarioLogBuffer();
self.log(failedScenarios);
self.log("\n");
};

self.logFailedStepResult = function logFailedStepResult(stepResult) {
var failureMessage = stepResult.getFailureException();
self.log(failureMessage.stack || failureMessage);
self.log("\n\n");
};

self.logScenariosSummary = function logScenariosSummary() {
var scenarioCount = statsJournal.getScenarioCount();
var passedScenarioCount = statsJournal.getPassedScenarioCount();
var undefinedScenarioCount = statsJournal.getUndefinedScenarioCount();
var pendingScenarioCount = statsJournal.getPendingScenarioCount();
var failedScenarioCount = statsJournal.getFailedScenarioCount();
var details = [];

self.log(scenarioCount + " scenario" + (scenarioCount != 1 ? "s" : ""));
if (scenarioCount > 0 ) {
if (failedScenarioCount > 0)
details.push(failedScenarioCount + " failed");
if (undefinedScenarioCount > 0)
details.push(undefinedScenarioCount + " undefined");
if (pendingScenarioCount > 0)
details.push(pendingScenarioCount + " pending");
if (passedScenarioCount > 0)
details.push(passedScenarioCount + " passed");
self.log(" (" + details.join(', ') + ")");
}
self.log("\n");
};

self.logStepsSummary = function logStepsSummary() {
var stepCount = statsJournal.getStepCount();
var passedStepCount = statsJournal.getPassedStepCount();
var undefinedStepCount = statsJournal.getUndefinedStepCount();
var skippedStepCount = statsJournal.getSkippedStepCount();
var pendingStepCount = statsJournal.getPendingStepCount();
var failedStepCount = statsJournal.getFailedStepCount();
var details = [];

self.log(stepCount + " step" + (stepCount != 1 ? "s" : ""));
if (stepCount > 0) {
if (failedStepCount > 0)
details.push(failedStepCount + " failed");
if (undefinedStepCount > 0)
details.push(undefinedStepCount + " undefined");
if (pendingStepCount > 0)
details.push(pendingStepCount + " pending");
if (skippedStepCount > 0)
details.push(skippedStepCount + " skipped");
if (passedStepCount > 0)
details.push(passedStepCount + " passed");
self.log(" (" + details.join(', ') + ")");
}
self.log("\n");
};

self.logUndefinedStepSnippets = function logUndefinedStepSnippets() {
var undefinedStepLogBuffer = self.getUndefinedStepLogBuffer();
self.log("\nYou can implement step definitions for undefined steps with these snippets:\n\n");
self.log(undefinedStepLogBuffer);
};

return self;
};
ProgressFormatter.PASSED_STEP_CHARACTER = '.';
Expand Down
176 changes: 176 additions & 0 deletions lib/cucumber/listener/summary_logger.js
@@ -0,0 +1,176 @@
var SummaryLogger = function () {
var Cucumber = require('../../cucumber');

var logs = "";
var failedScenarioLogBuffer = "";
var undefinedStepLogBuffer = "";
var failedStepResults = Cucumber.Type.Collection();
var statsJournal = Cucumber.Listener.StatsJournal();

var self = Cucumber.Listener();

var parentHear = self.hear;
self.hear = function hear(event, callback) {
statsJournal.hear(event, function () {
parentHear(event, callback);
});
};

self.log = function log(string) {
logs += string;
};

self.getLogs = function getLogs() {
return logs;
};

self.handleStepResultEvent = function handleStepResult(event, callback) {
var stepResult = event.getPayloadItem('stepResult');
if (stepResult.isUndefined()) {
self.handleUndefinedStepResult(stepResult);
} else if (stepResult.isFailed()) {
self.handleFailedStepResult(stepResult);
}
callback();
};

self.handleUndefinedStepResult = function handleUndefinedStepResult(stepResult) {
var step = stepResult.getStep();
self.storeUndefinedStep(step);
};

self.handleFailedStepResult = function handleFailedStepResult(stepResult) {
self.storeFailedStepResult(stepResult);
};

self.handleAfterScenarioEvent = function handleAfterScenarioEvent(event, callback) {
if (statsJournal.isCurrentScenarioFailing()) {
var scenario = event.getPayloadItem('scenario');
self.storeFailedScenario(scenario);
}
callback();
};

self.handleAfterFeaturesEvent = function handleAfterFeaturesEvent(event, callback) {
self.logSummary();
callback();
};

self.storeFailedStepResult = function storeFailedStepResult(failedStepResult) {
failedStepResults.add(failedStepResult);
};

self.storeFailedScenario = function storeFailedScenario(failedScenario) {
var name = failedScenario.getName();
var line = failedScenario.getLine();
self.appendStringToFailedScenarioLogBuffer(":" + line + " # Scenario: " + name);
};

self.storeUndefinedStep = function storeUndefinedStep(step) {
var snippetBuilder = Cucumber.SupportCode.StepDefinitionSnippetBuilder(step);
var snippet = snippetBuilder.buildSnippet();
self.appendStringToUndefinedStepLogBuffer(snippet);
};

self.appendStringToFailedScenarioLogBuffer = function appendStringToFailedScenarioLogBuffer(string) {
failedScenarioLogBuffer += string + "\n";
};

self.appendStringToUndefinedStepLogBuffer = function appendStringToUndefinedStepLogBuffer(string) {
if (undefinedStepLogBuffer.indexOf(string) == -1)
undefinedStepLogBuffer += string + "\n";
};

self.getFailedScenarioLogBuffer = function getFailedScenarioLogBuffer() {
return failedScenarioLogBuffer;
};

self.getUndefinedStepLogBuffer = function getUndefinedStepLogBuffer() {
return undefinedStepLogBuffer;
};

self.logSummary = function logSummary() {
self.log("\n\n");
if (statsJournal.witnessedAnyFailedStep())
self.logFailedStepResults();
self.logScenariosSummary();
self.logStepsSummary();
if (statsJournal.witnessedAnyUndefinedStep())
self.logUndefinedStepSnippets();
};

self.logFailedStepResults = function logFailedStepResults() {
self.log("(::) failed steps (::)\n\n");
failedStepResults.syncForEach(function(stepResult) {
self.logFailedStepResult(stepResult);
});
self.log("Failing scenarios:\n");
var failedScenarios = self.getFailedScenarioLogBuffer();
self.log(failedScenarios);
self.log("\n");
};

self.logFailedStepResult = function logFailedStepResult(stepResult) {
var failureMessage = stepResult.getFailureException();
self.log(failureMessage.stack || failureMessage);
self.log("\n\n");
};

self.logScenariosSummary = function logScenariosSummary() {
var scenarioCount = statsJournal.getScenarioCount();
var passedScenarioCount = statsJournal.getPassedScenarioCount();
var undefinedScenarioCount = statsJournal.getUndefinedScenarioCount();
var pendingScenarioCount = statsJournal.getPendingScenarioCount();
var failedScenarioCount = statsJournal.getFailedScenarioCount();
var details = [];

self.log(scenarioCount + " scenario" + (scenarioCount != 1 ? "s" : ""));
if (scenarioCount > 0 ) {
if (failedScenarioCount > 0)
details.push(failedScenarioCount + " failed");
if (undefinedScenarioCount > 0)
details.push(undefinedScenarioCount + " undefined");
if (pendingScenarioCount > 0)
details.push(pendingScenarioCount + " pending");
if (passedScenarioCount > 0)
details.push(passedScenarioCount + " passed");
self.log(" (" + details.join(', ') + ")");
}
self.log("\n");
};

self.logStepsSummary = function logStepsSummary() {
var stepCount = statsJournal.getStepCount();
var passedStepCount = statsJournal.getPassedStepCount();
var undefinedStepCount = statsJournal.getUndefinedStepCount();
var skippedStepCount = statsJournal.getSkippedStepCount();
var pendingStepCount = statsJournal.getPendingStepCount();
var failedStepCount = statsJournal.getFailedStepCount();
var details = [];

self.log(stepCount + " step" + (stepCount != 1 ? "s" : ""));
if (stepCount > 0) {
if (failedStepCount > 0)
details.push(failedStepCount + " failed");
if (undefinedStepCount > 0)
details.push(undefinedStepCount + " undefined");
if (pendingStepCount > 0)
details.push(pendingStepCount + " pending");
if (skippedStepCount > 0)
details.push(skippedStepCount + " skipped");
if (passedStepCount > 0)
details.push(passedStepCount + " passed");
self.log(" (" + details.join(', ') + ")");
}
self.log("\n");
};

self.logUndefinedStepSnippets = function logUndefinedStepSnippets() {
var undefinedStepLogBuffer = self.getUndefinedStepLogBuffer();
self.log("\nYou can implement step definitions for undefined steps with these snippets:\n\n");
self.log(undefinedStepLogBuffer);
};

return self;
};
module.exports = SummaryLogger;

0 comments on commit 3057098

Please sign in to comment.