diff options
author | José Valim <jose.valim@gmail.com> | 2009-12-30 11:09:27 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-12-30 11:09:27 +0100 |
commit | e58ad8ed9cb0e67fde93c7edc8a0e13b8bc342ae (patch) | |
tree | 96b719af681337ba7020b5e2d58b73e25a987696 | |
parent | 97204fc0bc52af8fb6714e6f9fcd414567e0fc1a (diff) | |
download | rails-e58ad8ed9cb0e67fde93c7edc8a0e13b8bc342ae.tar.gz rails-e58ad8ed9cb0e67fde93c7edc8a0e13b8bc342ae.tar.bz2 rails-e58ad8ed9cb0e67fde93c7edc8a0e13b8bc342ae.zip |
Setup and teardown now use new callbacks.
-rw-r--r-- | activesupport/lib/active_support/callbacks.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/testing/setup_and_teardown.rb | 73 | ||||
-rw-r--r-- | activesupport/test/test_test.rb | 9 |
3 files changed, 52 insertions, 32 deletions
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 97e2a478b5..05bc453dbf 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -90,7 +90,7 @@ module ActiveSupport class Callback @@_callback_sequence = 0 - attr_accessor :chain, :filter, :kind, :options, :per_key, :klass + attr_accessor :chain, :filter, :kind, :options, :per_key, :klass, :raw_filter def initialize(chain, filter, kind, options, klass) @chain, @kind, @klass = chain, kind, klass diff --git a/activesupport/lib/active_support/testing/setup_and_teardown.rb b/activesupport/lib/active_support/testing/setup_and_teardown.rb index 0e998d2dbe..6ce9495cee 100644 --- a/activesupport/lib/active_support/testing/setup_and_teardown.rb +++ b/activesupport/lib/active_support/testing/setup_and_teardown.rb @@ -1,16 +1,26 @@ module ActiveSupport module Testing module SetupAndTeardown - def self.included(base) - base.class_eval do - include ActiveSupport::DeprecatedCallbacks - define_callbacks :setup, :teardown + extend ActiveSupport::Concern - if defined?(MiniTest::Assertions) && TestCase < MiniTest::Assertions - include ForMiniTest - else - include ForClassicTestUnit - end + included do + include ActiveSupport::Callbacks + define_callbacks :setup, :teardown + + if defined?(MiniTest::Assertions) && TestCase < MiniTest::Assertions + include ForMiniTest + else + include ForClassicTestUnit + end + end + + module ClassMethods + def setup(*args, &block) + set_callback(:setup, :before, *args, &block) + end + + def teardown(*args, &block) + set_callback(:teardown, :after, *args, &block) end end @@ -18,13 +28,14 @@ module ActiveSupport def run(runner) result = '.' begin - run_callbacks :setup - result = super + _run_setup_callbacks do + result = super + end rescue Exception => e result = runner.puke(self.class, method_name, e) ensure begin - run_callbacks :teardown, :enumerator => :reverse_each + _run_teardown_callbacks rescue Exception => e result = runner.puke(self.class, method_name, e) end @@ -42,23 +53,17 @@ module ActiveSupport def run(result) return if @method_name.to_s == "default_test" - if using_mocha = respond_to?(:mocha_verify) - assertion_counter_klass = if defined?(Mocha::TestCaseAdapter::AssertionCounter) - Mocha::TestCaseAdapter::AssertionCounter - else - Mocha::Integration::TestUnit::AssertionCounter - end - assertion_counter = assertion_counter_klass.new(result) - end - + mocha_counter = retrieve_mocha_counter(result) yield(Test::Unit::TestCase::STARTED, name) @_result = result + begin begin - run_callbacks :setup - setup - __send__(@method_name) - mocha_verify(assertion_counter) if using_mocha + _run_setup_callbacks do + setup + __send__(@method_name) + mocha_verify(mocha_counter) if mocha_counter + end rescue Mocha::ExpectationError => e add_failure(e.message, e.backtrace) rescue Test::Unit::AssertionFailedError => e @@ -69,7 +74,7 @@ module ActiveSupport ensure begin teardown - run_callbacks :teardown, :enumerator => :reverse_each + _run_teardown_callbacks rescue Test::Unit::AssertionFailedError => e add_failure(e.message, e.backtrace) rescue Exception => e @@ -78,12 +83,26 @@ module ActiveSupport end end ensure - mocha_teardown if using_mocha + mocha_teardown if mocha_counter end + result.add_run yield(Test::Unit::TestCase::FINISHED, name) end + + protected + + def retrieve_mocha_counter(result) #:nodoc: + if using_mocha = respond_to?(:mocha_verify) + if defined?(Mocha::TestCaseAdapter::AssertionCounter) + Mocha::TestCaseAdapter::AssertionCounter.new(result) + else + Mocha::Integration::TestUnit::AssertionCounter.new(result) + end + end + end end + end end end diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 5cbffb81fc..1928da51ca 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -102,9 +102,9 @@ class SetupAndTeardownTest < ActiveSupport::TestCase teardown :foo, :sentinel, :foo def test_inherited_setup_callbacks - assert_equal [:reset_callback_record, :foo], self.class.setup_callback_chain.map(&:method) + assert_equal [:reset_callback_record, :foo], self.class._setup_callbacks.map(&:raw_filter) assert_equal [:foo], @called_back - assert_equal [:foo, :sentinel, :foo], self.class.teardown_callback_chain.map(&:method) + assert_equal [:foo, :sentinel, :foo], self.class._teardown_callbacks.map(&:raw_filter) end def setup @@ -114,6 +114,7 @@ class SetupAndTeardownTest < ActiveSupport::TestCase end protected + def reset_callback_record @called_back = [] end @@ -133,9 +134,9 @@ class SubclassSetupAndTeardownTest < SetupAndTeardownTest teardown :bar def test_inherited_setup_callbacks - assert_equal [:reset_callback_record, :foo, :bar], self.class.setup_callback_chain.map(&:method) + assert_equal [:reset_callback_record, :foo, :bar], self.class._setup_callbacks.map(&:raw_filter) assert_equal [:foo, :bar], @called_back - assert_equal [:foo, :sentinel, :foo, :bar], self.class.teardown_callback_chain.map(&:method) + assert_equal [:foo, :sentinel, :foo, :bar], self.class._teardown_callbacks.map(&:raw_filter) end protected |