Class: Spec::Matchers::Have
Overview
Instance Method Summary
collapse
Constructor Details
#initialize(expected, relativity = :exactly) ⇒ Have
Returns a new instance of Have.
5
6
7
8
|
# File 'lib/spec/matchers/have.rb', line 5
def initialize(expected, relativity=:exactly)
@expected = (expected == :no ? 0 : expected)
@relativity = relativity
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
18
19
20
21
22
23
|
# File 'lib/spec/matchers/have.rb', line 18
def method_missing(sym, *args, &block)
@collection_name = sym
@args = args
@block = block
self
end
|
Instance Method Details
#description ⇒ Object
71
72
73
|
# File 'lib/spec/matchers/have.rb', line 71
def description
"have #{relative_expectation} #{@collection_name}"
end
|
#failure_message ⇒ Object
45
46
47
|
# File 'lib/spec/matchers/have.rb', line 45
def failure_message
"expected #{relative_expectation} #{@collection_name}, got #{@actual}"
end
|
#matches?(collection_owner) ⇒ Boolean
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/spec/matchers/have.rb', line 25
def matches?(collection_owner)
if collection_owner.respond_to?(@collection_name)
collection = collection_owner.send(@collection_name, *@args, &@block)
elsif (collection_owner.respond_to?(:length) || collection_owner.respond_to?(:size))
collection = collection_owner
else
collection_owner.send(@collection_name, *@args, &@block)
end
@actual = collection.size if collection.respond_to?(:size)
@actual = collection.length if collection.respond_to?(:length)
raise not_a_collection if @actual.nil?
return @actual >= @expected if @relativity == :at_least
return @actual <= @expected if @relativity == :at_most
return @actual == @expected
end
|
#negative_failure_message ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/spec/matchers/have.rb', line 49
def negative_failure_message
if @relativity == :exactly
return "expected target not to have #{@expected} #{@collection_name}, got #{@actual}"
elsif @relativity == :at_most
return <<-EOF
Isn't life confusing enough?
Instead of having to figure out the meaning of this:
should_not have_at_most(#{@expected}).#{@collection_name}
We recommend that you use this instead:
should have_at_least(#{@expected + 1}).#{@collection_name}
EOF
elsif @relativity == :at_least
return <<-EOF
Isn't life confusing enough?
Instead of having to figure out the meaning of this:
should_not have_at_least(#{@expected}).#{@collection_name}
We recommend that you use this instead:
should have_at_most(#{@expected - 1}).#{@collection_name}
EOF
end
end
|
#not_a_collection ⇒ Object
41
42
43
|
# File 'lib/spec/matchers/have.rb', line 41
def not_a_collection
"expected #{@collection_name} to be a collection but it does not respond to #length or #size"
end
|
#relativities ⇒ Object
10
11
12
13
14
15
16
|
# File 'lib/spec/matchers/have.rb', line 10
def relativities
@relativities ||= {
:exactly => "",
:at_least => "at least ",
:at_most => "at most "
}
end
|