Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
rcov/ruby/1.8/gems/rspec-expectations-2.5.0/lib/rspec/matchers/change.rb | 190 | 102 | 61.05%
|
30.39%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 module RSpec |
2 module Matchers |
3 |
4 #Based on patch from Wilson Bilkovich |
5 class Change #:nodoc: |
6 def initialize(receiver=nil, message=nil, &block) |
7 @message = message |
8 @value_proc = block || lambda {receiver.__send__(message)} |
9 @to = @from = @minimum = @maximum = @amount = nil |
10 @given_from = @given_to = false |
11 end |
12 |
13 def matches?(event_proc) |
14 raise_block_syntax_error if block_given? |
15 |
16 @before = evaluate_value_proc |
17 event_proc.call |
18 @after = evaluate_value_proc |
19 |
20 (!change_expected? || changed?) && matches_before? && matches_after? && matches_amount? && matches_min? && matches_max? |
21 end |
22 |
23 def raise_block_syntax_error |
24 raise MatcherError.new(<<-MESSAGE |
25 block passed to should or should_not change must use {} instead of do/end |
26 MESSAGE |
27 ) |
28 end |
29 |
30 def evaluate_value_proc |
31 @value_proc.call |
32 end |
33 |
34 def failure_message_for_should |
35 if @given_from && @before != @from |
36 "#{message} should have initially been #{@from.inspect}, but was #{@before.inspect}" |
37 elsif @given_to && @to != @after |
38 "#{message} should have been changed to #{@to.inspect}, but is now #{@after.inspect}" |
39 elsif @amount |
40 "#{message} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}" |
41 elsif @minimum |
42 "#{message} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}" |
43 elsif @maximum |
44 "#{message} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}" |
45 else |
46 "#{message} should have changed, but is still #{@before.inspect}" |
47 end |
48 end |
49 |
50 def actual_delta |
51 @after - @before |
52 end |
53 |
54 def failure_message_for_should_not |
55 "#{message} should not have changed, but did change from #{@before.inspect} to #{@after.inspect}" |
56 end |
57 |
58 def by(amount) |
59 @amount = amount |
60 self |
61 end |
62 |
63 def by_at_least(minimum) |
64 @minimum = minimum |
65 self |
66 end |
67 |
68 def by_at_most(maximum) |
69 @maximum = maximum |
70 self |
71 end |
72 |
73 def to(to) |
74 @given_to = true |
75 @to = to |
76 self |
77 end |
78 |
79 def from (from) |
80 @given_from = true |
81 @from = from |
82 self |
83 end |
84 |
85 def description |
86 "change ##{message}" |
87 end |
88 |
89 private |
90 |
91 def message |
92 @message || "result" |
93 end |
94 |
95 def change_expected? |
96 @amount != 0 |
97 end |
98 |
99 def changed? |
100 @before != @after |
101 end |
102 |
103 def matches_before? |
104 @given_from ? @from == @before : true |
105 end |
106 |
107 def matches_after? |
108 @given_to ? @to == @after : true |
109 end |
110 |
111 def matches_amount? |
112 @amount ? (@before + @amount == @after) : true |
113 end |
114 |
115 def matches_min? |
116 @minimum ? (@after - @before >= @minimum) : true |
117 end |
118 |
119 def matches_max? |
120 @maximum ? (@after - @before <= @maximum) : true |
121 end |
122 |
123 end |
124 |
125 # :call-seq: |
126 # should change(receiver, message) |
127 # should change(receiver, message).by(value) |
128 # should change(receiver, message).from(old).to(new) |
129 # should_not change(receiver, message) |
130 # |
131 # should change {...} |
132 # should change {...}.by(value) |
133 # should change {...}.from(old).to(new) |
134 # should_not change {...} |
135 # |
136 # Applied to a proc, specifies that its execution will cause some value to |
137 # change. |
138 # |
139 # You can either pass <tt>receiver</tt> and <tt>message</tt>, or a block, |
140 # but not both. |
141 # |
142 # When passing a block, it must use the <tt>{ ... }</tt> format, not |
143 # do/end, as <tt>{ ... }</tt> binds to the +change+ method, whereas do/end |
144 # would errantly bind to the +should+ or +should_not+ method. |
145 # |
146 # == Examples |
147 # |
148 # lambda { |
149 # team.add_player(player) |
150 # }.should change(roster, :count) |
151 # |
152 # lambda { |
153 # team.add_player(player) |
154 # }.should change(roster, :count).by(1) |
155 # |
156 # lambda { |
157 # team.add_player(player) |
158 # }.should change(roster, :count).by_at_least(1) |
159 # |
160 # lambda { |
161 # team.add_player(player) |
162 # }.should change(roster, :count).by_at_most(1) |
163 # |
164 # string = "string" |
165 # lambda { |
166 # string.reverse! |
167 # }.should change { string }.from("string").to("gnirts") |
168 # |
169 # lambda { |
170 # person.happy_birthday |
171 # }.should change(person, :birthday).from(32).to(33) |
172 # |
173 # lambda { |
174 # employee.develop_great_new_social_networking_app |
175 # }.should change(employee, :title).from("Mail Clerk").to("CEO") |
176 # |
177 # == Notes |
178 # |
179 # Evaluates <tt>receiver.message</tt> or <tt>block</tt> before and after it |
180 # evaluates the proc object (generated by the lambdas in the examples |
181 # above). |
182 # |
183 # <tt>should_not change</tt> only supports the form with no subsequent |
184 # calls to <tt>by</tt>, <tt>by_at_least</tt>, <tt>by_at_most</tt>, |
185 # <tt>to</tt> or <tt>from</tt>. |
186 def change(receiver=nil, message=nil, &block) |
187 Matchers::Change.new(receiver, message, &block) |
188 end |
189 end |
190 end |
Generated on Fri Apr 22 17:22:41 -0700 2011 with rcov 0.9.8