vendor/plugins/rspec/lib/spec/matchers/change.rb in typo-5.1.3 vs vendor/plugins/rspec/lib/spec/matchers/change.rb in typo-5.1.98

- old
+ new

@@ -2,64 +2,64 @@ module Matchers #Based on patch from Wilson Bilkovich class Change #:nodoc: def initialize(receiver=nil, message=nil, &block) - @receiver = receiver - @message = message - @block = block + @message = message || "result" + @value_proc = block || lambda { + receiver.__send__(message) + } end - def matches?(target, &block) - if block - raise MatcherError.new(<<-EOF -block passed to should or should_not change must use {} instead of do/end -EOF -) - end - @target = target - execute_change - return false if @from && (@from != @before) - return false if @to && (@to != @after) + def matches?(event_proc) + raise_block_syntax_error if block_given? + + @before = evaluate_value_proc + event_proc.call + @after = evaluate_value_proc + + return false if @from unless @from == @before + return false if @to unless @to == @after return (@before + @amount == @after) if @amount return ((@after - @before) >= @minimum) if @minimum return ((@after - @before) <= @maximum) if @maximum return @before != @after end - def execute_change - @before = @block.nil? ? @receiver.send(@message) : @block.call - @target.call - @after = @block.nil? ? @receiver.send(@message) : @block.call + def raise_block_syntax_error + raise MatcherError.new(<<-MESSAGE +block passed to should or should_not change must use {} instead of do/end +MESSAGE + ) end + def evaluate_value_proc + @value_proc.call + end + def failure_message if @to - "#{result} should have been changed to #{@to.inspect}, but is now #{@after.inspect}" + "#{@message} should have been changed to #{@to.inspect}, but is now #{@after.inspect}" elsif @from - "#{result} should have initially been #{@from.inspect}, but was #{@before.inspect}" + "#{@message} should have initially been #{@from.inspect}, but was #{@before.inspect}" elsif @amount - "#{result} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}" + "#{@message} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}" elsif @minimum - "#{result} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}" + "#{@message} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}" elsif @maximum - "#{result} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}" + "#{@message} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}" else - "#{result} should have changed, but is still #{@before.inspect}" + "#{@message} should have changed, but is still #{@before.inspect}" end end - def result - @message || "result" - end - def actual_delta @after - @before end def negative_failure_message - "#{result} should not have changed, but did change from #{@before.inspect} to #{@after.inspect}" + "#{@message} should not have changed, but did change from #{@before.inspect} to #{@after.inspect}" end def by(amount) @amount = amount self @@ -112,33 +112,37 @@ # team.add_player(player) # }.should change(roster, :count).by_at_most(1) # # string = "string" # lambda { - # string.reverse + # string.reverse! # }.should change { string }.from("string").to("gnirts") # # lambda { # person.happy_birthday # }.should change(person, :birthday).from(32).to(33) # # lambda { # employee.develop_great_new_social_networking_app # }.should change(employee, :title).from("Mail Clerk").to("CEO") # - # Evaluates +receiver.message+ or +block+ before and - # after it evaluates the c object (generated by the lambdas in the examples above). + # Evaluates <tt>receiver.message</tt> or <tt>block</tt> before and after + # it evaluates the c object (generated by the lambdas in the examples + # above). # - # Then compares the values before and after the +receiver.message+ and - # evaluates the difference compared to the expected difference. + # Then compares the values before and after the <tt>receiver.message</tt> + # and evaluates the difference compared to the expected difference. # - # == Warning - # +should_not+ +change+ only supports the form with no subsequent calls to - # +by+, +by_at_least+, +by_at_most+, +to+ or +from+. + # == WARNING + # <tt>should_not change</tt> only supports the form with no + # subsequent calls to <tt>by</tt>, <tt>by_at_least</tt>, + # <tt>by_at_most</tt>, <tt>to</tt> or <tt>from</tt>. # - # blocks passed to +should+ +change+ and +should_not+ +change+ - # must use the <tt>{}</tt> form (<tt>do/end</tt> is not supported) - def change(target=nil, message=nil, &block) - Matchers::Change.new(target, message, &block) + # blocks passed to <tt>should</tt> <tt>change</tt> and <tt>should_not</tt> + # <tt>change</tt> must use the <tt>{}</tt> form (<tt>do/end</tt> is not + # supported). + # + def change(receiver=nil, message=nil, &block) + Matchers::Change.new(receiver, message, &block) end end end