Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rework testing extensions to reflect the recent miniunit upheaval

  • Loading branch information...
commit 18099b0fd53b8f9ba88ef46bdd910347f03c72c5 1 parent 7b28a55
@jeremy jeremy authored
View
44 activesupport/lib/active_support/test_case.rb
@@ -1,28 +1,32 @@
-require 'test/unit/testcase'
require 'active_support/testing/setup_and_teardown'
require 'active_support/testing/assertions'
-require 'active_support/testing/default'
+require 'active_support/testing/declarative'
module ActiveSupport
- class TestCase < ::Test::Unit::TestCase
- include ActiveSupport::Testing::SetupAndTeardown
- include ActiveSupport::Testing::Assertions
- include ActiveSupport::Testing::Default
+ # Prefer MiniTest with Test::Unit compatibility.
+ # Hacks around the test/unit autorun.
+ begin
+ require 'minitest/unit'
+ MiniTest::Unit.disable_autorun
+ require 'test/unit'
+
+ class TestCase < ::Test::Unit::TestCase
+ @@installed_at_exit = false
+ end
+
+ # Test::Unit compatibility.
+ rescue LoadError
+ require 'test/unit/testcase'
+ require 'active_support/testing/default'
- # test "verify something" do
- # ...
- # end
- def self.test(name, &block)
- test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
- defined = instance_method(test_name) rescue false
- raise "#{test_name} is already defined in #{self}" if defined
- if block_given?
- define_method(test_name, &block)
- else
- define_method(test_name) do
- flunk "No implementation provided for #{name}"
- end
- end
+ class TestCase < ::Test::Unit::TestCase
+ include ActiveSupport::Testing::Default
end
end
+
+ class TestCase
+ include ActiveSupport::Testing::SetupAndTeardown
+ include ActiveSupport::Testing::Assertions
+ extend ActiveSupport::Testing::Declarative
+ end
end
View
2  activesupport/lib/active_support/testing/assertions.rb
@@ -32,7 +32,7 @@ module Assertions
# post :delete, :id => ...
# end
def assert_difference(expressions, difference = 1, message = nil, &block)
- expression_evaluations = Array(expressions).collect{ |expression| lambda { eval(expression, block.send!(:binding)) } }
+ expression_evaluations = Array(expressions).collect{ |expression| lambda { eval(expression, block.send(:binding)) } }
original_values = expression_evaluations.inject([]) { |memo, expression| memo << expression.call }
yield
View
28 activesupport/lib/active_support/testing/setup_and_teardown.rb
@@ -14,9 +14,8 @@ def self.included(base)
include ActiveSupport::Callbacks
define_callbacks :setup, :teardown
- if defined?(::Mini)
- undef_method :run
- alias_method :run, :run_with_callbacks_and_miniunit
+ if defined?(::MiniTest)
+ include ForMiniTest
else
begin
require 'mocha'
@@ -30,22 +29,23 @@ def self.included(base)
end
end
- def run_with_callbacks_and_miniunit(runner)
- result = '.'
- begin
- run_callbacks :setup
- result = super
- rescue Exception => e
- result = runner.puke(self.class, self.name, e)
- ensure
+ module ForMiniTest
+ def run(runner)
+ result = '.'
begin
- teardown
- run_callbacks :teardown, :enumerator => :reverse_each
+ run_callbacks :setup
+ result = super
rescue Exception => e
result = runner.puke(self.class, self.name, e)
+ ensure
+ begin
+ run_callbacks :teardown, :enumerator => :reverse_each
+ rescue Exception => e
+ result = runner.puke(self.class, self.name, e)
+ end
end
+ result
end
- result
end
# This redefinition is unfortunate but test/unit shows us no alternative.
View
8 activesupport/test/core_ext/module/model_naming_test.rb
@@ -2,18 +2,18 @@
class ModelNamingTest < Test::Unit::TestCase
def setup
- @name = ActiveSupport::ModelName.new('Post::TrackBack')
+ @model_name = ActiveSupport::ModelName.new('Post::TrackBack')
end
def test_singular
- assert_equal 'post_track_back', @name.singular
+ assert_equal 'post_track_back', @model_name.singular
end
def test_plural
- assert_equal 'post_track_backs', @name.plural
+ assert_equal 'post_track_backs', @model_name.plural
end
def test_partial_path
- assert_equal 'post/track_backs/track_back', @name.partial_path
+ assert_equal 'post/track_backs/track_back', @model_name.partial_path
end
end
View
24 activesupport/test/deprecation_test.rb
@@ -143,19 +143,21 @@ def test_deprecation_with_explicit_message
assert_deprecated(/you now need to do something extra for this one/) { @dtc.d }
end
- def test_assertion_failed_error_doesnt_spout_deprecation_warnings
- error_class = Class.new(StandardError) do
- def message
- ActiveSupport::Deprecation.warn 'warning in error message'
- super
+ unless defined?(::MiniTest)
+ def test_assertion_failed_error_doesnt_spout_deprecation_warnings
+ error_class = Class.new(StandardError) do
+ def message
+ ActiveSupport::Deprecation.warn 'warning in error message'
+ super
+ end
end
- end
- raise error_class.new('hmm')
+ raise error_class.new('hmm')
- rescue => e
- error = Test::Unit::Error.new('testing ur doodz', e)
- assert_not_deprecated { error.message }
- assert_nil @last_message
+ rescue => e
+ error = Test::Unit::Error.new('testing ur doodz', e)
+ assert_not_deprecated { error.message }
+ assert_nil @last_message
+ end
end
end
View
10 activesupport/test/test_test.rb
@@ -66,6 +66,8 @@ def test_array_of_expressions_identify_failure
@object.increment
end
fail 'should not get to here'
+ rescue MiniTest::Assertion => e
+ assert_equal "<3> expected but was\n<2>.", e.message
rescue Test::Unit::AssertionFailedError => e
assert_equal "<1 + 1> was the expression that failed.\n<3> expected but was\n<2>.", e.message
end
@@ -75,6 +77,8 @@ def test_array_of_expressions_identify_failure_when_message_provided
@object.increment
end
fail 'should not get to here'
+ rescue MiniTest::Assertion => e
+ assert_equal "something went wrong.\n<3> expected but was\n<2>.", e.message
rescue Test::Unit::AssertionFailedError => e
assert_equal "something went wrong.\n<1 + 1> was the expression that failed.\n<3> expected but was\n<2>.", e.message
end
@@ -84,8 +88,10 @@ def default_test; end
end
# These should always pass
-class NotTestingThingsTest < Test::Unit::TestCase
- include ActiveSupport::Testing::Default
+if defined? ActiveSupport::Testing::Default
+ class NotTestingThingsTest < Test::Unit::TestCase
+ include ActiveSupport::Testing::Default
+ end
end
class AlsoDoingNothingTest < ActiveSupport::TestCase
Please sign in to comment.
Something went wrong with that request. Please try again.