Skip to content

Instantly share code, notes, and snippets.

@ashmoran
Created December 7, 2009 23:33
Show Gist options
  • Save ashmoran/251233 to your computer and use it in GitHub Desktop.
Save ashmoran/251233 to your computer and use it in GitHub Desktop.
class BeforeFeatureHooks
def initialize
@hooks = []
end
def register_hook(proc)
@hooks << proc
end
def run(feature)
@hooks.each do |hook|
hook.call(feature)
end
end
end
class BeforeFeatureListener
def initialize(step_mother, io, options)
@step_mother, @io, @options = step_mother, io, options
end
class << self
def reset_scenario_count
@scenario_count = 0
end
def increase_scenario_count
@scenario_count += 1
end
def scenario_count
@scenario_count
end
def run_background_once=(state)
@run_background_once = state
end
def run_background_once?
@run_background_once
end
def in_background?
@in_background
end
def background_run?
@background_run
end
def record_background_started
@in_background = true
end
def record_background_complete
@in_background = false
@background_run = true
end
def reset_background_run
@in_background = false
@background_run = false
end
end
def before_feature(feature)
self.class.reset_scenario_count
self.class.reset_background_run
$before_feature_hooks.run(feature)
end
# The following two methods are only called on the first scenario
# of a feature, because (as of Cucumber 0.3.104), the listeners are
# driven by the AST
def before_background(background)
self.class.record_background_started
end
def after_background(background)
self.class.record_background_complete
end
# On the second scenario, we don't visit the background again -
# the background steps get copied into the scenario
def before_steps(steps)
self.class.increase_scenario_count unless self.class.in_background?
self.class.record_background_started unless self.class.scenario_count == 1
end
end
$before_feature_hooks = BeforeFeatureHooks.new
def BeforeFeature(&proc)
$before_feature_hooks.register_hook(proc)
end
AfterConfiguration do |configuration|
configuration.options[:formats] << ["BeforeFeatureListener", nil]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment