Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
rcov/ruby/1.8/gems/rspec-expectations-2.5.0/lib/rspec/matchers/matcher.rb | 161 | 117 | 71.43%
|
60.68%
|
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 Matchers |
3 class Matcher |
4 include RSpec::Matchers::InstanceExec |
5 include RSpec::Matchers::Pretty |
6 include RSpec::Matchers |
7 |
8 attr_reader :expected, :actual, :rescued_exception |
9 def initialize(name, *expected, &declarations) |
10 @name = name |
11 @expected = expected |
12 @actual = nil |
13 @diffable = false |
14 @expected_exception, @rescued_exception = nil, nil |
15 @match_for_should_not_block = nil |
16 |
17 @messages = { |
18 :description => lambda {"#{name_to_sentence}#{expected_to_sentence}"}, |
19 :failure_message_for_should => lambda {|actual| "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"}, |
20 :failure_message_for_should_not => lambda {|actual| "expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"} |
21 } |
22 making_declared_methods_public do |
23 instance_exec(*@expected, &declarations) |
24 end |
25 end |
26 |
27 #Used internally by +should+ and +should_not+. |
28 def matches?(actual) |
29 @actual = actual |
30 if @expected_exception |
31 begin |
32 instance_exec(actual, &@match_block) |
33 true |
34 rescue @expected_exception => @rescued_exception |
35 false |
36 end |
37 else |
38 begin |
39 instance_exec(actual, &@match_block) |
40 rescue RSpec::Expectations::ExpectationNotMetError |
41 false |
42 end |
43 end |
44 end |
45 |
46 # Used internally by +should_not+ |
47 def does_not_match?(actual) |
48 @actual = actual |
49 @match_for_should_not_block ? |
50 instance_exec(actual, &@match_for_should_not_block) : |
51 !matches?(actual) |
52 end |
53 |
54 def define_method(name, &block) # :nodoc: |
55 singleton_class.__send__(:define_method, name, &block) |
56 end |
57 |
58 # See RSpec::Matchers |
59 def match(&block) |
60 @match_block = block |
61 end |
62 alias match_for_should match |
63 |
64 # See RSpec::Matchers |
65 def match_for_should_not(&block) |
66 @match_for_should_not_block = block |
67 end |
68 |
69 # See RSpec::Matchers |
70 def match_unless_raises(exception=Exception, &block) |
71 @expected_exception = exception |
72 match(&block) |
73 end |
74 |
75 # See RSpec::Matchers |
76 def failure_message_for_should(&block) |
77 cache_or_call_cached(:failure_message_for_should, &block) |
78 end |
79 |
80 # See RSpec::Matchers |
81 def failure_message_for_should_not(&block) |
82 cache_or_call_cached(:failure_message_for_should_not, &block) |
83 end |
84 |
85 # See RSpec::Matchers |
86 def description(&block) |
87 cache_or_call_cached(:description, &block) |
88 end |
89 |
90 #Used internally by objects returns by +should+ and +should_not+. |
91 def diffable? |
92 @diffable |
93 end |
94 |
95 # See RSpec::Matchers |
96 def diffable |
97 @diffable = true |
98 end |
99 |
100 # See RSpec::Matchers |
101 def chain(method, &block) |
102 define_method method do |*args| |
103 block.call(*args) |
104 self |
105 end |
106 end |
107 |
108 private |
109 |
110 def method_missing(method, *args, &block) |
111 if $matcher_execution_context.respond_to?(method) |
112 $matcher_execution_context.send method, *args, &block |
113 else |
114 super(method, *args, &block) |
115 end |
116 end |
117 |
118 def making_declared_methods_public # :nodoc: |
119 # Our home-grown instance_exec in ruby 1.8.6 results in any methods |
120 # declared in the block eval'd by instance_exec in the block to which we |
121 # are yielding here are scoped private. This is NOT the case for Ruby |
122 # 1.8.7 or 1.9. |
123 # |
124 # Also, due some crazy scoping that I don't understand, these methods |
125 # are actually available in the specs (something about the matcher being |
126 # defined in the scope of RSpec::Matchers or within an example), so not |
127 # doing the following will not cause specs to fail, but they *will* |
128 # cause features to fail and that will make users unhappy. So don't. |
129 orig_private_methods = private_methods |
130 yield |
131 (private_methods - orig_private_methods).each {|m| singleton_class.__send__ :public, m} |
132 end |
133 |
134 def cache_or_call_cached(key, &block) |
135 block ? cache(key, &block) : call_cached(key) |
136 end |
137 |
138 def cache(key, &block) |
139 @messages[key] = block |
140 end |
141 |
142 def call_cached(key) |
143 @messages[key].arity == 1 ? @messages[key].call(@actual) : @messages[key].call |
144 end |
145 |
146 def name_to_sentence |
147 split_words(@name) |
148 end |
149 |
150 def expected_to_sentence |
151 to_sentence(@expected) |
152 end |
153 |
154 unless method_defined?(:singleton_class) |
155 def singleton_class |
156 class << self; self; end |
157 end |
158 end |
159 end |
160 end |
161 end |
Generated on Fri Apr 22 17:22:42 -0700 2011 with rcov 0.9.8