C0 code coverage information
Generated on Mon Aug 13 01:18:54 -0400 2007 with rcov 0.8.0
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 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 @before != @after
25 end
26
27 def execute_change
28 @before = @block.nil? ? @receiver.send(@message) : @block.call
29 @target.call
30 @after = @block.nil? ? @receiver.send(@message) : @block.call
31 end
32
33 def failure_message
34 if @to
35 "#{result} should have been changed to #{@to.inspect}, but is now #{@after.inspect}"
36 elsif @from
37 "#{result} should have initially been #{@from.inspect}, but was #{@before.inspect}"
38 elsif @amount
39 "#{result} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}"
40 else
41 "#{result} should have changed, but is still #{@before.inspect}"
42 end
43 end
44
45 def result
46 @message || "result"
47 end
48
49 def actual_delta
50 @after - @before
51 end
52
53 def negative_failure_message
54 "#{result} should not have changed, but did change from #{@before.inspect} to #{@after.inspect}"
55 end
56
57 def by(amount)
58 @amount = amount
59 self
60 end
61
62 def to(to)
63 @to = to
64 self
65 end
66
67 def from (from)
68 @from = from
69 self
70 end
71 end
72
73 # :call-seq:
74 # should change(receiver, message, &block)
75 # should change(receiver, message, &block).by(value)
76 # should change(receiver, message, &block).from(old).to(new)
77 # should_not change(receiver, message, &block)
78 #
79 # Allows you to specify that a Proc will cause some value to change.
80 #
81 # == Examples
82 #
83 # lambda {
84 # team.add_player(player)
85 # }.should change(roster, :count)
86 #
87 # lambda {
88 # team.add_player(player)
89 # }.should change(roster, :count).by(1)
90 #
91 # string = "string"
92 # lambda {
93 # string.reverse
94 # }.should change { string }.from("string").to("gnirts")
95 #
96 # lambda {
97 # person.happy_birthday
98 # }.should change(person, :birthday).from(32).to(33)
99 #
100 # lambda {
101 # employee.develop_great_new_social_networking_app
102 # }.should change(employee, :title).from("Mail Clerk").to("CEO")
103 #
104 # Evaluates +receiver.message+ or +block+ before and
105 # after it evaluates the c object (generated by the lambdas in the examples above).
106 #
107 # Then compares the values before and after the +receiver.message+ and
108 # evaluates the difference compared to the expected difference.
109 #
110 # == Warning
111 # +should_not+ +change+ only supports the form with no subsequent calls to
112 # +be+, +to+ or +from+.
113 #
114 # blocks passed to +should+ +change+ and +should_not+ +change+
115 # must use the <tt>{}</tt> form (<tt>do/end</tt> is not supported)
116 def change(target=nil, message=nil, &block)
117 Matchers::Change.new(target, message, &block)
118 end
119 end
120 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.0.