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