From 8f5c83bdeac2d345f3ec55ecb6460a76c4450a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20L=C3=BCtke?= Date: Tue, 1 May 2007 21:02:37 +0000 Subject: 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 --- activesupport/lib/active_support/core_ext/test.rb | 2 ++ .../lib/active_support/core_ext/test/difference.rb | 29 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/test.rb create mode 100644 activesupport/lib/active_support/core_ext/test/difference.rb (limited to 'activesupport/lib/active_support') 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 -- cgit v1.2.3