Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

New approach to load available steps for tags

This first refactoring is to prepare the code to be able to enable steps
for a specific tag within the execution of a step
  • Loading branch information...
commit b28789283145532db35939af60b087e850552de2 1 parent c738651
Gabriel Reis authored
2  lib/turnip.rb
View
@@ -28,7 +28,7 @@ def run(feature_file)
describe feature.name, feature.metadata_hash do
feature.scenarios.each do |scenario|
it scenario.name, scenario.metadata_hash do
- Turnip::ScenarioRunner.new(self).load(Turnip::ScenarioContext.new(feature, scenario)).run
+ Turnip::ScenarioRunner.new(self).run(feature, scenario)
end
end
end
37 lib/turnip/scenario_context.rb
View
@@ -1,41 +1,30 @@
module Turnip
class ScenarioContext
- attr_accessor :feature
- attr_accessor :scenario
+ attr_accessor :world
- def initialize(feature, scenario)
- self.feature = feature
- self.scenario = scenario
+ def initialize(world)
+ self.world = world
end
- def available_background_steps
- available_steps_for(*feature_tags)
+ def enable_tags(*tags)
+ available_tags.concat(tags).uniq!
+ load_modules(*tags)
end
- def available_scenario_steps
- available_steps_for(*scenario_tags)
+ def available_steps
+ Turnip::StepModule.all_steps_for(*available_tags)
end
- def backgrounds
- feature.backgrounds
- end
-
- def modules
- Turnip::StepModule.modules_for(*scenario_tags)
+ def available_tags
+ @tags ||= []
end
private
- def available_steps_for(*tags)
- Turnip::StepModule.all_steps_for(*tags)
+ def load_modules(*tags)
+ modules = Turnip::StepModule.modules_for(*tags)
+ modules.each { |mod| world.extend mod }
end
- def feature_tags
- @feature_tags ||= feature.active_tags.uniq
- end
-
- def scenario_tags
- @scenario_tags ||= (feature_tags + scenario.active_tags).uniq
- end
end
end
28 lib/turnip/scenario_runner.rb
View
@@ -1,35 +1,37 @@
module Turnip
class ScenarioRunner
- attr_accessor :available_steps
attr_accessor :context
attr_accessor :world
def initialize(world)
self.world = world
- end
-
- def load(context)
- self.context = context
world.extend Turnip::RunnerDSL
world.turnip_runner = self
- context.modules.each {|mod| world.extend mod }
- self
+ self.context = Turnip::ScenarioContext.new(world)
+ end
+
+ def run(feature, scenario)
+ run_background_steps(feature)
+ run_scenario_steps(scenario)
end
- def run
- self.available_steps = context.available_background_steps
- context.backgrounds.each do |background|
+ def run_background_steps(feature)
+ context.enable_tags(*feature.active_tags)
+ feature.backgrounds.each do |background|
run_steps(background.steps)
end
+ end
- self.available_steps = context.available_scenario_steps
- run_steps(context.scenario.steps)
+ def run_scenario_steps(scenario)
+ context.enable_tags(*scenario.active_tags)
+ run_steps(scenario.steps)
end
def run_steps(steps)
steps.each do |step|
- Turnip::StepDefinition.execute(world, available_steps, step)
+ Turnip::StepDefinition.execute(world, context.available_steps, step)
end
end
+
end
end
57 spec/scenario_context_spec.rb
View
@@ -1,51 +1,42 @@
require 'spec_helper'
describe Turnip::ScenarioContext do
- let(:context) { Turnip::ScenarioContext.new(feature, scenario) }
- let(:feature) { stub(:active_tags => feature_tags) }
- let(:feature_tags) { %w(feature both) }
- let(:scenario) { stub(:active_tags => scenario_tags) }
- let(:scenario_tags) { %w(scenario both) }
- let(:unique_tags) { (feature_tags + scenario_tags).uniq }
+ let(:context) { Turnip::ScenarioContext.new(world) }
+ let(:world) { stub }
describe '#initialize' do
- let(:feature) { stub }
- let(:scenario) { stub }
-
- it 'keeps track of the feature' do
- Turnip::ScenarioContext.new(feature, scenario).feature.should eq(feature)
- end
-
- it 'keeps track of the scenario' do
- Turnip::ScenarioContext.new(feature, scenario).scenario.should eq(scenario)
+ it 'keeps track of the world' do
+ context.world.should eq(world)
end
end
- describe '#available_background_steps' do
- it 'gathers the steps for the feature tags' do
- Turnip::StepModule.should_receive(:all_steps_for).with(*feature_tags)
- context.available_background_steps
+ describe '#enable_tags' do
+
+ let(:tags) { [:tag1, :tag2, :tag1] }
+ let(:some_module) { Module.new }
+
+ before do
+ Turnip::StepModule.should_receive(:modules_for).with(*tags).and_return([some_module])
+ context.enable_tags(*tags)
end
- end
- describe '#available_scenario_steps' do
- it 'gathers the steps for the unique scenario and feature tags' do
- Turnip::StepModule.should_receive(:all_steps_for).with(*unique_tags)
- context.available_scenario_steps
+ it 'loads modules into the world' do
+ world.should be_kind_of(some_module)
end
- end
- describe '#backgrounds' do
- it 'delegates to the feature' do
- feature.should_receive :backgrounds
- context.backgrounds
+ it 'keeps unique tags' do
+ context.available_tags.should == [:tag1, :tag2]
end
+
end
- describe '#modules' do
- it 'gathers the modules for the unique scenario and feature tags' do
- Turnip::StepModule.should_receive(:modules_for).with(*unique_tags)
- context.modules
+ describe '#available_steps' do
+ it 'gathers the steps for the available tags' do
+ tags = [stub]
+ context.enable_tags(*tags)
+ Turnip::StepModule.should_receive(:all_steps_for).with(*tags)
+ context.available_steps
end
end
+
end
73 spec/scenario_runner_spec.rb
View
@@ -6,54 +6,67 @@
describe '#initialize' do
it 'keeps track of the world' do
- Turnip::ScenarioRunner.new(world).world.should eq(world)
- end
- end
-
- describe '#load' do
- let(:context) { stub(:modules => [some_module]) }
- let(:some_module) { Module.new }
-
- it 'is chainable' do
- runner.load(context).should eq(runner)
+ runner.world.should eq(world)
end
- it 'keeps track of the scenario context' do
- runner.load(context).context.should eq(context)
+ it 'loads the DSL module into the world' do
+ runner.world.should be_kind_of(Turnip::RunnerDSL)
end
- it 'loads the given modules into the world' do
- runner.load(context).world.should be_kind_of(some_module)
+ it 'adds the runner to the world' do
+ runner.world.turnip_runner.should eq(runner)
end
- it 'loads the DSL module into the world' do
- runner.load(context).world.should be_kind_of(Turnip::RunnerDSL)
+ it 'creates a scenario context' do
+ runner.context.should be_kind_of(Turnip::ScenarioContext)
end
+ end
- it 'adds the runner to the world' do
- runner.load(context).world.turnip_runner.should eq(runner)
+ describe '#run' do
+ it 'runs steps' do
+ feature = stub
+ scenario = stub
+ runner.should_receive(:run_background_steps).with(feature)
+ runner.should_receive(:run_scenario_steps).with(scenario)
+ runner.run(feature, scenario)
end
end
- describe '#run' do
+ describe '#run_background_steps' do
it 'iterates over the background steps' do
- runner.context = stub(:backgrounds => (0..2).map { stub(:steps => [stub]) },
- :available_background_steps => [],
- :available_scenario_steps => [],
- :scenario => stub(:steps => []))
+ feature = stub(:backgrounds => (0..2).map { stub(:steps => [stub]) },
+ :active_tags => [])
Turnip::StepDefinition.should_receive(:execute).exactly(3).times
- runner.run
+ runner.run_background_steps(feature)
+ end
+
+ it 'enables feature tags' do
+ feature_tags = [stub]
+ feature = stub(:backgrounds => [],
+ :active_tags => feature_tags)
+ runner.context = stub
+ runner.context.should_receive(:enable_tags).with(*feature_tags)
+ runner.run_background_steps(feature)
end
+ end
+ describe '#run_scenario_steps' do
it 'iterates over the scenario steps' do
- runner.context = stub(:backgrounds => [],
- :available_background_steps => [],
- :available_scenario_steps => [],
- :scenario => stub(:steps => (0..3)))
+ scenario = stub(:active_tags => [],
+ :steps => (0..3))
Turnip::StepDefinition.should_receive(:execute).exactly(4).times
- runner.run
+ runner.run_scenario_steps(scenario)
+ end
+
+ it 'enables scenario tags' do
+ scenario_tags = [stub]
+ scenario = stub(:steps => [],
+ :active_tags => scenario_tags)
+ runner.context = stub
+ runner.context.should_receive(:enable_tags).with(*scenario_tags)
+ runner.run_scenario_steps(scenario)
end
end
@@ -63,7 +76,7 @@
it 'executes the steps with the current world' do
step = stub
steps = [step]
- runner.available_steps = available_steps
+ runner.context = stub(:available_steps => available_steps)
Turnip::StepDefinition.should_receive(:execute).with(world, available_steps, step)
runner.run_steps(steps)
Please sign in to comment.
Something went wrong with that request. Please try again.