Created
September 26, 2009 23:50
-
-
Save JackDanger/194512 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Shoulda | |
module Macros | |
def should_change(expression, options = {}) | |
by, from, to = get_options!([options], :by, :from, :to) | |
stmt = "change #{expression.inspect}" | |
stmt << " from #{from.inspect}" if from | |
stmt << " to #{to.inspect}" if to | |
stmt << " by #{by.inspect}" if by | |
expression_eval = lambda { eval(expression) } | |
before_var_name = '@_before_should_change_' + expression.downcase.gsub(/[^\w]/, '_') | |
before = lambda { self.instance_variable_set( before_var_name, expression_eval.bind(self).call ) } | |
should stmt, :before => before do | |
old_value = self.instance_variable_get( before_var_name ) | |
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 | |
assert_equal old_value + by, new_value if by | |
end | |
end | |
def should_not_change(expression) | |
expression_eval = lambda { eval(expression) } | |
before_var_name = '@_before_should_change_' + expression.downcase.gsub(/[^\w]/, '_') | |
before = lambda { self.instance_variable_set( before_var_name, expression_eval.bind(self).call ) } | |
should "not change #{expression.inspect}", :before => before do | |
old_value = self.instance_variable_get( before_var_name ) | |
new_value = expression_eval.bind(self).call | |
assert_equal old_value, new_value, "#{expression.inspect} changed" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment