Geminstaller C0 Coverage Information - RCov

/Users/woolley/.rvm/gems/ruby-1.8.7-p174@geminstaller/gems/rspec-1.3.0/lib/spec/mocks/argument_matchers.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
/Users/woolley/.rvm/gems/ruby-1.8.7-p174@geminstaller/gems/rspec-1.3.0/lib/spec/mocks/argument_matchers.rb 237 149
65.40%
44.97%

Key

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.

Coverage Details

1 module Spec
2   module Mocks
3 
4     # ArgumentMatchers are messages that you can include in message
5     # expectations to match arguments against a broader check than simple
6     # equality.
7     #
8     # With the exception of any_args() and no_args(), the matchers
9     # are all positional - they match against the arg in the given position.
10     module ArgumentMatchers
11 
12       class AnyArgsMatcher
13         def description
14           "any args"
15         end
16       end
17 
18       class NoArgsMatcher
19         def description
20           "no args"
21         end
22       end
23 
24       class AnyArgMatcher
25         def initialize(ignore)
26         end
27 
28         def ==(other)
29           true
30         end
31         
32         def description
33           "anything"
34         end
35       end
36 
37       class RegexpMatcher
38         def initialize(regexp)
39           @regexp = regexp
40         end
41 
42         def ==(value)
43           return value =~ @regexp unless value.is_a?(Regexp)
44           value == @regexp
45         end
46       end
47 
48       class BooleanMatcher
49         def initialize(ignore)
50         end
51 
52         def ==(value)
53           TrueClass === value || FalseClass === value
54         end
55       end
56 
57       class HashIncludingMatcher
58         def initialize(expected)
59           @expected = expected
60         end
61 
62         def ==(actual)
63           @expected.each do | key, value |
64             return false unless actual.has_key?(key) && value == actual[key]
65           end
66           true
67         rescue NoMethodError => ex
68           return false
69         end
70 
71         def description
72           "hash_including(#{@expected.inspect.sub(/^\{/,"").sub(/\}$/,"")})"
73         end
74       end
75       
76       class HashNotIncludingMatcher
77         def initialize(expected)
78           @expected = expected
79         end
80 
81         def ==(actual)
82           @expected.each do | key, value |
83             return false if actual.has_key?(key) && value == actual[key]
84           end
85           true
86         rescue NoMethodError => ex
87           return false
88         end
89 
90         def description
91           "hash_not_including(#{@expected.inspect.sub(/^\{/,"").sub(/\}$/,"")})"
92         end
93       end
94       
95       class DuckTypeMatcher
96         def initialize(*methods_to_respond_to)
97           @methods_to_respond_to = methods_to_respond_to
98         end
99 
100         def ==(value)
101           @methods_to_respond_to.all? { |sym| value.respond_to?(sym) }
102         end
103       end
104 
105       class MatcherMatcher
106         def initialize(matcher)
107           @matcher = matcher
108         end
109 
110         def ==(value)
111           @matcher.matches?(value)
112         end
113       end
114 
115       class EqualityProxy
116         def initialize(given)
117           @given = given
118         end
119 
120         def ==(expected)
121           @given == expected
122         end
123       end
124       
125       class InstanceOf
126         def initialize(klass)
127           @klass = klass
128         end
129         
130         def ==(actual)
131           actual.instance_of?(@klass)
132         end
133       end
134       
135       class KindOf
136         def initialize(klass)
137           @klass = klass
138         end
139         
140         def ==(actual)
141           actual.kind_of?(@klass)
142         end
143       end
144 
145       # :call-seq:
146       #   object.should_receive(:message).with(no_args())
147       #
148       # Passes if no arguments are passed along with the message
149       def no_args
150         NoArgsMatcher.new
151       end
152       
153       # :call-seq:
154       #   object.should_receive(:message).with(any_args())
155       #
156       # Passes if object receives :message with any args at all. This is
157       # really a more explicit variation of object.should_receive(:message)
158       def any_args
159         AnyArgsMatcher.new
160       end
161       
162       # :call-seq:
163       #   object.should_receive(:message).with(anything())
164       #
165       # Passes as long as there is an argument.
166       def anything
167         AnyArgMatcher.new(nil)
168       end
169       
170       # :call-seq:
171       #   object.should_receive(:message).with(duck_type(:hello))
172       #   object.should_receive(:message).with(duck_type(:hello, :goodbye))
173       #
174       # Passes if the argument responds to the specified messages.
175       #
176       # == Examples
177       #
178       #   array = []
179       #   display = mock('display')
180       #   display.should_receive(:present_names).with(duck_type(:length, :each))
181       #   => passes
182       def duck_type(*args)
183         DuckTypeMatcher.new(*args)
184       end
185 
186       # :call-seq:
187       #   object.should_receive(:message).with(boolean())
188       #
189       # Passes if the argument is boolean.
190       def boolean
191         BooleanMatcher.new(nil)
192       end
193       
194       # :call-seq:
195       #   object.should_receive(:message).with(hash_including(:key => val))
196       #   object.should_receive(:message).with(hash_including(:key))
197       #   object.should_receive(:message).with(hash_including(:key, :key2 => val2))
198       # Passes if the argument is a hash that includes the specified key(s) or key/value
199       # pairs. If the hash includes other keys, it will still pass.
200       def hash_including(*args)
201         HashIncludingMatcher.new(anythingize_lonely_keys(*args))
202       end
203       
204       # :call-seq:
205       #   object.should_receive(:message).with(hash_not_including(:key => val))
206       #   object.should_receive(:message).with(hash_not_including(:key))
207       #   object.should_receive(:message).with(hash_not_including(:key, :key2 => :val2))
208       #
209       # Passes if the argument is a hash that doesn't include the specified key(s) or key/value
210       def hash_not_including(*args)
211         HashNotIncludingMatcher.new(anythingize_lonely_keys(*args))
212       end
213       
214       # Passes if arg.instance_of?(klass)
215       def instance_of(klass)
216         InstanceOf.new(klass)
217       end
218       
219       alias_method :an_instance_of, :instance_of
220       
221       # Passes if arg.kind_of?(klass)
222       def kind_of(klass)
223         KindOf.new(klass)
224       end
225       
226       alias_method :a_kind_of, :kind_of
227       
228     private
229       
230       def anythingize_lonely_keys(*args)
231         hash = args.last.class == Hash ? args.delete_at(-1) : {}
232         args.each { | arg | hash[arg] = anything }
233         hash
234       end
235     end
236   end
237 end

Generated on Mon May 10 23:40:27 -0700 2010 with rcov 0.9.8