]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/matchers/change.rb
added RSpec and RSpec on Rails
[rails.git] / vendor / gems / rspec-1.1.2 / lib / spec / matchers / change.rb
1 module Spec
2   module Matchers
3     
4     #Based on patch from Wilson Bilkovich
5     class Change #:nodoc:
6       def initialize(receiver=nil, message=nil, &block)
7         @receiver = receiver
8         @message = message
9         @block = block
10       end
11       
12       def matches?(target, &block)
13         if block
14           raise MatcherError.new(<<-EOF
15 block passed to should or should_not change must use {} instead of do/end
16 EOF
17 )
18         end
19         @target = target
20         execute_change
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
27       end
28       
29       def execute_change
30         @before = @block.nil? ? @receiver.send(@message) : @block.call
31         @target.call
32         @after = @block.nil? ? @receiver.send(@message) : @block.call
33       end
34       
35       def failure_message
36         if @to
37           "#{result} should have been changed to #{@to.inspect}, but is now #{@after.inspect}"
38         elsif @from
39           "#{result} should have initially been #{@from.inspect}, but was #{@before.inspect}"
40         elsif @amount
41           "#{result} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}"
42         elsif @minimum
43           "#{result} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}"
44         elsif @maximum
45           "#{result} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}"
46         else
47           "#{result} should have changed, but is still #{@before.inspect}"
48         end
49       end
50       
51       def result
52         @message || "result"
53       end
54       
55       def actual_delta
56         @after - @before
57       end
58       
59       def negative_failure_message
60         "#{result} should not have changed, but did change from #{@before.inspect} to #{@after.inspect}"
61       end
62       
63       def by(amount)
64         @amount = amount
65         self
66       end
67       
68       def by_at_least(minimum)
69         @minimum = minimum
70         self
71       end
72       
73       def by_at_most(maximum)
74         @maximum = maximum
75         self
76       end      
77       
78       def to(to)
79         @to = to
80         self
81       end
82       
83       def from (from)
84         @from = from
85         self
86       end
87     end
88     
89     # :call-seq:
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)
94     #
95     # Allows you to specify that a Proc will cause some value to change.
96     #
97     # == Examples
98     #
99     #   lambda {
100     #     team.add_player(player) 
101     #   }.should change(roster, :count)
102     #
103     #   lambda {
104     #     team.add_player(player) 
105     #   }.should change(roster, :count).by(1)
106     #
107     #   lambda {
108     #     team.add_player(player) 
109     #   }.should change(roster, :count).by_at_least(1)
110     #
111     #   lambda {
112     #     team.add_player(player)
113     #   }.should change(roster, :count).by_at_most(1)    
114     #
115     #   string = "string"
116     #   lambda {
117     #     string.reverse
118     #   }.should change { string }.from("string").to("gnirts")
119     #
120     #   lambda {
121     #     person.happy_birthday
122     #   }.should change(person, :birthday).from(32).to(33)
123     #       
124     #   lambda {
125     #     employee.develop_great_new_social_networking_app
126     #   }.should change(employee, :title).from("Mail Clerk").to("CEO")
127     #
128     # Evaluates +receiver.message+ or +block+ before and
129     # after it evaluates the c object (generated by the lambdas in the examples above).
130     #
131     # Then compares the values before and after the +receiver.message+ and
132     # evaluates the difference compared to the expected difference.
133     #
134     # == Warning
135     # +should_not+ +change+ only supports the form with no subsequent calls to
136     # +by+, +by_at_least+, +by_at_most+, +to+ or +from+.
137     #
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)
142     end
143   end
144 end