Class: RSpec::Matchers::Have

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/matchers/have.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected, relativity = :exactly) ⇒ Have

Returns a new instance of Have.



4
5
6
7
8
9
10
11
12
# File 'lib/rspec/matchers/have.rb', line 4

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
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



90
91
92
93
94
95
96
97
98
# File 'lib/rspec/matchers/have.rb', line 90

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

#descriptionObject



80
81
82
# File 'lib/rspec/matchers/have.rb', line 80

def description
  "have #{relative_expectation} #{@collection_name}"
end

#determine_collection(collection_or_owner) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rspec/matchers/have.rb', line 34

def determine_collection(collection_or_owner)
  if collection_or_owner.respond_to?(@collection_name)
    collection_or_owner.send(@collection_name, *@args, &@block)
  elsif (@plural_collection_name && collection_or_owner.respond_to?(@plural_collection_name))
    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

#determine_query_method(collection) ⇒ Object



46
47
48
# File 'lib/rspec/matchers/have.rb', line 46

def determine_query_method(collection)
  [:size, :length, :count].detect {|m| collection.respond_to?(m)}
end

#failure_message_for_shouldObject



54
55
56
# File 'lib/rspec/matchers/have.rb', line 54

def failure_message_for_should
  "expected #{relative_expectation} #{@collection_name}, got #{@actual}"
end

#failure_message_for_should_notObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rspec/matchers/have.rb', line 58

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:
  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

#matches?(collection_or_owner) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rspec/matchers/have.rb', line 22

def matches?(collection_or_owner)
  collection = determine_collection(collection_or_owner)
  query_method = determine_query_method(collection)
  raise not_a_collection unless query_method
  @actual = collection.send(query_method)
  case @relativity
  when :at_least then @actual >= @expected
  when :at_most  then @actual <= @expected
  else                @actual == @expected
  end
end

#not_a_collectionObject



50
51
52
# File 'lib/rspec/matchers/have.rb', line 50

def not_a_collection
  "expected #{@collection_name} to be a collection but it does not respond to #length, #size or #count"
end

#relativitiesObject



14
15
16
17
18
19
20
# File 'lib/rspec/matchers/have.rb', line 14

def relativities
  @relativities ||= {
    :exactly => "",
    :at_least => "at least ",
    :at_most => "at most "
  }
end

#respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/rspec/matchers/have.rb', line 84

def respond_to?(m)
  @expected.respond_to?(m) || super
end