Skip to content

Commit

Permalink
Keep entities small
Browse files Browse the repository at this point in the history
  • Loading branch information
oriolgual committed Feb 12, 2012
1 parent 93587b5 commit 0ff0a54
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 58 deletions.
76 changes: 18 additions & 58 deletions lib/spinach/runner/scenario_runner.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,6 @@
require_relative 'scenario_runner_mutex'
require_relative 'step_runner'

module Spinach module Spinach
class Runner class Runner
# A Scenario Runner handles a particular scenario run. # A Scenario Runner handles a particular scenario run.
Expand All @@ -24,7 +27,9 @@ def feature
# #
# @api public # @api public
def steps def steps
feature.background_steps + @scenario.steps @steps ||=(feature.background_steps + @scenario.steps).map do |step|
StepRunner.new(step, step_definitions)
end
end end


# @return [FeatureSteps] # @return [FeatureSteps]
Expand All @@ -48,80 +53,35 @@ def step_definitions_klass
# @api public # @api public
def run def run
hooks.run_before_scenario @scenario hooks.run_before_scenario @scenario
scenario_mutex.deactivate scenario_runner_mutex.deactivate
hooks.run_around_scenario @scenario do hooks.run_around_scenario @scenario do
scenario_mutex.activate scenario_runner_mutex.activate
run_scenario_steps run_scenario_steps
end end
raise "around_scenario hooks *must* yield" if !scenario_mutex.active? && !@exception raise "around_scenario hooks *must* yield" if !scenario_runner_mutex.active? && success?
hooks.run_after_scenario @scenario hooks.run_after_scenario @scenario
!@exception !!success?
end end


def hooks def hooks
Spinach.hooks Spinach.hooks
end end


def scenario_mutex def scenario_runner_mutex
@scenario_mutex ||= ScenarioMutex.new @scenario_runner_mutex ||= ScenarioRunnerMutex.new
end end


def run_scenario_steps def run_scenario_steps
previous_step_success = true
steps.each do |step| steps.each do |step|
run_step_with_hooks(step) step.run(previous_step_success)
previous_step_success = step.success?
end end
end end


def run_step_with_hooks(step) def success?
hooks.run_before_step step steps.all? do |step|
run_step(step) unless skip_step?(step) step.success?
hooks.run_after_step step
end

def skip_step?(step)
if @exception
hooks.run_on_skipped_step step
return true
end
return false
end

# Runs a particular step.
#
# @param [Gherkin::AST::Step] step
# The step to be run.
#
# @api semipublic
def run_step(step)
step_location = step_definitions.step_location_for(step.name)
step_definitions.execute(step)
hooks.run_on_successful_step step, step_location
rescue *Spinach.config[:failure_exceptions] => exception
@exception = exception
hooks.run_on_failed_step step, @exception, step_location
rescue Spinach::StepNotDefinedException => exception
@exception = exception
hooks.run_on_undefined_step step, @exception
rescue Exception => exception
@exception = exception
hooks.run_on_error_step step, @exception, step_location
end

class ScenarioMutex
def initialize
@running = false
end

def deactivate
@running = false
end

def activate
@running = true
end

def active?
!!@running
end end
end end
end end
Expand Down
21 changes: 21 additions & 0 deletions lib/spinach/runner/scenario_runner_mutex.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,21 @@
module Spinach
class Runner
class ScenarioRunnerMutex
def initialize
@running = false
end

def deactivate
@running = false
end

def activate
@running = true
end

def active?
!!@running
end
end
end
end
56 changes: 56 additions & 0 deletions lib/spinach/runner/step_runner.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,56 @@
module Spinach
class Runner
class StepRunner
def initialize(step, context)
@step = step
@exception = false
@context = context
end

def run(previous_step_success)
hooks.run_before_step @step
execute unless skip?(previous_step_success)
hooks.run_after_step @step
end

def skip?(previous_step_success)
unless previous_step_success
hooks.run_on_skipped_step @step
end
!previous_step_success
end

# Runs a particular step.
#
# @param [Gherkin::AST::Step] step
# The step to be run.
#
# @api semipublic
def execute
@context.execute(@step)
hooks.run_on_successful_step @step, location
rescue *Spinach.config[:failure_exceptions] => exception
@exception = exception
hooks.run_on_failed_step @step, @exception, location
rescue Spinach::StepNotDefinedException => exception
@exception = exception
hooks.run_on_undefined_step @step, @exception
rescue Exception => exception
@exception = exception
hooks.run_on_error_step @step, @exception, location
end

def hooks
Spinach.hooks
end

def success?
!@exception
end

def location
@context.step_location_for(@step.name)
end
end
end
end

0 comments on commit 0ff0a54

Please sign in to comment.