Class: RSpec::Matchers::BuiltIn::Have
- Inherits:
-
Object
- Object
- RSpec::Matchers::BuiltIn::Have
show all
- Includes:
- MatchAliases
- Defined in:
- lib/rspec/matchers/built_in/have.rb
Constant Summary
- QUERY_METHODS =
[:size, :length, :count].freeze
Instance Method Summary
(collapse)
#==, #===
Constructor Details
- (Have) initialize(expected, relativity = :exactly)
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/rspec/matchers/built_in/have.rb', line 9
def initialize(expected, relativity=:exactly)
@expected = case expected
when :no then 0
when String then expected.to_i
else expected
end
@relativity = relativity
@actual = @collection_name = @plural_collection_name = nil
@target_owns_a_collection = false
@negative_expectation = false
@expectation_format_method = "to"
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(method, *args)
124
125
126
127
128
129
130
131
132
|
# File 'lib/rspec/matchers/built_in/have.rb', line 124
def method_missing(method, *args, &block)
@collection_name = method
if inflector = (defined?(ActiveSupport::Inflector) && ActiveSupport::Inflector.respond_to?(:pluralize) ? ActiveSupport::Inflector : (defined?(Inflector) ? Inflector : nil))
@plural_collection_name = inflector.pluralize(method.to_s)
end
@args = args
@block = block
self
end
|
Instance Method Details
- (Object) description
114
115
116
|
# File 'lib/rspec/matchers/built_in/have.rb', line 114
def description
"have #{relative_expectation} #{@collection_name}"
end
|
- (Object) determine_collection(collection_or_owner)
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/rspec/matchers/built_in/have.rb', line 66
def determine_collection(collection_or_owner)
if collection_or_owner.respond_to?(@collection_name)
@target_owns_a_collection = true
collection_or_owner.__send__(@collection_name, *@args, &@block)
elsif (@plural_collection_name && collection_or_owner.respond_to?(@plural_collection_name))
@target_owns_a_collection = true
collection_or_owner.__send__(@plural_collection_name, *@args, &@block)
elsif determine_query_method(collection_or_owner)
collection_or_owner
else
collection_or_owner.__send__(@collection_name, *@args, &@block)
end
end
|
- (Object) determine_query_method(collection)
80
81
82
|
# File 'lib/rspec/matchers/built_in/have.rb', line 80
def determine_query_method(collection)
QUERY_METHODS.detect {|m| collection.respond_to?(m)}
end
|
- (Boolean) does_not_match?(collection_or_owner)
60
61
62
63
64
|
# File 'lib/rspec/matchers/built_in/have.rb', line 60
def does_not_match?(collection_or_owner)
@negative_expectation = true
@expectation_format_method = "to_not"
!matches?(collection_or_owner)
end
|
- (Object) failure_message_for_should
88
89
90
|
# File 'lib/rspec/matchers/built_in/have.rb', line 88
def failure_message_for_should
"expected #{relative_expectation} #{@collection_name}, got #{@actual}"
end
|
- (Object) failure_message_for_should_not
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/rspec/matchers/built_in/have.rb', line 92
def failure_message_for_should_not
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:
#{Expectations::Syntax.negative_expression("actual", "have_at_most(#{@expected}).#{@collection_name}")}
We recommend that you use this instead:
#{Expectations::Syntax.positive_expression("actual", "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:
#{Expectations::Syntax.negative_expression("actual", "have_at_least(#{@expected}).#{@collection_name}")}
We recommend that you use this instead:
#{Expectations::Syntax.positive_expression("actual", "have_at_most(#{@expected - 1}).#{@collection_name}")}
EOF
end
end
|
- (Boolean) matches?(collection_or_owner)
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/rspec/matchers/built_in/have.rb', line 31
def matches?(collection_or_owner)
collection = determine_collection(collection_or_owner)
case collection
when enumerator_class
for query_method in QUERY_METHODS
next unless collection.respond_to?(query_method)
@actual = collection.__send__(query_method)
if @actual
print_deprecation_message(query_method)
break
end
end
raise not_a_collection if @actual.nil?
else
query_method = determine_query_method(collection)
raise not_a_collection unless query_method
@actual = collection.__send__(query_method)
print_deprecation_message(query_method)
end
case @relativity
when :at_least then @actual >= @expected
when :at_most then @actual <= @expected
else @actual == @expected
end
end
|
- (Object) not_a_collection
84
85
86
|
# File 'lib/rspec/matchers/built_in/have.rb', line 84
def not_a_collection
"expected #{@collection_name} to be a collection but it does not respond to #length, #size or #count"
end
|
- (Object) relativities
23
24
25
26
27
28
29
|
# File 'lib/rspec/matchers/built_in/have.rb', line 23
def relativities
@relativities ||= {
:exactly => "",
:at_least => "at least ",
:at_most => "at most "
}
end
|
- (Boolean) respond_to?(m)
118
119
120
|
# File 'lib/rspec/matchers/built_in/have.rb', line 118
def respond_to?(m)
@expected.respond_to?(m) || super
end
|