From e58ad8ed9cb0e67fde93c7edc8a0e13b8bc342ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 30 Dec 2009 11:09:27 +0100 Subject: Setup and teardown now use new callbacks. --- .../active_support/testing/setup_and_teardown.rb | 73 ++++++++++++++-------- 1 file changed, 46 insertions(+), 27 deletions(-) (limited to 'activesupport/lib/active_support/testing/setup_and_teardown.rb') 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 -- cgit v1.2.3