Skip to content

Commit

Permalink
Refactor listener infrastructure (#35, #59, #63)
Browse files Browse the repository at this point in the history
* Extract common listener behaviour from progress formatter
* Extract stats journal from progress formatter

Theses changes will ease the implementation of new listeners (e.g. the pretty, JSON and jUnit formatters).
  • Loading branch information
jbpros committed Jun 17, 2012
1 parent 264d097 commit 837c021
Show file tree
Hide file tree
Showing 19 changed files with 2,029 additions and 1,641 deletions.
37 changes: 36 additions & 1 deletion lib/cucumber/listener.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
var Listener = {};
var Listener = function () {
var self = {
hear: function hear(event, callback) {
if (self.hasHandlerForEvent(event)) {
var handler = self.getHandlerForEvent(event);
handler(event, callback);
} else {
callback();
}
},

hasHandlerForEvent: function hasHandlerForEvent(event) {
var handlerName = self.buildHandlerNameForEvent(event);
return self[handlerName] != undefined;
},

buildHandlerNameForEvent: function buildHandlerNameForEvent(event) {
var handlerName =
Listener.EVENT_HANDLER_NAME_PREFIX +
event.getName() +
Listener.EVENT_HANDLER_NAME_SUFFIX;
return handlerName;
},

getHandlerForEvent: function getHandlerForEvent(event) {
var eventHandlerName = self.buildHandlerNameForEvent(event);
return self[eventHandlerName];
}
};
return self;
};

Listener.EVENT_HANDLER_NAME_PREFIX = 'handle';
Listener.EVENT_HANDLER_NAME_SUFFIX = 'Event';

Listener.ProgressFormatter = require('./listener/progress_formatter');
Listener.StatsJournal = require('./listener/stats_journal');
module.exports = Listener;
Loading

0 comments on commit 837c021

Please sign in to comment.