Skip to content

Commit

Permalink
End refactor - missing docs
Browse files Browse the repository at this point in the history
  • Loading branch information
josepjaume committed Oct 13, 2011
1 parent 134147b commit 2c3f1d0
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 148 deletions.
11 changes: 2 additions & 9 deletions features/support/spinach_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@ module SpinachRunner
include Aruba::Api

def self.included(base)
base.class_eval do
before_scenario do
in_current_dir do
FileUtils.rm_rf("features")
end
end
before_scenario do
@aruba_timeout_seconds = 6
end
Spinach.hooks.before_scenario do
@aruba_timeout_seconds = 6
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::FeatureRunner.when_not_found do |data|
Spinach.hooks.on_undefined_feature do |data|
Spinach::Generators.generate_feature(data)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/spinach/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def reset

def run_hook(name, *args)
if callbacks = @hooks[name.to_sym]
callbacks.each{ |c| c.call(args) }
callbacks.each{ |c| c.call(*args) }
end
end

Expand Down
52 changes: 28 additions & 24 deletions lib/spinach/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,32 @@ def initialize(options = {})

# A Hash with options for the reporter
#
attr_reader :options

attr_accessor :current_feature, :current_scenario
attr_reader :options, :current_feature, :current_scenario

attr_reader :undefined_steps, :failed_steps, :error_steps, :undefined_features, :successful_steps

# Hooks the reporter to the runner endpoints
def bind
runner.after_run method(:after_run)
feature_runner.before_run method(:before_feature_run)
feature_runner.after_run method(:after_feature_run)
feature_runner.when_not_found method(:on_feature_not_found)
scenario_runner.before_run method(:before_scenario_run)
scenario_runner.after_run method(:after_scenario_run)
scenario_runner.on_successful_step method(:on_successful_step)
scenario_runner.on_undefined_step method(:on_undefined_step)
scenario_runner.on_failed_step method(:on_failed_step)
scenario_runner.on_error_step method(:on_error_step)
scenario_runner.on_skipped_step method(:on_skipped_step)
Spinach.hooks.tap do |hooks|
hooks.after_run { |*args| after_run(*args) }
hooks.before_feature { |*args| before_feature_run(*args) }
hooks.after_feature { |*args| after_feature_run(*args) }
hooks.on_undefined_feature { |*args| on_feature_not_found(*args) }
hooks.before_scenario { |*args| before_scenario_run(*args) }
hooks.after_scenario { |*args| after_scenario_run(*args) }
hooks.on_successful_step { |*args| on_successful_step(*args) }
hooks.on_undefined_step { |*args| on_undefined_step(*args) }
hooks.on_failed_step { |*args| on_failed_step(*args) }
hooks.on_error_step { |*args| on_error_step(*args) }
hooks.on_skipped_step { |*args| on_skipped_step(*args) }

feature_runner.before_run method(:current_feature=)
feature_runner.after_run method(:clear_current_feature)
scenario_runner.before_run method(:current_scenario=)
scenario_runner.after_run method(:clear_current_scenario)
hooks.before_feature { |*args| set_current_feature(*args) }
hooks.after_feature { |*args| clear_current_feature(*args) }
hooks.before_scenario { |*args| set_current_scenario(*args) }
hooks.after_scenario { |*args| clear_current_scenario(*args) }
end
end

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

def after_run(*args); end;
def before_feature_run(*args); end
def after_feature_run(*args); end
Expand All @@ -61,12 +57,20 @@ def on_error_step(*args); end;
def on_undefined_step(*args); end;
def on_skipped_step(*args); end;

def set_current_feature(data)
@current_feature = data
end

def clear_current_feature(*args)
self.current_feature = nil
@current_feature = nil
end

def set_current_scenario(data)
@current_scenario = data
end

def clear_current_scenario(*args)
self.current_scenario = nil
@current_scenario = nil
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/spinach/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def run
require_dependencies
require_suites

run_hook :before_run
Spinach.hooks.run_before_run

successful = true

Expand All @@ -65,7 +65,7 @@ def run
successful = false unless success
end

run_hook :after_run, successful
Spinach.hooks.run_after_run(successful)

successful
end
Expand Down
4 changes: 2 additions & 2 deletions lib/spinach/runner/feature_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def scenarios
#
# @api public
def run
Spinach.hooks.run_before_run data
Spinach.hooks.run_before_feature data

scenarios.each do |scenario|
if !@scenario_line || scenario['line'].to_s == @scenario_line
Expand All @@ -65,7 +65,7 @@ def run
Spinach.hooks.run_on_undefined_feature data, e
@failed = true
ensure
Spinach.hooks.run_after_run data
Spinach.hooks.run_after_feature data
return !@failed
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/spinach/runner/scenario_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ def feature_steps
# true if this scenario succeeded, false if not
def run
Spinach.hooks.run_before_scenario data
feature_steps.run_hook :before_scenario, data
steps.each do |step|
Spinach.hooks.run_before_step step
unless @exception
begin
step_location = feature_steps.execute_step(step['name'])
Spinach.hooks.run_on_successful_step step, step_location
rescue Spinach::FeatureStepsNotFoundException => e
raise e
rescue *Spinach.config[:failure_exceptions] => e
@exception = e
Spinach.hooks.run_on_failed_step step, @exception, step_location
Expand Down
3 changes: 2 additions & 1 deletion test/spinach/capybara_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'test_helper'
require 'spinach'
require 'spinach/capybara'
require 'sinatra'

Expand Down Expand Up @@ -50,7 +51,7 @@ def go_home

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

Capybara.current_session.expects(:reset!).times(3)
Capybara.current_session.expects(:reset!).at_least_once

@feature_runner.run
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::FeatureRunner.new(stub_everything).run_hook :when_not_found, data
Spinach::Runner::FeatureRunner._when_not_found_callbacks = []
Spinach.hooks.run_on_undefined_feature data
Spinach.hooks.reset
end
end

Expand Down
161 changes: 58 additions & 103 deletions test/spinach/reporter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ module Spinach
@reporter = Reporter.new(@options)
end

describe "#initialize" do
it "initializes the option hash" do
end
end

describe "#options" do
it "returns the options passed to the reporter" do
@reporter.options[:backtrace].must_equal true
Expand Down Expand Up @@ -52,124 +47,84 @@ module Spinach
@reporter.stubs(:method)
end

it "binds a callback after running all the suite" do
@reporter.expects(:method).with(:after_run).returns(@callback)
@reporter.runner.expects(:after_run).with(@callback)
@reporter.bind
end

it "binds a callback before running every feature" do
@reporter.expects(:method).with(:before_feature_run).returns(@callback)
@reporter.feature_runner.expects(:before_run).with(@callback)
@reporter.bind
end

it "binds a callback after running every feature" do
@reporter.expects(:method).with(:after_feature_run).returns(@callback)
@reporter.feature_runner.expects(:after_run).with(@callback)
@reporter.bind
end

it "binds a callback for not defined features" do
@reporter.expects(:method).with(:on_feature_not_found).returns(@callback)
@reporter.feature_runner.expects(:when_not_found).with(@callback)
@reporter.bind
end

it "binds a callback before running every scenario" do
@reporter.expects(:method).with(:before_scenario_run).returns(@callback)
@reporter.scenario_runner.expects(:before_run).with(@callback)
@reporter.bind
end
describe "bindings" do
before do
@reporter.bind
end

it "binds a callback after running every feature" do
@reporter.expects(:method).with(:after_scenario_run).returns(@callback)
@reporter.scenario_runner.expects(:after_run).with(@callback)
@reporter.bind
end
after do
Spinach.hooks.reset
end

describe "when running steps" do
%w{successful failed error undefined skipped}.each do |type|
it "binds a callback after running a #{type} step" do
@reporter.expects(:method).with(:"on_#{type}_step").returns(@callback)
@reporter.scenario_runner.expects(:"on_#{type}_step").with(@callback)
@reporter.bind
end
it "binds a callback after running" do
@reporter.expects(:after_run)
Spinach.hooks.run_after_run
end
end

describe "binds the context methods" do
it "binds the current feature setter" do
@reporter.expects(:method).with(:current_feature=).returns(@callback)
@reporter.feature_runner.expects(:before_run).with(@callback)
@reporter.bind
it "binds a callback before running every feature" do
@reporter.expects(:before_feature_run)
Spinach.hooks.run_before_feature(anything)
end

it "binds the current feature clearer" do
@reporter.expects(:method).with(:clear_current_feature).returns(@callback)
@reporter.feature_runner.expects(:after_run).with(@callback)
@reporter.bind
it "binds a callback after running every feature" do
@reporter.expects(:after_feature_run)
Spinach.hooks.run_after_feature
end

it "binds the current scenario setter" do
@reporter.expects(:method).with(:current_scenario=).returns(@callback)
@reporter.scenario_runner.expects(:before_run).with(@callback)
@reporter.bind
it "binds a callback when a feature is not found" do
@reporter.expects(:on_feature_not_found)
Spinach.hooks.run_on_undefined_feature
end

it "binds the current feature clearer" do
@reporter.expects(:method).with(:clear_current_scenario).returns(@callback)
@reporter.scenario_runner.expects(:after_run).with(@callback)
@reporter.bind
it "binds a callback before every scenario" do
@reporter.expects(:before_scenario_run)
Spinach.hooks.run_before_scenario(anything)
end
end
end

describe "#clear_current_feature" do
it "clears the current feature" do
@reporter.current_feature = mock
@reporter.clear_current_feature
@reporter.current_feature.must_equal nil
end
end
it "binds a callback after every scenario" do
@reporter.expects(:after_scenario_run)
Spinach.hooks.run_after_scenario
end

describe "#clear_current_scenario" do
it "clears the current scenario" do
@reporter.current_scenario = mock
@reporter.clear_current_scenario
@reporter.current_scenario.must_equal nil
end
end
it "binds a callback after every successful step" do
@reporter.expects(:on_successful_step)
Spinach.hooks.run_on_successful_step
end

describe "runner classes" do
describe "#feature_runner" do
it "returns a runner class" do
@reporter.feature_runner.must_be_kind_of Class
it "binds a callback after every failed step" do
@reporter.expects(:on_failed_step)
Spinach.hooks.run_on_failed_step
end
end

describe "#scenario_runner" do
it "returns a runner class" do
@reporter.scenario_runner.must_be_kind_of Class
it "binds a callback after every error step" do
@reporter.expects(:on_error_step)
Spinach.hooks.run_on_error_step
end
end

describe "#runner" do
it "returns a runner class" do
@reporter.runner.must_be_kind_of Class
it "binds a callback after every skipped step" do
@reporter.expects(:on_skipped_step)
Spinach.hooks.run_on_skipped_step
end
end
end

describe "callback methods" do
%w{
after_run before_feature_run after_feature_run before_scenario_run
after_scenario_run on_successful_step on_failed_step on_error_step
on_undefined_step on_skipped_step
}.each do |callback|
describe "##{callback}" do
it "does nothing" do
@reporter.send(callback)
describe "internals" do
it "binds a callback before running" do
@reporter.expects(:set_current_feature)
Spinach.hooks.run_before_feature({})
end

it "binds a callback after running" do
@reporter.expects(:clear_current_feature)
Spinach.hooks.run_after_feature
end

it "binds a callback before every scenario" do
@reporter.expects(:set_current_scenario)
Spinach.hooks.run_before_scenario
end

it "binds a callback after every scenario" do
@reporter.expects(:clear_current_scenario)
Spinach.hooks.run_after_scenario
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions test/spinach/runner/scenario_runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@
Spinach::StepNotDefinedException.new('bar'))
Spinach.hooks.expects("run_before_scenario").with(has_value("A cool scenario"))
Spinach.hooks.expects("run_after_scenario").with(has_value("A cool scenario"))
Spinach.hooks.expects("run_on_undefined_step").with(
anything, anything, anything)
Spinach.hooks.expects("run_on_undefined_step").with(
anything, anything, anything)
Spinach.hooks.expects("run_on_skipped_step").with(
Expand Down

0 comments on commit 2c3f1d0

Please sign in to comment.