Skip to content

Commit

Permalink
Add pretty formatter (simplified, monochrome) (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbpros committed Jun 20, 2012
1 parent 5b45be4 commit e750a9e
Show file tree
Hide file tree
Showing 9 changed files with 631 additions and 118 deletions.
11 changes: 8 additions & 3 deletions lib/cucumber/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ var Cli = function(argv) {
},

runSuiteWithConfiguration: function runSuiteWithConfiguration(configuration, callback) {
var runtime = Cucumber.Runtime(configuration);
var progressFormatter = Cucumber.Listener.ProgressFormatter();
runtime.attachListener(progressFormatter);
var runtime = Cucumber.Runtime(configuration);
var formatter = configuration.getFormatter();
runtime.attachListener(formatter);
runtime.start(callback);
},

Expand Down Expand Up @@ -57,6 +57,11 @@ var Cli = function(argv) {
tags to exclude several tags you have to use\n\
logical AND: --tags ~@fixme --tags ~@buggy.\n\
\n\
-f, --format FORMAT How to format features (default: progress).\n\
Available formats:\n\
pretty : prints the feature as is\n\
progress : prints one character per scenario\n\
\n\
-v, --version Display Cucumber.js's version.\n\
\n\
-h, --help You're looking at it.\n");
Expand Down
10 changes: 10 additions & 0 deletions lib/cucumber/cli/argument_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,16 @@ var ArgumentParser = function(argv) {
return tagGroups;
},

getFormat: function getFormat() {
var format = self.getOptionOrDefault(ArgumentParser.FORMAT_OPTION_NAME, ArgumentParser.DEFAULT_FORMAT_VALUE);
return format;
},

getKnownOptionDefinitions: function getKnownOptionDefinitions() {
var definitions = {};
definitions[ArgumentParser.REQUIRE_OPTION_NAME] = [path, Array];
definitions[ArgumentParser.TAGS_OPTION_NAME] = [String, Array];
definitions[ArgumentParser.FORMAT_OPTION_NAME] = String;
definitions[ArgumentParser.HELP_FLAG_NAME] = Boolean;
definitions[ArgumentParser.VERSION_FLAG_NAME] = Boolean;
return definitions;
Expand All @@ -69,6 +75,7 @@ var ArgumentParser = function(argv) {
getShortenedOptionDefinitions: function getShortenedOptionDefinitions() {
var definitions = {};
definitions[ArgumentParser.REQUIRE_OPTION_SHORT_NAME] = [ArgumentParser.LONG_OPTION_PREFIX + ArgumentParser.REQUIRE_OPTION_NAME];
definitions[ArgumentParser.FORMAT_OPTION_SHORT_NAME] = [ArgumentParser.LONG_OPTION_PREFIX + ArgumentParser.FORMAT_OPTION_NAME];
definitions[ArgumentParser.HELP_FLAG_SHORT_NAME] = [ArgumentParser.LONG_OPTION_PREFIX + ArgumentParser.HELP_FLAG_NAME];
return definitions;
},
Expand Down Expand Up @@ -107,6 +114,9 @@ ArgumentParser.REQUIRE_OPTION_NAME = "require";
ArgumentParser.REQUIRE_OPTION_SHORT_NAME = "r";
ArgumentParser.TAGS_OPTION_NAME = "tags";
ArgumentParser.TAGS_OPTION_SHORT_NAME = "t";
ArgumentParser.FORMAT_OPTION_NAME = "format";
ArgumentParser.FORMAT_OPTION_SHORT_NAME = "f";
ArgumentParser.DEFAULT_FORMAT_VALUE = "progress";
ArgumentParser.HELP_FLAG_NAME = "help";
ArgumentParser.HELP_FLAG_SHORT_NAME = "h";
ArgumentParser.DEFAULT_HELP_FLAG_VALUE = false;
Expand Down
18 changes: 18 additions & 0 deletions lib/cucumber/cli/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ var Configuration = function(argv) {
argumentParser.parse(argv);

var self = {
getFormatter: function getFormatter() {
var formatter;
var format = argumentParser.getFormat();
switch(format) {
case Configuration.PROGRESS_FORMAT_NAME:
formatter = Cucumber.Listener.ProgressFormatter();
break;
case Configuration.PRETTY_FORMAT_NAME:
formatter = Cucumber.Listener.PrettyFormatter();
break;
default:
throw new Error("Unknown formatter name \"" + format + "\".");
}
return formatter;
},

getFeatureSources: function getFeatureSources() {
var featureFilePaths = argumentParser.getFeatureFilePaths();
var featureSourceLoader = Cucumber.Cli.FeatureSourceLoader(featureFilePaths);
Expand Down Expand Up @@ -47,4 +63,6 @@ var Configuration = function(argv) {
};
return self;
};
Configuration.PRETTY_FORMAT_NAME = "pretty";
Configuration.PROGRESS_FORMAT_NAME = "progress";
module.exports = Configuration;
1 change: 1 addition & 0 deletions lib/cucumber/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Listener.EVENT_HANDLER_NAME_PREFIX = 'handle';
Listener.EVENT_HANDLER_NAME_SUFFIX = 'Event';

Listener.Formatter = require('./listener/formatter');
Listener.PrettyFormatter = require('./listener/pretty_formatter');
Listener.ProgressFormatter = require('./listener/progress_formatter');
Listener.StatsJournal = require('./listener/stats_journal');
Listener.Summarizer = require('./listener/summarizer');
Expand Down
71 changes: 71 additions & 0 deletions lib/cucumber/listener/pretty_formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var PrettyFormatter = function(options) {
var Cucumber = require('../../cucumber');

var self = Cucumber.Listener.Formatter(options);
var summarizer = Cucumber.Listener.Summarizer();

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

self.handleBeforeFeatureEvent = function handleBeforeFeatureEvent(event, callback) {
var feature = event.getPayloadItem('feature');
var source = feature.getKeyword() + ": " + feature.getName() + "\n\n";
self.log(source);
callback();
};

self.handleBeforeScenarioEvent = function handleBeforeScenarioEvent(event, callback) {
var scenario = event.getPayloadItem('scenario');
var source = scenario.getKeyword() + ": " + scenario.getName() + "\n";
self.logIndented(source, 1);
callback();
};

self.handleAfterScenarioEvent = function handleAfterScenarioEvent(event, callback) {
self.log("\n");
callback();
};

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

stepResult.isFailed();
if (stepResult.isFailed()) {
var failure = stepResult.getFailureException();
var failureDescription = failure.stack || failure;
self.logIndented(failureDescription + "\n", 3);
}
callback();
};

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

self.logIndented = function logIndented(text, level) {
var indented = self.indent(text, level);
self.log(indented);
};

self.indent = function indent(text, level) {
var indented;
text.split("\n").forEach(function(line) {
var prefix = new Array(level + 1).join(" ");
line = (prefix + line).replace(/\s+$/, '');
indented = (typeof(indented) == 'undefined' ? line : indented + "\n" + line);
});
return indented;
};

return self;
};
module.exports = PrettyFormatter;
Loading

0 comments on commit e750a9e

Please sign in to comment.