Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
rcov/ruby/1.8/gems/rspec-expectations-2.5.0/lib/rspec/matchers/be.rb | 222 | 146 | 52.70%
|
32.88%
|
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 require 'rspec/matchers/dsl' |
2 |
3 RSpec::Matchers.define :be_true do |
4 match do |actual| |
5 actual |
6 end |
7 end |
8 |
9 RSpec::Matchers.define :be_false do |
10 match do |actual| |
11 !actual |
12 end |
13 end |
14 |
15 RSpec::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\n got: #{actual.inspect}" |
22 end |
23 |
24 failure_message_for_should_not do |
25 "expected: not nil\n got: nil" |
26 end |
27 end |
28 |
29 module RSpec |
30 module Matchers |
31 |
32 class Be #:nodoc: |
33 include RSpec::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 "(#{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.inspect}\n got: #{@operator.to_s.gsub(/./, ' ')} #{@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 # :call-seq: |
181 # should be_true |
182 # should be_false |
183 # should be_nil |
184 # should be_[arbitrary_predicate](*args) |
185 # should_not be_nil |
186 # should_not be_[arbitrary_predicate](*args) |
187 # |
188 # Given true, false, or nil, will pass if actual value is |
189 # true, false or nil (respectively). Given no args means |
190 # the caller should satisfy an if condition (to be or not to be). |
191 # |
192 # Predicates are any Ruby method that ends in a "?" and returns true or false. |
193 # Given be_ followed by arbitrary_predicate (without the "?"), RSpec will match |
194 # convert that into a query against the target object. |
195 # |
196 # The arbitrary_predicate feature will handle any predicate |
197 # prefixed with "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of) |
198 # or "be_" (e.g. be_empty), letting you choose the prefix that best suits the predicate. |
199 # |
200 # == Examples |
201 # |
202 # target.should be_true |
203 # target.should be_false |
204 # target.should be_nil |
205 # target.should_not be_nil |
206 # |
207 # collection.should be_empty #passes if target.empty? |
208 # target.should_not be_empty #passes unless target.empty? |
209 # target.should_not be_old_enough(16) #passes unless target.old_enough?(16) |
210 def be(*args) |
211 args.empty? ? |
212 Matchers::Be.new : equal(*args) |
213 end |
214 |
215 # passes if target.kind_of?(klass) |
216 def be_a(klass) |
217 be_a_kind_of(klass) |
218 end |
219 |
220 alias_method :be_an, :be_a |
221 end |
222 end |
Generated on Fri Apr 22 17:22:41 -0700 2011 with rcov 0.9.8