module RSpec::SleepingKingStudios::Matchers::Shared::MatchParameters

Helper methods for checking the parameters and keywords (Ruby 2.0 only) of a method.

Public Instance Methods

and_a_block()
Alias for: with_a_block
and_any_keywords()
Alias for: with_any_keywords
and_arbitrary_keywords()
and_keywords(*keywords)
Alias for: with_keywords
and_unlimited_arguments()
argument() click to toggle source

Convenience method for more fluent specs. Does nothing and returns self.

@return self

# File lib/rspec/sleeping_king_studios/matchers/shared/match_parameters.rb, line 14
def argument
  self
end
Also aliased as: arguments
arguments()
Alias for: argument
with(*keywords) click to toggle source

Adds a parameter count expectation and/or one or more keyword expectations.

@overload with count

Adds a parameter count expectation.

@param [Integer, Range, nil] count (optional) The number of expected
  parameters.

@return [RespondToMatcher] self

@overload with *keywords

Adds one or more keyword expectations.

@param [Array<String, Symbol>] keywords List of keyword arguments
  accepted by the method.

@return self

@overload with count, *keywords

Adds a parameter count expectation and one or more keyword
expectations.

@param [Integer, Range, nil] count (optional) The number of expected
  parameters.
@param [Array<String, Symbol>] keywords List of keyword arguments
  accepted by the method.

@return self
# File lib/rspec/sleeping_king_studios/matchers/shared/match_parameters.rb, line 46
def with *keywords
  case keywords.first
  when Range
    arity = keywords.shift

    method_signature_expectation.min_arguments = arity.begin
    method_signature_expectation.max_arguments = arity.end
  when Integer
    arity = keywords.shift

    method_signature_expectation.min_arguments = arity
    method_signature_expectation.max_arguments = arity
  end # case

  method_signature_expectation.keywords = keywords

  self
end
with_a_block() click to toggle source

Adds a block expectation. The actual object will only match a block expectation if it expects a parameter of the form &block.

@return self

# File lib/rspec/sleeping_king_studios/matchers/shared/match_parameters.rb, line 69
def with_a_block
  method_signature_expectation.block_argument = true

  self
end
Also aliased as: and_a_block
with_any_keywords()
Also aliased as: and_any_keywords
with_arbitrary_keywords() click to toggle source

Adds an arbitrary keyword expectation, e.g. that the method supports any keywords with splatted hash arguments of the form **kwargs.

# File lib/rspec/sleeping_king_studios/matchers/shared/match_parameters.rb, line 78
def with_arbitrary_keywords
  method_signature_expectation.any_keywords = true

  self
end
with_keywords(*keywords) click to toggle source

Adds one or more keyword expectations.

@param [Array<String, Symbol>] keywords List of keyword arguments

accepted by the method.

@return self

# File lib/rspec/sleeping_king_studios/matchers/shared/match_parameters.rb, line 93
def with_keywords *keywords
  method_signature_expectation.keywords = keywords

  self
end
Also aliased as: and_keywords
with_unlimited_arguments() click to toggle source

Adds an unlimited parameter count expectation, e.g. that the method supports splatted array arguments of the form *args.

@return self

# File lib/rspec/sleeping_king_studios/matchers/shared/match_parameters.rb, line 104
def with_unlimited_arguments
  method_signature_expectation.unlimited_arguments = true

  self
end
Also aliased as: and_unlimited_arguments

Private Instance Methods

check_method_signature(method) click to toggle source

@api private

# File lib/rspec/sleeping_king_studios/matchers/shared/match_parameters.rb, line 114
def check_method_signature method
  method_signature_expectation.matches?(method)
end
format_errors(errors) click to toggle source

@api private

# File lib/rspec/sleeping_king_studios/matchers/shared/match_parameters.rb, line 119
def format_errors errors
  messages = []

  # TODO: Replace this with "  expected :arity arguments, but method can "\
  # "receive :count arguments", with :count being one of the following:
  # - an integer (for methods that do not have optional or variadic params)
  # - between :min and :max (for methods with optional but not variadic
  #   params)
  # - at least :min (for methods with variadic params)
  if hsh = errors.fetch(:not_enough_args, false)
    messages << "  expected at least #{hsh[:expected]} arguments, but received #{hsh[:received]}"
  end # if

  if hsh = errors.fetch(:too_many_args, false)
    messages << "  expected at most #{hsh[:expected]} arguments, but received #{hsh[:received]}"
  end # if

  # TODO: Replace this with "  expected method to receive unlimited "\
  # "arguments, but method can receive at most :max arguments"
  if hsh = errors.fetch(:no_variadic_args, false)
    messages << "  expected at most #{hsh[:expected]} arguments, but received unlimited arguments"
  end # if

  # TODO: Replace this with "  expected method to receive arbitrary "\
  # "keywords, but the method can receive :keyword_list", with
  # :keyword_list being a comma-separated list. If the method cannot
  # receive keywords, replace last fragment with ", but the method cannot"\
  # " receive keywords"
  if errors.fetch(:no_variadic_keywords, false)
    messages << "  expected arbitrary keywords"
  end # if

  # TODO: Replace this with "  expected method to receive keywords "\
  # ":received_list, but the method requires keywords :required_list"
  if ary = errors.fetch(:missing_keywords, false)
    tools = ::SleepingKingStudios::Tools::ArrayTools

    messages <<
      "  missing keyword#{ary.count == 1 ? '' : 's'} "\
      "#{tools.humanize_list ary.map(&:inspect)}"
  end # if

  # TODO: Replace this with "  expected method to receive keywords "\
  # ":received_list, but the method can receive :keyword_list"
  if ary = errors.fetch(:unexpected_keywords, false)
    tools = ::SleepingKingStudios::Tools::ArrayTools

    messages <<
      "  unexpected keyword#{ary.count == 1 ? '' : 's'} "\
      "#{tools.humanize_list ary.map(&:inspect)}"
  end # if

  # TODO: Replace this with "  expected method to receive a block "\
  # "argument, but the method signature does not specify a block argument"
  if errors.fetch(:no_block_argument, false)
    messages << "  unexpected block"
  end # if

  messages.join "\n"
end
method_signature_expectation() click to toggle source

@api private

# File lib/rspec/sleeping_king_studios/matchers/shared/match_parameters.rb, line 181
def method_signature_expectation
  @method_signature_expectation ||=
    ::RSpec::SleepingKingStudios::Support::MethodSignatureExpectation.new
end
method_signature_expectation?() click to toggle source

@api private

# File lib/rspec/sleeping_king_studios/matchers/shared/match_parameters.rb, line 187
def method_signature_expectation?
  !!@method_signature_expectation
end