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