Skip to content

Commit

Permalink
Re-enabled the old behavior for should_change and should_not_change m…
Browse files Browse the repository at this point in the history
…acros, but displaying a deprecation warning when they are used without a block
  • Loading branch information
matflores authored and jferris committed Jun 9, 2009
1 parent 4e4317f commit d13a460
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/shoulda/macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test/other/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit d13a460

Please sign in to comment.