4 #Based on patch from Wilson Bilkovich
6 def initialize(receiver=nil, message=nil, &block)
12 def matches?(target, &block)
14 raise MatcherError.new(<<-EOF
15 block passed to should or should_not change must use {} instead of do/end
21 return false if @from && (@from != @before)
22 return false if @to && (@to != @after)
23 return (@before + @amount == @after) if @amount
24 return ((@after - @before) >= @minimum) if @minimum
25 return ((@after - @before) <= @maximum) if @maximum
26 return @before != @after
30 @before = @block.nil? ? @receiver.send(@message) : @block.call
32 @after = @block.nil? ? @receiver.send(@message) : @block.call
37 "#{result} should have been changed to #{@to.inspect}, but is now #{@after.inspect}"
39 "#{result} should have initially been #{@from.inspect}, but was #{@before.inspect}"
41 "#{result} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}"
43 "#{result} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}"
45 "#{result} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}"
47 "#{result} should have changed, but is still #{@before.inspect}"
59 def negative_failure_message
60 "#{result} should not have changed, but did change from #{@before.inspect} to #{@after.inspect}"
68 def by_at_least(minimum)
73 def by_at_most(maximum)
90 # should change(receiver, message, &block)
91 # should change(receiver, message, &block).by(value)
92 # should change(receiver, message, &block).from(old).to(new)
93 # should_not change(receiver, message, &block)
95 # Allows you to specify that a Proc will cause some value to change.
100 # team.add_player(player)
101 # }.should change(roster, :count)
104 # team.add_player(player)
105 # }.should change(roster, :count).by(1)
108 # team.add_player(player)
109 # }.should change(roster, :count).by_at_least(1)
112 # team.add_player(player)
113 # }.should change(roster, :count).by_at_most(1)
118 # }.should change { string }.from("string").to("gnirts")
121 # person.happy_birthday
122 # }.should change(person, :birthday).from(32).to(33)
125 # employee.develop_great_new_social_networking_app
126 # }.should change(employee, :title).from("Mail Clerk").to("CEO")
128 # Evaluates +receiver.message+ or +block+ before and
129 # after it evaluates the c object (generated by the lambdas in the examples above).
131 # Then compares the values before and after the +receiver.message+ and
132 # evaluates the difference compared to the expected difference.
135 # +should_not+ +change+ only supports the form with no subsequent calls to
136 # +by+, +by_at_least+, +by_at_most+, +to+ or +from+.
138 # blocks passed to +should+ +change+ and +should_not+ +change+
139 # must use the <tt>{}</tt> form (<tt>do/end</tt> is not supported)
140 def change(target=nil, message=nil, &block)
141 Matchers::Change.new(target, message, &block)