diff options
| -rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
| -rw-r--r-- | activesupport/lib/active_support/core_ext/test/difference.rb | 51 | ||||
| -rw-r--r-- | activesupport/test/test_test.rb | 47 | 
3 files changed, 64 insertions, 36 deletions
| diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 28ff1136e2..1a1cb9c275 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@  *SVN* +* Simplify API of assert_difference by passing in an expression that is evaluated before and after the passed in block. See documenation for examples of new API. [Marcel Molina Jr.] +  * 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] diff --git a/activesupport/lib/active_support/core_ext/test/difference.rb b/activesupport/lib/active_support/core_ext/test/difference.rb index 1f85f0f4b7..65dae3469b 100644 --- a/activesupport/lib/active_support/core_ext/test/difference.rb +++ b/activesupport/lib/active_support/core_ext/test/difference.rb @@ -1,29 +1,40 @@ -  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) } +    class TestCase #:nodoc:       +      # Test numeric difference between the return value of an expression as a result of what is evaluated +      # in the yielded block. +      # +      #   assert_difference 'Post.count' do +      #     post :create, :post => {...} +      #   end +      # +      # An arbitrary expression is passed in an evaluated. +      # +      #   assert_difference 'assigns(:post).comments(:reload).size' do +      #     post :create, :comment => {...} +      #   end +      # +      # An arbitrary positive or negative difference can be specified. The default is 1. +      # +      #   assert_difference 'Post.count', -1 do +      #     post :delete, :id => ... +      #   end +      def assert_difference(expression, difference = 1, &block) +        expression_evaluation = lambda { eval(expression) } +        original_value        = expression_evaluation.call          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 +        assert_equal original_value + difference, expression_evaluation.call        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 +      # Assertion that the numeric result of evaluating an expression is not changed before and after +      # invoking the passed in block. +      # +      #   assert_no_difference 'Post.count' do +      #     post :create, :post => invalid_attributes +      #   end +      def assert_no_difference(expression, &block) +        assert_difference expression, 0, &block        end -      end    end  end diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 63a44e9379..d8a02b5497 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -3,32 +3,47 @@ require File.dirname(__FILE__) + '/abstract_unit'  class AssertDifferenceTest < Test::Unit::TestCase    def setup -    @object = Class.new { attr_accessor :num }.new     +    @object = Class.new do +      attr_accessor :num  +      def increment +        self.num += 1 +      end + +      def decrement +        self.num -= 1 +      end +    end.new     +    @object.num = 0    end    def test_assert_no_difference -    @object.num = 0 -     -    assert_no_difference @object, :num do +    assert_no_difference '@object.num' do        # ...      end -          end +    def test_assert_difference -    @object.num = 0 -     -     -    assert_difference @object, :num, +1 do -      @object.num = 1 +    assert_difference '@object.num', +1 do +      @object.increment      end -          end -  def test_methods_available -     -    assert self.respond_to?(:assert_difference) -    assert self.respond_to?(:assert_no_difference) -     +  def test_assert_difference_with_implicit_difference +    assert_difference '@object.num' do +      @object.increment +    end    end +  def test_arbitrary_expression +    assert_difference '@object.num + 1', +2 do +      @object.increment +      @object.increment +    end +  end + +  def test_negative_differences +    assert_difference '@object.num', -1 do +      @object.decrement +    end +  end  end | 
