Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
rcov/ruby/1.8/gems/rspec-expectations-2.5.0/lib/rspec/matchers/have.rb | 151 | 91 | 65.56%
|
42.86%
|
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 Have #:nodoc: |
4 def initialize(expected, relativity=:exactly) |
5 @expected = (expected == :no ? 0 : expected) |
6 @relativity = relativity |
7 @actual = @collection_name = @plural_collection_name = nil |
8 end |
9 |
10 def relativities |
11 @relativities ||= { |
12 :exactly => "", |
13 :at_least => "at least ", |
14 :at_most => "at most " |
15 } |
16 end |
17 |
18 def matches?(collection_owner) |
19 if collection_owner.respond_to?(@collection_name) |
20 collection = collection_owner.__send__(@collection_name, *@args, &@block) |
21 elsif (@plural_collection_name && collection_owner.respond_to?(@plural_collection_name)) |
22 collection = collection_owner.__send__(@plural_collection_name, *@args, &@block) |
23 elsif (collection_owner.respond_to?(:length) || collection_owner.respond_to?(:size)) |
24 collection = collection_owner |
25 else |
26 collection_owner.__send__(@collection_name, *@args, &@block) |
27 end |
28 @actual = collection.size if collection.respond_to?(:size) |
29 @actual = collection.length if collection.respond_to?(:length) |
30 raise not_a_collection if @actual.nil? |
31 return @actual >= @expected if @relativity == :at_least |
32 return @actual <= @expected if @relativity == :at_most |
33 return @actual == @expected |
34 end |
35 |
36 def not_a_collection |
37 "expected #{@collection_name} to be a collection but it does not respond to #length or #size" |
38 end |
39 |
40 def failure_message_for_should |
41 "expected #{relative_expectation} #{@collection_name}, got #{@actual}" |
42 end |
43 |
44 def failure_message_for_should_not |
45 if @relativity == :exactly |
46 return "expected target not to have #{@expected} #{@collection_name}, got #{@actual}" |
47 elsif @relativity == :at_most |
48 return <<-EOF |
49 Isn't life confusing enough? |
50 Instead of having to figure out the meaning of this: |
51 should_not have_at_most(#{@expected}).#{@collection_name} |
52 We recommend that you use this instead: |
53 should have_at_least(#{@expected + 1}).#{@collection_name} |
54 EOF |
55 elsif @relativity == :at_least |
56 return <<-EOF |
57 Isn't life confusing enough? |
58 Instead of having to figure out the meaning of this: |
59 should_not have_at_least(#{@expected}).#{@collection_name} |
60 We recommend that you use this instead: |
61 should have_at_most(#{@expected - 1}).#{@collection_name} |
62 EOF |
63 end |
64 end |
65 |
66 def description |
67 "have #{relative_expectation} #{@collection_name}" |
68 end |
69 |
70 def respond_to?(sym) |
71 @expected.respond_to?(sym) || super |
72 end |
73 |
74 private |
75 |
76 def method_missing(method, *args, &block) |
77 @collection_name = method |
78 if inflector = (defined?(ActiveSupport::Inflector) && ActiveSupport::Inflector.respond_to?(:pluralize) ? ActiveSupport::Inflector : (defined?(Inflector) ? Inflector : nil)) |
79 @plural_collection_name = inflector.pluralize(method.to_s) |
80 end |
81 @args = args |
82 @block = block |
83 self |
84 end |
85 |
86 def relative_expectation |
87 "#{relativities[@relativity]}#{@expected}" |
88 end |
89 end |
90 |
91 # :call-seq: |
92 # should have(number).named_collection__or__sugar |
93 # should_not have(number).named_collection__or__sugar |
94 # |
95 # Passes if receiver is a collection with the submitted |
96 # number of items OR if the receiver OWNS a collection |
97 # with the submitted number of items. |
98 # |
99 # If the receiver OWNS the collection, you must use the name |
100 # of the collection. So if a <tt>Team</tt> instance has a |
101 # collection named <tt>#players</tt>, you must use that name |
102 # to set the expectation. |
103 # |
104 # If the receiver IS the collection, you can use any name |
105 # you like for <tt>named_collection</tt>. We'd recommend using |
106 # either "elements", "members", or "items" as these are all |
107 # standard ways of describing the things IN a collection. |
108 # |
109 # This also works for Strings, letting you set an expectation |
110 # about its length |
111 # |
112 # == Examples |
113 # |
114 # # Passes if team.players.size == 11 |
115 # team.should have(11).players |
116 # |
117 # # Passes if [1,2,3].length == 3 |
118 # [1,2,3].should have(3).items #"items" is pure sugar |
119 # |
120 # # Passes if "this string".length == 11 |
121 # "this string".should have(11).characters #"characters" is pure sugar |
122 def have(n) |
123 Matchers::Have.new(n) |
124 end |
125 alias :have_exactly :have |
126 |
127 # :call-seq: |
128 # should have_at_least(number).items |
129 # |
130 # Exactly like have() with >=. |
131 # |
132 # == Warning |
133 # |
134 # +should_not+ +have_at_least+ is not supported |
135 def have_at_least(n) |
136 Matchers::Have.new(n, :at_least) |
137 end |
138 |
139 # :call-seq: |
140 # should have_at_most(number).items |
141 # |
142 # Exactly like have() with <=. |
143 # |
144 # == Warning |
145 # |
146 # +should_not+ +have_at_most+ is not supported |
147 def have_at_most(n) |
148 Matchers::Have.new(n, :at_most) |
149 end |
150 end |
151 end |
Generated on Fri Apr 22 17:22:42 -0700 2011 with rcov 0.9.8