diff options
author | Tobias Lütke <tobias.luetke@gmail.com> | 2007-05-01 21:02:37 +0000 |
---|---|---|
committer | Tobias Lütke <tobias.luetke@gmail.com> | 2007-05-01 21:02:37 +0000 |
commit | 8f5c83bdeac2d345f3ec55ecb6460a76c4450a48 (patch) | |
tree | 7f9544ba4bd4ebd393598eb0963dc48bbf50471e /activesupport | |
parent | 04fd94d8f8e0718687c8f4edcc9470178b5cb6ee (diff) | |
download | rails-8f5c83bdeac2d345f3ec55ecb6460a76c4450a48.tar.gz rails-8f5c83bdeac2d345f3ec55ecb6460a76c4450a48.tar.bz2 rails-8f5c83bdeac2d345f3ec55ecb6460a76c4450a48.zip |
Added assert_difference and assert_no_difference to test/unit assertions
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6647 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/test.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/test/difference.rb | 29 | ||||
-rw-r--r-- | activesupport/test/test_test.rb | 34 |
5 files changed, 68 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index f8ac504127..28ff1136e2 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added assert_difference and assert_no_difference to test/unit assertions [Tobias Luetke] + * Removed breakpointer and Binding.of_caller in favor of relying on ruby-debug by Kent Sibilev since the breakpointer has been broken since Ruby 1.8.4 and will not be coming back [DHH] * Added parsing of file type in Hash.xml_in so you can easily do file uploads with base64 from an API [DHH] diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index e094cbe0fe..668bb6e228 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -43,3 +43,4 @@ require 'active_support/duration' require 'active_support/json' require 'active_support/multibyte' + diff --git a/activesupport/lib/active_support/core_ext/test.rb b/activesupport/lib/active_support/core_ext/test.rb new file mode 100644 index 0000000000..193599b8dc --- /dev/null +++ b/activesupport/lib/active_support/core_ext/test.rb @@ -0,0 +1,2 @@ +require File.dirname(__FILE__) + '/test/difference' + diff --git a/activesupport/lib/active_support/core_ext/test/difference.rb b/activesupport/lib/active_support/core_ext/test/difference.rb new file mode 100644 index 0000000000..1f85f0f4b7 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/test/difference.rb @@ -0,0 +1,29 @@ + +module Test #:nodoc: + module Unit #:nodoc: + class TestCase #:nodoc: + + # Test difference between the return value of method on object for duration of the block + def assert_difference(objects, method = nil, difference = 1) + objects = [objects].flatten + initial_values = objects.inject([]) { |sum,obj| sum << obj.send(method) } + yield + if difference.nil? + objects.each_with_index { |obj,i| + assert_not_equal initial_values[i], obj.send(method), "#{obj}##{method}" + } + else + objects.each_with_index { |obj,i| + assert_equal initial_values[i] + difference, obj.send(method), "#{obj}##{method}" + } + end + end + + # Test absence of difference between the return value of method on object for duration of the block + def assert_no_difference(objects, method = nil, &block) + assert_difference objects, method, 0, &block + end + + end + end +end diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb new file mode 100644 index 0000000000..63a44e9379 --- /dev/null +++ b/activesupport/test/test_test.rb @@ -0,0 +1,34 @@ +require File.dirname(__FILE__) + '/abstract_unit' + +class AssertDifferenceTest < Test::Unit::TestCase + + def setup + @object = Class.new { attr_accessor :num }.new + end + + def test_assert_no_difference + @object.num = 0 + + assert_no_difference @object, :num do + # ... + end + + end + def test_assert_difference + @object.num = 0 + + + assert_difference @object, :num, +1 do + @object.num = 1 + end + + end + + def test_methods_available + + assert self.respond_to?(:assert_difference) + assert self.respond_to?(:assert_no_difference) + + end + +end |