Skip to content
Browse files

Re-enabled the old behavior for should_change and should_not_change m…

…acros, but displaying a deprecation warning when they are used without a block
  • Loading branch information...
1 parent 4e4317f commit d13a46065fb325a8fae1d3537e47365843ec9d41 @matflores matflores committed with jferris May 23, 2009
Showing with 33 additions and 4 deletions.
  1. +18 −4 lib/shoulda/macros.rb
  2. +15 −0 test/other/helpers_test.rb
View
22 lib/shoulda/macros.rb
@@ -36,10 +36,17 @@ def should_change(description, options = {}, &block)
stmt << " to #{to.inspect}" if to
stmt << " by #{by.inspect}" if by
- before = lambda { @_before_should_change = block.bind(self).call }
+ if block_given?
+ code = block
+ else
+ warn "[DEPRECATION] should_change(expression, options) is deprecated. " <<
+ "Use should_change(description, options) { code } instead."
+ code = lambda { eval(description) }
+ end
+ before = lambda { @_before_should_change = code.bind(self).call }
should stmt, :before => before do
old_value = @_before_should_change
- new_value = block.bind(self).call
+ new_value = code.bind(self).call
assert_operator from, :===, old_value, "#{description} did not originally match #{from.inspect}" if from
assert_not_equal old_value, new_value, "#{description} did not change" unless by == 0
assert_operator to, :===, new_value, "#{description} was not changed to match #{to.inspect}" if to
@@ -60,9 +67,16 @@ def should_change(description, options = {}, &block)
# should_not_change("the number of posts") { Post.count }
# end
def should_not_change(description, &block)
- before = lambda { @_before_should_not_change = block.bind(self).call }
+ if block_given?
+ code = block
+ else
+ warn "[DEPRECATION] should_not_change(expression) is deprecated. " <<
+ "Use should_not_change(description) { code } instead."
+ code = lambda { eval(description) }
+ end
+ before = lambda { @_before_should_not_change = code.bind(self).call }
should "not change #{description}", :before => before do
- new_value = block.bind(self).call
+ new_value = code.bind(self).call
assert_equal @_before_should_not_change, new_value, "#{description} changed"
end
end
View
15 test/other/helpers_test.rb
@@ -93,6 +93,13 @@ class HelpersTest < ActiveSupport::TestCase # :nodoc:
should_change("the number of elements", :to => 4) { @a.length }
should_change("the first element", :by => 0) { @a[0] }
should_not_change("the first element") { @a[0] }
+
+ # tests for deprecated behavior
+ should_change "@a.length", :by => 1
+ should_change "@a.length", :from => 3
+ should_change "@a.length", :to => 4
+ should_change "@a[0]", :by => 0
+ should_not_change "@a[0]"
end
context "after replacing it with an array of strings" do
@@ -106,6 +113,14 @@ class HelpersTest < ActiveSupport::TestCase # :nodoc:
should_change("the second element", :from => 2, :to => "b") { @a[1] }
should_change("the third element", :from => /\d/, :to => /\w/) { @a[2] }
should_change("the last element", :to => String) { @a[3] }
+
+ # tests for deprecated behavior
+ should_change "@a.length", :by => 3
+ should_change "@a.length", :from => 3, :to => 6, :by => 3
+ should_change "@a[0]"
+ should_change "@a[1]", :from => 2, :to => "b"
+ should_change "@a[2]", :from => /\d/, :to => /\w/
+ should_change "@a[3]", :to => String
end
end

0 comments on commit d13a460

Please sign in to comment.
Something went wrong with that request. Please try again.