Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
rcov/ruby/1.8/gems/rspec-expectations-2.5.0/lib/rspec/matchers.rb | 196 | 39 | 96.94%
|
84.62%
|
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 # rspec-expecations provides a number of useful Matchers we use to compose |
3 # expectations. A Matcher is any object that responds to the following |
4 # methods: |
5 # |
6 # matches?(actual) |
7 # failure_message_for_should |
8 # |
9 # These methods are also part of the matcher protocol, but are optional: |
10 # |
11 # does_not_match?(actual) |
12 # failure_message_for_should_not |
13 # description #optional |
14 # |
15 # == Predicates |
16 # |
17 # In addition to those Expression Matchers that are defined explicitly, RSpec will |
18 # create custom Matchers on the fly for any arbitrary predicate, giving your specs |
19 # a much more natural language feel. |
20 # |
21 # A Ruby predicate is a method that ends with a "?" and returns true or false. |
22 # Common examples are +empty?+, +nil?+, and +instance_of?+. |
23 # |
24 # All you need to do is write +should be_+ followed by the predicate without |
25 # the question mark, and RSpec will figure it out from there. For example: |
26 # |
27 # [].should be_empty => [].empty? #passes |
28 # [].should_not be_empty => [].empty? #fails |
29 # |
30 # In addtion to prefixing the predicate matchers with "be_", you can also use "be_a_" |
31 # and "be_an_", making your specs read much more naturally: |
32 # |
33 # "a string".should be_an_instance_of(String) =>"a string".instance_of?(String) #passes |
34 # |
35 # 3.should be_a_kind_of(Fixnum) => 3.kind_of?(Numeric) #passes |
36 # 3.should be_a_kind_of(Numeric) => 3.kind_of?(Numeric) #passes |
37 # 3.should be_an_instance_of(Fixnum) => 3.instance_of?(Fixnum) #passes |
38 # 3.should_not be_instance_of(Numeric) => 3.instance_of?(Numeric) #fails |
39 # |
40 # RSpec will also create custom matchers for predicates like +has_key?+. To |
41 # use this feature, just state that the object should have_key(:key) and RSpec will |
42 # call has_key?(:key) on the target. For example: |
43 # |
44 # {:a => "A"}.should have_key(:a) => {:a => "A"}.has_key?(:a) #passes |
45 # {:a => "A"}.should have_key(:b) => {:a => "A"}.has_key?(:b) #fails |
46 # |
47 # You can use this feature to invoke any predicate that begins with "has_", whether it is |
48 # part of the Ruby libraries (like +Hash#has_key?+) or a method you wrote on your own class. |
49 # |
50 # == Custom Matchers |
51 # |
52 # When you find that none of the stock Expectation Matchers provide a natural |
53 # feeling expectation, you can very easily write your own using RSpec's matcher |
54 # DSL or writing one from scratch. |
55 # |
56 # === Matcher DSL |
57 # |
58 # Imagine that you are writing a game in which players can be in various |
59 # zones on a virtual board. To specify that bob should be in zone 4, you |
60 # could say: |
61 # |
62 # bob.current_zone.should eql(Zone.new("4")) |
63 # |
64 # But you might find it more expressive to say: |
65 # |
66 # bob.should be_in_zone("4") |
67 # |
68 # and/or |
69 # |
70 # bob.should_not be_in_zone("3") |
71 # |
72 # You can create such a matcher like so: |
73 # |
74 # RSpec::Matchers.define :be_in_zone do |zone| |
75 # match do |player| |
76 # player.in_zone?(zone) |
77 # end |
78 # end |
79 # |
80 # This will generate a <tt>be_in_zone</tt> method that returns a matcher |
81 # with logical default messages for failures. You can override the failure |
82 # messages and the generated description as follows: |
83 # |
84 # RSpec::Matchers.define :be_in_zone do |zone| |
85 # match do |player| |
86 # player.in_zone?(zone) |
87 # end |
88 # failure_message_for_should do |player| |
89 # # generate and return the appropriate string. |
90 # end |
91 # failure_message_for_should_not do |player| |
92 # # generate and return the appropriate string. |
93 # end |
94 # description do |
95 # # generate and return the appropriate string. |
96 # end |
97 # end |
98 # |
99 # Each of the message-generation methods has access to the block arguments |
100 # passed to the <tt>create</tt> method (in this case, <tt>zone</tt>). The |
101 # failure message methods (<tt>failure_message_for_should</tt> and |
102 # <tt>failure_message_for_should_not</tt>) are passed the actual value (the |
103 # receiver of <tt>should</tt> or <tt>should_not</tt>). |
104 # |
105 # === Custom Matcher from scratch |
106 # |
107 # You could also write a custom matcher from scratch, as follows: |
108 # |
109 # class BeInZone |
110 # def initialize(expected) |
111 # @expected = expected |
112 # end |
113 # def matches?(target) |
114 # @target = target |
115 # @target.current_zone.eql?(Zone.new(@expected)) |
116 # end |
117 # def failure_message_for_should |
118 # "expected #{@target.inspect} to be in Zone #{@expected}" |
119 # end |
120 # def failure_message_for_should_not |
121 # "expected #{@target.inspect} not to be in Zone #{@expected}" |
122 # end |
123 # end |
124 # |
125 # ... and a method like this: |
126 # |
127 # def be_in_zone(expected) |
128 # BeInZone.new(expected) |
129 # end |
130 # |
131 # And then expose the method to your specs. This is normally done |
132 # by including the method and the class in a module, which is then |
133 # included in your spec: |
134 # |
135 # module CustomGameMatchers |
136 # class BeInZone |
137 # ... |
138 # end |
139 # |
140 # def be_in_zone(expected) |
141 # ... |
142 # end |
143 # end |
144 # |
145 # describe "Player behaviour" do |
146 # include CustomGameMatchers |
147 # ... |
148 # end |
149 # |
150 # or you can include in globally in a spec_helper.rb file <tt>require</tt>d |
151 # from your spec file(s): |
152 # |
153 # RSpec::Runner.configure do |config| |
154 # config.include(CustomGameMatchers) |
155 # end |
156 # |
157 module Matchers |
158 # Include Matchers for other test frameworks |
159 if defined?(Test::Unit::TestCase) |
160 Test::Unit::TestCase.send(:include, self) |
161 end |
162 if defined?(MiniTest::Unit::TestCase) |
163 MiniTest::Unit::TestCase.send(:include, self) |
164 end |
165 end |
166 end |
167 |
168 require 'rspec/matchers/extensions/instance_exec' |
169 require 'rspec/matchers/pretty' |
170 require 'rspec/matchers/matcher' |
171 require 'rspec/matchers/operator_matcher' |
172 require 'rspec/matchers/be' |
173 require 'rspec/matchers/be_close' |
174 require 'rspec/matchers/be_instance_of' |
175 require 'rspec/matchers/be_kind_of' |
176 require 'rspec/matchers/be_within' |
177 require 'rspec/matchers/block_aliases' |
178 require 'rspec/matchers/change' |
179 require 'rspec/matchers/eq' |
180 require 'rspec/matchers/eql' |
181 require 'rspec/matchers/equal' |
182 require 'rspec/matchers/errors' |
183 require 'rspec/matchers/exist' |
184 require 'rspec/matchers/generated_descriptions' |
185 require 'rspec/matchers/has' |
186 require 'rspec/matchers/have' |
187 require 'rspec/matchers/include' |
188 require 'rspec/matchers/match' |
189 require 'rspec/matchers/match_array' |
190 require 'rspec/matchers/method_missing' |
191 require 'rspec/matchers/raise_error' |
192 require 'rspec/matchers/respond_to' |
193 require 'rspec/matchers/satisfy' |
194 require 'rspec/matchers/throw_symbol' |
195 require 'rspec/matchers/compatibility' |
196 require 'rspec/matchers/dsl' |
Generated on Fri Apr 22 17:22:42 -0700 2011 with rcov 0.9.8