class RSpec::CollectionMatchers::Have

Constants

IGNORED_CLASSES
QUERY_METHODS

Public Class Methods

new(expected, relativity=:exactly) click to toggle source
# File lib/rspec/collection_matchers/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
end

Public Instance Methods

==(collection_or_owner)
Alias for: matches?
description() click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 114
def description
  return errors_on_message(:have) if is_errors_on?
  "have #{relative_expectation} #{@collection_name}"
end
determine_collection(collection_or_owner) click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 61
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) click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 73
def determine_query_method(collection)
  QUERY_METHODS.detect {|m| collection.respond_to?(m)}
end
failure_message() click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 85
def failure_message
  return errors_on_message(:expected, ", got #{@actual}") if is_errors_on?
  "expected #{relative_expectation} #{@collection_name}, got #{@actual}"
end
Also aliased as: failure_message_for_should
failure_message_for_should()
Alias for: failure_message
failure_message_for_should_not()
failure_message_when_negated() click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 91
      def failure_message_when_negated
        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:
  #{Syntax.negative_expression("actual", "have_at_most(#{@expected}).#{@collection_name}")}
We recommend that you use this instead:
  #{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:
  #{Syntax.negative_expression("actual", "have_at_least(#{@expected}).#{@collection_name}")}
We recommend that you use this instead:
  #{Syntax.positive_expression("actual", "have_at_most(#{@expected - 1}).#{@collection_name}")}
EOF
        end
      end
is_ignored_class?(collection) click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 77
def is_ignored_class?(collection)
  IGNORED_CLASSES.any? {|klass| klass === collection}
end
matches?(collection_or_owner) click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 38
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)
      break unless @actual.nil?
    end
    raise not_a_collection if @actual.nil?
  else
    query_method = determine_query_method(collection)
    raise not_a_collection if !query_method || is_ignored_class?(collection)
    @actual = collection.__send__(query_method)
  end
  case @relativity
  when :at_least then @actual >= @expected
  when :at_most  then @actual <= @expected
  else                @actual == @expected
  end
end
Also aliased as: ==
not_a_collection() click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 81
def not_a_collection
  "expected #{@collection_name} to be a collection but it does not respond to #length, #size or #count"
end
relativities() click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 19
def relativities
  @relativities ||= {
    :exactly => "",
    :at_least => "at least ",
    :at_most => "at most "
  }
end
respond_to?(m, include_all = false) click to toggle source
Calls superclass method
# File lib/rspec/collection_matchers/have.rb, line 119
def respond_to?(m, include_all = false)
  @expected.respond_to?(m, include_all) || super
end
to_ary() click to toggle source

On Ruby 1.9.2 items that don't return an array for `to_ary` can't be flattened in arrays, we need to be able to do this to produce diffs for compound matchers, so this corrects the default implementation. Note that rspec-support has code that directly checks for pattern and prevents infinite recursion.

# File lib/rspec/collection_matchers/have.rb, line 33
def to_ary
  [self]
end

Private Instance Methods

enumerator_class() click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 139
def enumerator_class
  RUBY_VERSION < '1.9' ? Enumerable::Enumerator : Enumerator
end
errors_on_message(prefix, suffix = nil) click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 147
def errors_on_message(prefix, suffix = nil)
  "#{prefix} #{relative_expectation} #{@collection_name.to_s.gsub('_', ' ')} :#{@args[0]}#{suffix}"
end
is_errors_on?() click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 143
def is_errors_on?
  [:errors_on, :error_on].include? @collection_name
end
method_missing(method, *args, &block) click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 125
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
relative_expectation() click to toggle source
# File lib/rspec/collection_matchers/have.rb, line 135
def relative_expectation
  "#{relativities[@relativity]}#{@expected}"
end