From e7208d382a3d8bae9ab13d8a380b1a2a05fd99b0 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 22 Nov 2008 19:18:30 -0800 Subject: Get ActiveSupport::TestCase working with classic Test::Unit and MiniTest. Fix broken Mocha + MiniTest. Assume ruby-core applies patch #771 fixing libraries which extend Test::Unit. --- activesupport/lib/active_support/test_case.rb | 56 +++++------ .../testing/mocha_minitest_adapter.rb | 45 +++++++++ .../active_support/testing/setup_and_teardown.rb | 112 +++++++-------------- 3 files changed, 107 insertions(+), 106 deletions(-) create mode 100644 activesupport/lib/active_support/testing/mocha_minitest_adapter.rb (limited to 'activesupport') diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb index d1e657c15f..a4a45079fa 100644 --- a/activesupport/lib/active_support/test_case.rb +++ b/activesupport/lib/active_support/test_case.rb @@ -1,46 +1,40 @@ +require 'test/unit/testcase' require 'active_support/testing/setup_and_teardown' require 'active_support/testing/assertions' require 'active_support/testing/declarative' -module ActiveSupport - # Prefer MiniTest with Test::Unit compatibility. - begin - require 'minitest/unit' +begin + gem 'mocha', '>= 0.9.0' + require 'mocha' - # Hack around the test/unit autorun. - autorun_enabled = MiniTest::Unit.send(:class_variable_get, '@@installed_at_exit') - if MiniTest::Unit.respond_to?(:disable_autorun) - MiniTest::Unit.disable_autorun - else - MiniTest::Unit.send(:class_variable_set, '@@installed_at_exit', false) - end - require 'test/unit' - MiniTest::Unit.send(:class_variable_set, '@@installed_at_exit', autorun_enabled) + if defined?(MiniTest) + require 'active_support/testing/mocha_minitest_adapter' + end +rescue LoadError + # Fake Mocha::ExpectationError so we can rescue it in #run. Bleh. + Object.const_set :Mocha, Module.new + Mocha.const_set :ExpectationError, Class.new(StandardError) +end - class TestCase < ::Test::Unit::TestCase +module ActiveSupport + class TestCase < ::Test::Unit::TestCase + if defined? MiniTest Assertion = MiniTest::Assertion - end - - # TODO: Figure out how to get the Rails::BacktraceFilter into minitest/unit - # Test::Unit compatibility. - rescue LoadError - require 'test/unit/testcase' - require 'active_support/testing/default' - - if defined?(Rails) - require 'rails/backtrace_cleaner' - Test::Unit::Util::BacktraceFilter.module_eval { include Rails::BacktraceFilterForTestUnit } - end + else + # TODO: Figure out how to get the Rails::BacktraceFilter into minitest/unit + if defined?(Rails) + require 'rails/backtrace_cleaner' + Test::Unit::Util::BacktraceFilter.module_eval { include Rails::BacktraceFilterForTestUnit } + end - class TestCase < ::Test::Unit::TestCase Assertion = Test::Unit::AssertionFailedError + + require 'active_support/testing/default' include ActiveSupport::Testing::Default - end - end + end - class TestCase include ActiveSupport::Testing::SetupAndTeardown include ActiveSupport::Testing::Assertions extend ActiveSupport::Testing::Declarative end -end \ No newline at end of file +end diff --git a/activesupport/lib/active_support/testing/mocha_minitest_adapter.rb b/activesupport/lib/active_support/testing/mocha_minitest_adapter.rb new file mode 100644 index 0000000000..a96ce74526 --- /dev/null +++ b/activesupport/lib/active_support/testing/mocha_minitest_adapter.rb @@ -0,0 +1,45 @@ +class MiniTest::Unit::TestCase + include Mocha::Standalone + + class MochaAssertionCounter + def initialize(runner) @runner = runner end + def increment; @runner.assertion_count += 1 end + end + + def run(runner) + assertion_counter = MochaAssertionCounter.new(runner) + result = '.' + begin + begin + @passed = nil + setup + __send__ name + mocha_verify(assertion_counter) + @passed = true + rescue Exception => e + @passed = false + result = runner.puke(self.class, self.name, e) + ensure + begin + teardown + rescue Exception => e + result = runner.puke(self.class, self.name, e) + end + end + ensure + mocha_teardown + end + result + end +end + +module Test + module Unit + remove_const :TestCase + + class TestCase < MiniTest::Unit::TestCase + include Test::Unit::Assertions + def self.test_order; :sorted end + end + end +end diff --git a/activesupport/lib/active_support/testing/setup_and_teardown.rb b/activesupport/lib/active_support/testing/setup_and_teardown.rb index 595178fbd4..245f57a7b0 100644 --- a/activesupport/lib/active_support/testing/setup_and_teardown.rb +++ b/activesupport/lib/active_support/testing/setup_and_teardown.rb @@ -3,30 +3,15 @@ require 'active_support/callbacks' module ActiveSupport module Testing module SetupAndTeardown - # For compatibility with Ruby < 1.8.6 - PASSTHROUGH_EXCEPTIONS = - if defined?(Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS) - Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS - else - [NoMemoryError, SignalException, Interrupt, SystemExit] - end - def self.included(base) base.class_eval do include ActiveSupport::Callbacks define_callbacks :setup, :teardown - if defined?(::MiniTest) + if defined? MiniTest include ForMiniTest else - begin - require 'mocha' - undef_method :run - alias_method :run, :run_with_callbacks_and_mocha - rescue LoadError - undef_method :run - alias_method :run, :run_with_callbacks_and_testunit - end + include ForClassicTestUnit end end end @@ -50,74 +35,51 @@ module ActiveSupport end end - # This redefinition is unfortunate but test/unit shows us no alternative. - def run_with_callbacks_and_testunit(result) #:nodoc: - return if @method_name.to_s == "default_test" + module ForClassicTestUnit + # For compatibility with Ruby < 1.8.6 + PASSTHROUGH_EXCEPTIONS = Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS rescue [NoMemoryError, SignalException, Interrupt, SystemExit] - yield(Test::Unit::TestCase::STARTED, name) - @_result = result - begin - run_callbacks :setup - setup - __send__(@method_name) - rescue Test::Unit::AssertionFailedError => e - add_failure(e.message, e.backtrace) - rescue *PASSTHROUGH_EXCEPTIONS - raise - rescue Exception - add_error($!) - ensure - begin - teardown - run_callbacks :teardown, :enumerator => :reverse_each - rescue Test::Unit::AssertionFailedError => e - add_failure(e.message, e.backtrace) - rescue *PASSTHROUGH_EXCEPTIONS - raise - rescue Exception - add_error($!) - end - end - result.add_run - yield(Test::Unit::TestCase::FINISHED, name) - end + # This redefinition is unfortunate but test/unit shows us no alternative. + # Doubly unfortunate: hax to support Mocha's hax. + def run(result) + return if @method_name.to_s == "default_test" - # Doubly unfortunate: mocha does the same so we have to hax their hax. - def run_with_callbacks_and_mocha(result) - return if @method_name.to_s == "default_test" + if using_mocha = respond_to?(:mocha_verify) + assertion_counter = Mocha::TestCaseAdapter::AssertionCounter.new(result) + end - assertion_counter = Mocha::TestCaseAdapter::AssertionCounter.new(result) - yield(Test::Unit::TestCase::STARTED, name) - @_result = result - begin + yield(Test::Unit::TestCase::STARTED, name) + @_result = result begin - run_callbacks :setup - setup - __send__(@method_name) - mocha_verify(assertion_counter) - rescue Mocha::ExpectationError => e - add_failure(e.message, e.backtrace) - rescue Test::Unit::AssertionFailedError => e - add_failure(e.message, e.backtrace) - rescue Exception - raise if Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS.include? $!.class - add_error($!) - ensure begin - teardown - run_callbacks :teardown, :enumerator => :reverse_each + run_callbacks :setup + setup + __send__(@method_name) + mocha_verify(assertion_counter) if using_mocha + rescue Mocha::ExpectationError => e + add_failure(e.message, e.backtrace) rescue Test::Unit::AssertionFailedError => e add_failure(e.message, e.backtrace) - rescue Exception - raise if Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS.include? $!.class - add_error($!) + rescue Exception => e + raise if PASSTHROUGH_EXCEPTIONS.include?(e.class) + add_error(e) + ensure + begin + teardown + run_callbacks :teardown, :enumerator => :reverse_each + rescue Test::Unit::AssertionFailedError => e + add_failure(e.message, e.backtrace) + rescue Exception => e + raise if PASSTHROUGH_EXCEPTIONS.include?(e.class) + add_error(e) + end end + ensure + mocha_teardown if using_mocha end - ensure - mocha_teardown + result.add_run + yield(Test::Unit::TestCase::FINISHED, name) end - result.add_run - yield(Test::Unit::TestCase::FINISHED, name) end end end -- cgit v1.2.3