aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/deprecation.rb33
-rw-r--r--activesupport/lib/active_support/test_case.rb50
-rw-r--r--activesupport/lib/active_support/testing/assertions.rb61
-rw-r--r--activesupport/lib/active_support/testing/declarative.rb21
-rw-r--r--activesupport/lib/active_support/testing/setup_and_teardown.rb28
-rw-r--r--activesupport/test/abstract_unit.rb1
-rw-r--r--activesupport/test/core_ext/duration_test.rb2
-rw-r--r--activesupport/test/core_ext/module/model_naming_test.rb8
-rw-r--r--activesupport/test/deprecation_test.rb26
-rw-r--r--activesupport/test/test_test.rb19
10 files changed, 177 insertions, 72 deletions
diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb
index b4d8f61b8c..b3ad599371 100644
--- a/activesupport/lib/active_support/deprecation.rb
+++ b/activesupport/lib/active_support/deprecation.rb
@@ -221,23 +221,30 @@ class Module
include ActiveSupport::Deprecation::ClassMethods
end
-require 'test/unit/error'
-module Test
- module Unit
- class TestCase
- include ActiveSupport::Deprecation::Assertions
- end
+require 'active_support/test_case'
+
+class ActiveSupport::TestCase
+ include ActiveSupport::Deprecation::Assertions
+end
+
+begin
+ require 'test/unit/error'
- class Error # :nodoc:
- # Silence warnings when reporting test errors.
- def message_with_silenced_deprecation
- ActiveSupport::Deprecation.silence do
- message_without_silenced_deprecation
+ module Test
+ module Unit
+ class Error # :nodoc:
+ # Silence warnings when reporting test errors.
+ def message_with_silenced_deprecation
+ ActiveSupport::Deprecation.silence do
+ message_without_silenced_deprecation
+ end
end
- end
- alias_method_chain :message, :silenced_deprecation
+ alias_method_chain :message, :silenced_deprecation
+ end
end
end
+rescue LoadError
+ # Using miniunit, ignore.
end
diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb
index 197e73b3e8..f47329d026 100644
--- a/activesupport/lib/active_support/test_case.rb
+++ b/activesupport/lib/active_support/test_case.rb
@@ -1,24 +1,36 @@
-require 'test/unit/testcase'
-require 'active_support/testing/default'
-require 'active_support/testing/core_ext/test'
-
+require 'active_support/testing/setup_and_teardown'
+require 'active_support/testing/assertions'
+require 'active_support/testing/declarative'
module ActiveSupport
- class TestCase < Test::Unit::TestCase
- # 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
+ # Prefer MiniTest with Test::Unit compatibility.
+ begin
+ require 'minitest/unit'
+
+ # Hack around the test/unit autorun.
+ autorun_enabled = MiniTest::Unit.class_variable_get('@@installed_at_exit')
+ MiniTest::Unit.disable_autorun
+ require 'test/unit'
+ MiniTest::Unit.class_variable_set('@@installed_at_exit', autorun_enabled)
+
+ class TestCase < ::Test::Unit::TestCase
+ Assertion = MiniTest::Assertion
+ end
+
+ # Test::Unit compatibility.
+ rescue LoadError
+ require 'test/unit/testcase'
+ require 'active_support/testing/default'
+
+ class TestCase < ::Test::Unit::TestCase
+ Assertion = Test::Unit::AssertionFailedError
+ include ActiveSupport::Testing::Default
end
end
+
+ class TestCase
+ include ActiveSupport::Testing::SetupAndTeardown
+ include ActiveSupport::Testing::Assertions
+ extend ActiveSupport::Testing::Declarative
+ end
end
diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb
new file mode 100644
index 0000000000..ce2f44efd6
--- /dev/null
+++ b/activesupport/lib/active_support/testing/assertions.rb
@@ -0,0 +1,61 @@
+module ActiveSupport
+ module Testing
+ module Assertions
+ # Test numeric difference between the return value of an expression as a result of what is evaluated
+ # in the yielded block.
+ #
+ # assert_difference 'Article.count' do
+ # post :create, :article => {...}
+ # end
+ #
+ # An arbitrary expression is passed in and evaluated.
+ #
+ # assert_difference 'assigns(:article).comments(:reload).size' do
+ # post :create, :comment => {...}
+ # end
+ #
+ # An arbitrary positive or negative difference can be specified. The default is +1.
+ #
+ # assert_difference 'Article.count', -1 do
+ # post :delete, :id => ...
+ # end
+ #
+ # An array of expressions can also be passed in and evaluated.
+ #
+ # assert_difference [ 'Article.count', 'Post.count' ], +2 do
+ # post :create, :article => {...}
+ # end
+ #
+ # A error message can be specified.
+ #
+ # assert_difference 'Article.count', -1, "An Article should be destroyed" do
+ # 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)) } }
+
+ original_values = expression_evaluations.inject([]) { |memo, expression| memo << expression.call }
+ yield
+ expression_evaluations.each_with_index do |expression, i|
+ assert_equal original_values[i] + difference, expression.call, message
+ end
+ end
+
+ # Assertion that the numeric result of evaluating an expression is not changed before and after
+ # invoking the passed in block.
+ #
+ # assert_no_difference 'Article.count' do
+ # post :create, :article => invalid_attributes
+ # end
+ #
+ # A error message can be specified.
+ #
+ # assert_no_difference 'Article.count', "An Article should not be destroyed" do
+ # post :create, :article => invalid_attributes
+ # end
+ def assert_no_difference(expressions, message = nil, &block)
+ assert_difference expressions, 0, message, &block
+ end
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/testing/declarative.rb b/activesupport/lib/active_support/testing/declarative.rb
new file mode 100644
index 0000000000..cb6a5844eb
--- /dev/null
+++ b/activesupport/lib/active_support/testing/declarative.rb
@@ -0,0 +1,21 @@
+module ActiveSupport
+ module Testing
+ module Declarative
+ # test "verify something" do
+ # ...
+ # end
+ def 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
+ 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 a514b61fea..c70e149c16 100644
--- a/activesupport/lib/active_support/testing/setup_and_teardown.rb
+++ b/activesupport/lib/active_support/testing/setup_and_teardown.rb
@@ -14,9 +14,8 @@ module ActiveSupport
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 @@ module ActiveSupport
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.
diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb
index 9d8c252f86..a11ff25e41 100644
--- a/activesupport/test/abstract_unit.rb
+++ b/activesupport/test/abstract_unit.rb
@@ -5,6 +5,7 @@ require 'test/unit'
$:.unshift "#{File.dirname(__FILE__)}/../lib"
$:.unshift File.dirname(__FILE__)
require 'active_support'
+require 'active_support/test_case'
if RUBY_VERSION < '1.9'
$KCODE = 'UTF8'
diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb
index 5cd52d8d79..6fe0a98d39 100644
--- a/activesupport/test/core_ext/duration_test.rb
+++ b/activesupport/test/core_ext/duration_test.rb
@@ -1,6 +1,6 @@
require 'abstract_unit'
-class DurationTest < Test::Unit::TestCase
+class DurationTest < ActiveSupport::TestCase
def test_inspect
assert_equal '1 month', 1.month.inspect
assert_equal '1 month and 1 day', (1.month + 1.day).inspect
diff --git a/activesupport/test/core_ext/module/model_naming_test.rb b/activesupport/test/core_ext/module/model_naming_test.rb
index fc73fa5c36..d08349dd97 100644
--- a/activesupport/test/core_ext/module/model_naming_test.rb
+++ b/activesupport/test/core_ext/module/model_naming_test.rb
@@ -2,18 +2,18 @@ require 'abstract_unit'
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
diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb
index 27e9573ce2..73a1f9959c 100644
--- a/activesupport/test/deprecation_test.rb
+++ b/activesupport/test/deprecation_test.rb
@@ -32,7 +32,7 @@ class Deprecatee
end
-class DeprecationTest < Test::Unit::TestCase
+class DeprecationTest < ActiveSupport::TestCase
def setup
# Track the last warning.
@old_behavior = ActiveSupport::Deprecation.behavior
@@ -143,19 +143,21 @@ class DeprecationTest < Test::Unit::TestCase
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
diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb
index 4e253848f6..89fae1a9e4 100644
--- a/activesupport/test/test_test.rb
+++ b/activesupport/test/test_test.rb
@@ -1,7 +1,6 @@
require 'abstract_unit'
-require 'active_support/test_case'
-class AssertDifferenceTest < Test::Unit::TestCase
+class AssertDifferenceTest < ActiveSupport::TestCase
def setup
@object = Class.new do
attr_accessor :num
@@ -66,8 +65,8 @@ class AssertDifferenceTest < Test::Unit::TestCase
@object.increment
end
fail 'should not get to here'
- rescue Test::Unit::AssertionFailedError => e
- assert_equal "<1 + 1> was the expression that failed.\n<3> expected but was\n<2>.", e.message
+ rescue Exception => e
+ assert_equal "<3> expected but was\n<2>.", e.message
end
def test_array_of_expressions_identify_failure_when_message_provided
@@ -75,8 +74,8 @@ class AssertDifferenceTest < Test::Unit::TestCase
@object.increment
end
fail 'should not get to here'
- 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
+ rescue Exception => e
+ assert_equal "something went wrong.\n<3> expected but was\n<2>.", e.message
end
else
def default_test; end
@@ -84,15 +83,17 @@ class AssertDifferenceTest < Test::Unit::TestCase
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
end
# Setup and teardown callbacks.
-class SetupAndTeardownTest < Test::Unit::TestCase
+class SetupAndTeardownTest < ActiveSupport::TestCase
setup :reset_callback_record, :foo
teardown :foo, :sentinel, :foo