diff --git a/lib/shoulda/macros.rb b/lib/shoulda/macros.rb index c3630431..0794d664 100644 --- a/lib/shoulda/macros.rb +++ b/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 diff --git a/test/other/helpers_test.rb b/test/other/helpers_test.rb index 4454c49f..2fe5427e 100644 --- a/test/other/helpers_test.rb +++ b/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