Skip to content

Commit

Permalink
End naming change
Browse files Browse the repository at this point in the history
  • Loading branch information
josepjaume committed Oct 11, 2011
1 parent 693007f commit bc4c2ef
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 206 deletions.
14 changes: 7 additions & 7 deletions lib/spinach.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@
# scenarios.
#
module Spinach
@@features = []
@@feature_steps = []

# @return [Array<Feature>]
# All the registered features.
#
# @api public
def self.features
@@features
def self.feature_steps
@@feature_steps
end

# Resets Spinach to a pristine state, as if no feature was ever registered.
# Mostly useful in Spinach's own testing.
#
# @api semipublic
def self.reset_features
@@features = []
def self.reset_feature_steps
@@feature_steps = []
end

# Finds a feature given a feature name.
Expand All @@ -47,9 +47,9 @@ def self.reset_features
# The feature name.
#
# @api public
def self.find_feature(name)
def self.find_feature_steps(name)
klass = Spinach::Support.camelize(name)
@@features.detect do |feature|
feature_steps.detect do |feature|
feature.feature_name.to_s == name.to_s ||
feature.name == klass
end || raise(Spinach::FeatureStepsNotFoundException, [klass, name])
Expand Down
8 changes: 4 additions & 4 deletions lib/spinach/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ module InstanceMethods
#
# @api public
def execute_step(step)
undercored_step = Spinach::Support.underscore(step)
underscored_step = Spinach::Support.underscore(step)
location = nil
if self.respond_to?(undercored_step)
location = method(undercored_step).source_location
self.send(undercored_step)
if self.respond_to?(underscored_step)
location = method(underscored_step).source_location
self.send(underscored_step)
else
raise Spinach::StepNotDefinedException.new(step)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/spinach/feature_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FeatureSteps
#
# @api public
def self.inherited(base)
Spinach.features << base
Spinach.feature_steps << base
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/spinach/generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Spinach
module Generators
# Binds the feature generator to the "feature not found" hook
def self.bind
Spinach::Runner::Feature.when_not_found do |data|
Spinach::Runner::FeatureRunner.when_not_found do |data|
Spinach::Generators.generate_feature(data)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/spinach/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def bind
scenario_runner.after_run method(:clear_current_scenario)
end

def feature_runner; Runner::Feature; end
def scenario_runner; Runner::Scenario; end
def feature_runner; Runner::FeatureRunner; end
def scenario_runner; Runner::ScenarioRunner; end
def runner; Runner; end

def after_run(*args); end;
Expand Down
6 changes: 3 additions & 3 deletions lib/spinach/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def run
successful = true

filenames.each do |filename|
success = Feature.new(filename).run
success = FeatureRunner.new(filename).run
successful = false unless success
end

Expand Down Expand Up @@ -113,5 +113,5 @@ def support_files
end
end

require_relative 'runner/feature'
require_relative 'runner/scenario'
require_relative 'runner/feature_runner'
require_relative 'runner/scenario_runner'
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ module Spinach
class Runner
# A feature runner handles a particular feature run.
#
class Feature
class FeatureRunner
include Hooks

# The {Reporter} used in this feature.
attr_reader :reporter

# The file that describes the feature.
attr_reader :filename

Expand Down Expand Up @@ -66,7 +63,7 @@ def run

scenarios.each do |scenario|
if !@scenario_line || scenario['line'].to_s == @scenario_line
success = Scenario.new(feature_name, scenario).run
success = ScenarioRunner.new(feature_name, scenario).run
@failed = true unless success
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Spinach
class Runner
# A Scenario Runner handles a particular scenario run.
#
class Scenario
attr_reader :feature, :feature_name, :data
class ScenarioRunner
attr_reader :feature_name, :data

include Hooks

Expand Down Expand Up @@ -37,21 +37,21 @@ def steps
# The feature object used to run this scenario.
#
# @api public
def feature
@feature ||= Spinach.find_feature(feature_name).new
def feature_steps
@feature_steps ||= Spinach.find_feature_steps(feature_name).new
end

# Runs this scenario
# @return [True, False]
# true if this scenario succeeded, false if not
def run
run_hook :before_run, data
feature.run_hook :before_scenario, data
feature_steps.run_hook :before_scenario, data
steps.each do |step|
feature.run_hook :before_step, step
feature_steps.run_hook :before_step, step
unless @exception
begin
step_location = feature.execute_step(step['name'])
step_location = feature_steps.execute_step(step['name'])
run_hook :on_successful_step, step, step_location
rescue *Spinach.config[:failure_exceptions] => e
@exception = e
Expand All @@ -66,9 +66,9 @@ def run
else
run_hook :on_skipped_step, step
end
feature.run_hook :after_step, step
feature_steps.run_hook :after_step, step
end
feature.run_hook :after_scenario, data
feature_steps.run_hook :after_scenario, data
run_hook :after_run, data
!@exception
end
Expand Down
4 changes: 2 additions & 2 deletions test/spinach/capybara_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def go_home
end

it 'resets the capybara session after each scenario' do
@feature_runner = Spinach::Runner::Feature.new(
@feature_runner = Spinach::Runner::FeatureRunner.new(
'a_feature.feature')

@feature_runner.stubs(data: Spinach::Parser.new('
Expand All @@ -48,7 +48,7 @@ def go_home
Then Goodbye
').parse).at_least_once

Spinach::Runner::Scenario.any_instance.stubs(feature: @feature)
Spinach::Runner::ScenarioRunner.any_instance.stubs(feature_steps: @feature)

Capybara.current_session.expects(:reset!).twice

Expand Down
14 changes: 7 additions & 7 deletions test/spinach/feature_steps_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
describe 'class methods' do
describe '#inherited' do
it 'registers any feature subclass' do
@feature1 = Class.new(Spinach::FeatureSteps)
@feature2 = Class.new(Spinach::FeatureSteps)
@feature3 = Class.new
@feature_steps1 = Class.new(Spinach::FeatureSteps)
@feature_steps2 = Class.new(Spinach::FeatureSteps)
@feature_steps3 = Class.new

Spinach.features.must_include @feature1
Spinach.features.must_include @feature2
Spinach.features.wont_include @feature3
Spinach.feature_steps.must_include @feature_steps1
Spinach.feature_steps.must_include @feature_steps2
Spinach.feature_steps.wont_include @feature_steps3
end
end
end
Expand Down Expand Up @@ -55,7 +55,7 @@
end
end

Spinach.features.must_include feature
Spinach.feature_steps.must_include feature

instance = feature.new
instance.execute_step('Test')
Expand Down
2 changes: 1 addition & 1 deletion test/spinach/generators/feature_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ module Spinach::Generators
it "generates an entire feature_steps class definition" do
result = subject.generate
klass = eval(result)
feature_runner = Spinach::Runner::Feature.new(stub_everything)
feature_runner = Spinach::Runner::FeatureRunner.new(stub_everything)
feature_runner.stubs(data: data)
feature_runner.run.must_equal true
end
Expand Down
4 changes: 2 additions & 2 deletions test/spinach/generators_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
it "binds the generator to the missing feature hook" do
subject.expects(:generate_feature).with(data)
subject.bind
Spinach::Runner::Feature.new(stub_everything).run_hook :when_not_found, data
Spinach::Runner::Feature._when_not_found_callbacks = []
Spinach::Runner::FeatureRunner.new(stub_everything).run_hook :when_not_found, data
Spinach::Runner::FeatureRunner._when_not_found_callbacks = []
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
require_relative '../../test_helper'

describe Spinach::Runner::Feature do
describe Spinach::Runner::FeatureRunner do
let(:filename) { 'feature/a_cool_feature.feature' }
let(:feature) { Spinach::Runner::Feature.new(filename) }
subject{ Spinach::Runner::FeatureRunner.new(filename) }

describe '#initialize' do
it 'initializes the given filename' do
feature.filename.must_equal filename
subject.filename.must_equal filename
end

it 'initalizes the given scenario line' do
@filename = 'feature/a_cool_feature.feature:12'
@feature = Spinach::Runner::Feature.new(@filename)
@feature = Spinach::Runner::FeatureRunner.new(@filename)

@feature.instance_variable_get(:@scenario_line).must_equal '12'
end
Expand All @@ -22,77 +22,75 @@
parsed_data = {name: 'A cool feature'}
parser = stub(parse: parsed_data)
Spinach::Parser.expects(:open_file).returns(parser)
feature.data.must_equal parsed_data
subject.data.must_equal parsed_data
end
end

describe '#scenarios' do
it 'returns the parsed scenarios' do
feature.stubs(data: {'elements' => [1, 2, 3]})
feature.scenarios.must_equal [1,2,3]
subject.stubs(data: {'elements' => [1, 2, 3]})
subject.scenarios.must_equal [1,2,3]
end
end

describe '#run' do
before do
feature.stubs(data: {
subject.stubs(data: {
'name' => 'A cool feature',
'elements' => [{'keyword'=>'Scenario', 'name'=>'Basic guess', 'line'=>6, 'description'=>'', 'type'=>'scenario'},
{'keyword'=>'Scenario', 'name'=>'Basic guess II', 'line'=>12, 'description'=>'', 'type'=>'scenario'},
{'keyword'=>'Scenario', 'name'=>'Basic guess III', 'line'=>18, 'description'=>'', 'type'=>'scenario'}]
})
feature.stubs(feature: stub_everything)
subject.stubs(feature: stub_everything)
end

it 'calls the steps as expected' do
seq = sequence('feature')
3.times do
Spinach::Runner::Scenario.
Spinach::Runner::ScenarioRunner.
expects(:new).
returns(stub_everything).
in_sequence(seq)
end
feature.run
subject.run
end

it 'returns true if the execution succeeds' do
Spinach::Runner::Scenario.any_instance.
Spinach::Runner::ScenarioRunner.any_instance.
expects(run: true).times(3)
feature.run.must_equal true
subject.run.must_equal true
end

it 'returns false if the execution fails' do
Spinach::Runner::Scenario.any_instance.
Spinach::Runner::ScenarioRunner.any_instance.
expects(run: false).times(3)
feature.run.must_equal false
subject.run.must_equal false
end

it 'calls only the given scenario' do
@filename = 'feature/a_cool_feature.feature:12'
@feature = Spinach::Runner::Feature.new(@filename)
@feature = Spinach::Runner::FeatureRunner.new(@filename)
@feature.stubs(data: {
'name' => 'A cool feature',
'elements' => [{'keyword'=>'Scenario', 'name'=>'Basic guess', 'line'=>6, 'description'=>'', 'type'=>'scenario'},
{'keyword'=>'Scenario', 'name'=>'Basic guess II', 'line'=>12, 'description'=>'', 'type'=>'scenario'},
{'keyword'=>'Scenario', 'name'=>'Basic guess III', 'line'=>18, 'description'=>'', 'type'=>'scenario'}]
})
@feature.stubs(feature: stub_everything)

Spinach::Runner::Scenario.expects(:new).with(anything, @feature.scenarios[1], anything).once.returns(stub_everything)
Spinach::Runner::ScenarioRunner.expects(:new).with(anything, @feature.scenarios[1], anything).once.returns(stub_everything)
@feature.run
end

it "fires a hook if the feature is not defined" do
feature = Spinach::Runner::Feature.new(filename)
data = mock
exception = Spinach::FeatureStepsNotFoundException.new([anything, anything])
feature.stubs(:scenarios).raises(exception)
feature.stubs(:data).returns(data)
subject.stubs(:scenarios).raises(exception)
subject.stubs(:data).returns(data)
not_found_called = false
feature.class.when_not_found do |data, exception|
subject.class.when_not_found do |data, exception|
not_found_called = [data, exception]
end
feature.run
subject.run
not_found_called.must_equal [data, exception]
end
end
Expand Down
Loading

0 comments on commit bc4c2ef

Please sign in to comment.