Permalink
Browse files

Updated should_change and should_not_change to accept a string descri…

…ption and a block [#154]
  • Loading branch information...
1 parent 40568e7 commit 4e4317fcfbaee4af32cbcf97be4d7fef48903789 @matflores matflores committed with jferris May 13, 2009
Showing with 36 additions and 34 deletions.
  1. +25 −23 lib/shoulda/macros.rb
  2. +11 −11 test/other/helpers_test.rb
View
@@ -3,65 +3,67 @@
module Shoulda # :nodoc:
module Macros
# Macro that creates a test asserting a change between the return value
- # of an expression that is run before and after the current setup block
+ # of a block that is run before and after the current setup block
# is run. This is similar to Active Support's <tt>assert_difference</tt>
# assertion, but supports more than just numeric values. See also
# should_not_change.
#
+ # The passed description will be used when generating the test name and failure messages.
+ #
# Example:
#
# context "Creating a post" do
# setup { Post.create }
- # should_change "Post.count", :by => 1
+ # should_change("the number of posts", :by => 1) { Post.count }
# end
#
# As shown in this example, the <tt>:by</tt> option expects a numeric
# difference between the before and after values of the expression. You
# may also specify <tt>:from</tt> and <tt>:to</tt> options:
#
- # should_change "Post.count", :from => 0, :to => 1
- # should_change "@post.title", :from => "old", :to => "new"
+ # should_change("the number of posts", :from => 0, :to => 1) { Post.count }
+ # should_change("the post title", :from => "old", :to => "new") { @post.title }
#
# Combinations of <tt>:by</tt>, <tt>:from</tt>, and <tt>:to</tt> are allowed:
#
- # should_change "@post.title" # => assert the value changed in some way
- # should_change "@post.title", :from => "old" # => assert the value changed to anything other than "old"
- # should_change "@post.title", :to => "new" # => assert the value changed from anything other than "new"
- def should_change(expression, options = {})
+ # should_change("the post title") { @post.title } # => assert the value changed in some way
+ # should_change("the post title", :from => "old") { @post.title } # => assert the value changed to anything other than "old"
+ # should_change("the post title", :to => "new") { @post.title } # => assert the value changed from anything other than "new"
+ def should_change(description, options = {}, &block)
by, from, to = get_options!([options], :by, :from, :to)
- stmt = "change #{expression.inspect}"
+ stmt = "change #{description}"
stmt << " from #{from.inspect}" if from
stmt << " to #{to.inspect}" if to
stmt << " by #{by.inspect}" if by
- expression_eval = lambda { eval(expression) }
- before = lambda { @_before_should_change = expression_eval.bind(self).call }
+ before = lambda { @_before_should_change = block.bind(self).call }
should stmt, :before => before do
old_value = @_before_should_change
- new_value = expression_eval.bind(self).call
- assert_operator from, :===, old_value, "#{expression.inspect} did not originally match #{from.inspect}" if from
- assert_not_equal old_value, new_value, "#{expression.inspect} did not change" unless by == 0
- assert_operator to, :===, new_value, "#{expression.inspect} was not changed to match #{to.inspect}" if to
+ new_value = block.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
assert_equal old_value + by, new_value if by
end
end
# Macro that creates a test asserting no change between the return value
- # of an expression that is run before and after the current setup block
+ # of a block that is run before and after the current setup block
# is run. This is the logical opposite of should_change.
#
+ # The passed description will be used when generating the test name and failure message.
+ #
# Example:
#
# context "Updating a post" do
# setup { @post.update_attributes(:title => "new") }
- # should_not_change "Post.count"
+ # should_not_change("the number of posts") { Post.count }
# end
- def should_not_change(expression)
- expression_eval = lambda { eval(expression) }
- before = lambda { @_before_should_not_change = expression_eval.bind(self).call }
- should "not change #{expression.inspect}", :before => before do
- new_value = expression_eval.bind(self).call
- assert_equal @_before_should_not_change, new_value, "#{expression.inspect} changed"
+ def should_not_change(description, &block)
+ before = lambda { @_before_should_not_change = block.bind(self).call }
+ should "not change #{description}", :before => before do
+ new_value = block.bind(self).call
+ assert_equal @_before_should_not_change, new_value, "#{description} changed"
end
end
View
@@ -88,24 +88,24 @@ class HelpersTest < ActiveSupport::TestCase # :nodoc:
@a.push(4)
end
- 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]"
+ should_change("the number of elements", :by => 1) { @a.length }
+ should_change("the number of elements", :from => 3) { @a.length }
+ 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] }
end
context "after replacing it with an array of strings" do
setup do
@a = %w(a b c d e f)
end
- 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
+ should_change("the number of elements", :by => 3) { @a.length }
+ should_change("the number of elements", :from => 3, :to => 6, :by => 3) { @a.length }
+ should_change("the first element") { @a[0] }
+ 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] }
end
end

0 comments on commit 4e4317f

Please sign in to comment.