module RSpec::SleepingKingStudios::Matchers::Shared::MatchParameters
Helper methods for checking the parameters and keywords (Ruby 2.0 only) of a method.
Public Instance Methods
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
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
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
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
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
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
Private Instance Methods
@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
@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
@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
@api private
# File lib/rspec/sleeping_king_studios/matchers/shared/match_parameters.rb, line 187 def method_signature_expectation? !!@method_signature_expectation end