Geminstaller C0 Coverage Information - RCov

/Users/woolley/.rvm/gems/ruby-1.8.7-p174@geminstaller/gems/rspec-1.3.0/lib/spec/matchers/be.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/matchers/be.rb 249 166
67.47%
54.82%

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 require 'spec/matchers/dsl'
2 
3 Spec::Matchers.define :be_true do
4   match do |actual|
5     !!actual
6   end
7 end
8 
9 Spec::Matchers.define :be_false do
10   match do |actual|
11     !actual
12   end
13 end
14 
15 Spec::Matchers.define :be_nil do
16   match do |actual|
17     actual.nil?
18   end
19 
20   failure_message_for_should do |actual|
21     "expected nil, got #{actual.inspect}"
22   end
23 
24   failure_message_for_should_not do
25     "expected not nil, got nil"
26   end
27 end
28 
29 module Spec
30   module Matchers
31 
32     class Be #:nodoc:
33       include Spec::Matchers::Pretty
34       
35       def initialize(*args, &block)
36         @args = args
37       end
38       
39       def matches?(actual)
40         @actual = actual
41         !!@actual
42       end
43 
44       def failure_message_for_should
45         "expected #{@actual.inspect} to evaluate to true"
46       end
47       
48       def failure_message_for_should_not
49         "expected #{@actual.inspect} to evaluate to false"
50       end
51       
52       def description
53         "be"
54       end
55 
56       [:==, :<, :<=, :>=, :>, :===].each do |operator|
57         define_method operator do |operand|
58           BeComparedTo.new(operand, operator)
59         end
60       end
61 
62     private
63 
64       def args_to_s
65         @args.empty? ? "" : parenthesize(inspected_args.join(', '))
66       end
67       
68       def parenthesize(string)
69         return "(#{string})"
70       end
71       
72       def inspected_args
73         @args.collect{|a| a.inspect}
74       end
75       
76       def expected_to_sentence
77         split_words(@expected)
78       end
79       
80       def args_to_sentence
81         to_sentence(@args)
82       end
83         
84     end
85 
86     class BeComparedTo < Be
87 
88       def initialize(operand, operator)
89         @expected, @operator = operand, operator
90         @args = []
91       end
92 
93       def matches?(actual)
94         @actual = actual
95         @actual.__send__(@operator, @expected)
96       end
97 
98       def failure_message_for_should
99         "expected #{@operator} #{@expected}, got #{@actual.inspect}"
100       end
101       
102       def failure_message_for_should_not
103         message = <<-MESSAGE
104 'should_not be #{@operator} #{@expected}' not only FAILED,
105 it is a bit confusing.
106           MESSAGE
107           
108         raise message << ([:===,:==].include?(@operator) ?
109           "It might be more clearly expressed without the \"be\"?" :
110           "It might be more clearly expressed in the positive?")
111       end
112 
113       def description
114         "be #{@operator} #{expected_to_sentence}#{args_to_sentence}"
115       end
116 
117     end
118 
119     class BePredicate < Be
120 
121       def initialize(*args, &block)
122         @expected = parse_expected(args.shift)
123         @args = args
124         @block = block
125       end
126       
127       def matches?(actual)
128         @actual = actual
129         begin
130           return @result = actual.__send__(predicate, *@args, &@block)
131         rescue NameError => predicate_missing_error
132           "this needs to be here or rcov will not count this branch even though it's executed in a code example"
133         end
134 
135         begin
136           return @result = actual.__send__(present_tense_predicate, *@args, &@block)
137         rescue NameError
138           raise predicate_missing_error
139         end
140       end
141       
142       def failure_message_for_should
143         "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}"
144       end
145       
146       def failure_message_for_should_not
147         "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
148       end
149 
150       def description
151         "#{prefix_to_sentence}#{expected_to_sentence}#{args_to_sentence}"
152       end
153 
154     private
155 
156       def predicate
157         "#{@expected}?".to_sym
158       end
159       
160       def present_tense_predicate
161         "#{@expected}s?".to_sym
162       end
163       
164       def parse_expected(expected)
165         @prefix, expected = prefix_and_expected(expected)
166         expected
167       end
168 
169       def prefix_and_expected(symbol)
170         symbol.to_s =~ /^(be_(an?_)?)(.*)/
171         return $1, $3
172       end
173 
174       def prefix_to_sentence
175         split_words(@prefix)
176       end
177 
178     end
179 
180     class BeSameAs < Be
181       
182       def initialize(*args, &block)
183         @expected = args.shift
184         @args = args
185       end
186       
187       def matches?(actual)
188         @actual = actual
189         @actual.equal?(@expected)
190       end
191 
192       def failure_message_for_should
193         "expected #{@expected}, got #{@actual.inspect}"
194       end
195       
196       def failure_message_for_should_not
197         "expected not #{@expected}, got #{@actual.inspect}"
198       end
199 
200       def description
201         "be #{expected_to_sentence}#{args_to_sentence}"
202       end
203 
204     end
205  
206     # :call-seq:
207     #   should be_true
208     #   should be_false
209     #   should be_nil
210     #   should be_[arbitrary_predicate](*args)
211     #   should_not be_nil
212     #   should_not be_[arbitrary_predicate](*args)
213     #
214     # Given true, false, or nil, will pass if actual value is
215     # true, false or nil (respectively). Given no args means
216     # the caller should satisfy an if condition (to be or not to be). 
217     #
218     # Predicates are any Ruby method that ends in a "?" and returns true or false.
219     # Given be_ followed by arbitrary_predicate (without the "?"), RSpec will match
220     # convert that into a query against the target object.
221     #
222     # The arbitrary_predicate feature will handle any predicate
223     # prefixed with "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of)
224     # or "be_" (e.g. be_empty), letting you choose the prefix that best suits the predicate.
225     #
226     # == Examples 
227     #
228     #   target.should be_true
229     #   target.should be_false
230     #   target.should be_nil
231     #   target.should_not be_nil
232     #
233     #   collection.should be_empty #passes if target.empty?
234     #   target.should_not be_empty #passes unless target.empty?
235     #   target.should_not be_old_enough(16) #passes unless target.old_enough?(16)
236     def be(*args)
237       args.empty? ?
238         Matchers::Be.new :
239         Matchers::BeSameAs.new(*args)
240     end
241 
242     # passes if target.kind_of?(klass)
243     def be_a(klass)
244       be_a_kind_of(klass)
245     end
246     
247     alias_method :be_an, :be_a
248   end
249 end

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