Class: RSpec::Support::MethodSignature

### Home Classes Methods

### In Files

- lib/rspec/support/method_signature_verifier.rb

### Parent

Object

### Methods

- ::new
- #classify_parameters
- #could_contain_kw_args?
- #description
- #has_kw_args_in?
- #invalid_kw_args_from
- #missing_kw_args_from
- #non_kw_args_arity_description
- #valid_non_kw_args?

### Class/Module Index

Quicksearch

- RSpec
- RSpec::CallerFilter
- RSpec::Support
- RSpec::Support::BlockSignature
- RSpec::Support::Differ
- RSpec::Support::DirectoryMaker
- RSpec::Support::EncodedString
- RSpec::Support::FormattingSupport
- RSpec::Support::FuzzyMatcher
- RSpec::Support::HunkGenerator
- RSpec::Support::InSubProcess
- RSpec::Support::LooseSignatureVerifier
- RSpec::Support::LooseSignatureVerifier::SignatureWithKeywordArgumentsMatcher
- RSpec::Support::MethodSignature
- RSpec::Support::MethodSignatureVerifier
- RSpec::Support::OS
- RSpec::Support::RecursiveConstMethods
- RSpec::Support::Ruby
- RSpec::Support::RubyFeatures
- RSpec::Support::ShellOut
- RSpec::Support::Spec
- RSpec::Support::StdErrSplitter
- RSpec::Support::StrictSignatureVerifier
- RSpec::Support::Version
- RSpec::Support::VersionChecker
- RSpec::Support::Warnings
- RSpec::Support::WarningsPrevention
- RSpec::Support::WithIsolatedStdErr
- Object
- RSpecHelpers

No matching classes.

# RSpec::Support::MethodSignature

Extracts info about the number of arguments and allowed/required keyword args of a given method.

@private

### Constants

INFINITY

### Attributes

max_non_kw_args[R]
min_non_kw_args[R]
optional_kw_args[R]
required_kw_args[R]

### Public Class Methods

new(method) click to toggle source

    # File lib/rspec/support/method_signature_verifier.rb, line 14
    def initialize(method)
      @method           = method
      @optional_kw_args = []
      @required_kw_args = []
      classify_parameters
    end

### Public Instance Methods

classify_parameters() click to toggle source

    # File lib/rspec/support/method_signature_verifier.rb, line 77
    def classify_parameters
      optional_non_kw_args = @min_non_kw_args = 0
      @allows_any_kw_args = false

      @method.parameters.each do |(type, name)|
        case type
        # def foo(a:)
        when :keyreq  then @required_kw_args << name
        # def foo(a: 1)
        when :key     then @optional_kw_args << name
        # def foo(**kw_args)
        when :keyrest then @allows_any_kw_args = true
        # def foo(a)
        when :req     then @min_non_kw_args += 1
        # def foo(a = 1)
        when :opt     then optional_non_kw_args += 1
        # def foo(*a)
        when :rest    then optional_non_kw_args = INFINITY
        end
      end

      @max_non_kw_args = @min_non_kw_args  + optional_non_kw_args
      @allowed_kw_args = @required_kw_args + @optional_kw_args
    end

could_contain_kw_args?(args) click to toggle source

Without considering what the last arg is, could it contain keyword arguments?

    # File lib/rspec/support/method_signature_verifier.rb, line 72
    def could_contain_kw_args?(args)
      return false if args.count <= min_non_kw_args
      @allows_any_kw_args || @allowed_kw_args.any?
    end

description() click to toggle source

    # File lib/rspec/support/method_signature_verifier.rb, line 35
    def description
      @description ||= begin
        parts = []

        unless non_kw_args_arity_description == "0"
          parts << "arity of #{non_kw_args_arity_description}"
        end

        if @optional_kw_args.any?
          parts << "optional keyword args (#{@optional_kw_args.map(&:inspect).join(", ")})"
        end

        if @required_kw_args.any?
          parts << "required keyword args (#{@required_kw_args.map(&:inspect).join(", ")})"
        end

        parts << "any additional keyword args" if @allows_any_kw_args

        parts.join(" and ")
      end
    end

has_kw_args_in?(args) click to toggle source

    # File lib/rspec/support/method_signature_verifier.rb, line 66
    def has_kw_args_in?(args)
      Hash === args.last && could_contain_kw_args?(args)
    end

invalid_kw_args_from(given_kw_args) click to toggle source

    # File lib/rspec/support/method_signature_verifier.rb, line 61
    def invalid_kw_args_from(given_kw_args)
      return [] if @allows_any_kw_args
      given_kw_args - @allowed_kw_args
    end

missing_kw_args_from(given_kw_args) click to toggle source

    # File lib/rspec/support/method_signature_verifier.rb, line 57
    def missing_kw_args_from(given_kw_args)
      @required_kw_args - given_kw_args
    end

non_kw_args_arity_description() click to toggle source

    # File lib/rspec/support/method_signature_verifier.rb, line 21
    def non_kw_args_arity_description
      case max_non_kw_args
      when min_non_kw_args then min_non_kw_args.to_s
      when INFINITY then "#{min_non_kw_args} or more"
      else "#{min_non_kw_args} to #{max_non_kw_args}"
      end
    end

valid_non_kw_args?(positional_arg_count) click to toggle source

    # File lib/rspec/support/method_signature_verifier.rb, line 29
    def valid_non_kw_args?(positional_arg_count)
      min_non_kw_args <= positional_arg_count &&
        positional_arg_count <= max_non_kw_args
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.