Class: RSpec::Support::MethodSignature Private
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
RSpec::Support::BlockSignature
|
|
Inherits: | Object |
Defined in: | rspec-support/lib/rspec/support/method_signature_verifier.rb, rspec-support/lib/rspec/support/method_signature_verifier.rb, rspec-support/lib/rspec/support/method_signature_verifier.rb |
Overview
Extracts info about the number of arguments and allowed/required keyword args of a given method.
Constant Summary
Class Method Summary
- .new(method) ⇒ MethodSignature constructor Internal use only
Instance Attribute Summary
-
#arbitrary_kw_args?
readonly
See additional method definition at line 98.
- #max_non_kw_args readonly Internal use only
- #min_non_kw_args readonly Internal use only
- #optional_kw_args readonly Internal use only
- #required_kw_args readonly Internal use only
-
#unlimited_args?
readonly
See additional method definition at line 102.
Instance Method Summary
- #classify_arity(arity = @method.arity) (also: #classify_parameters) Internal use only
-
#could_contain_kw_args? ⇒ Boolean
Internal use only
Without considering what the last arg is, could it contain keyword arguments?
-
#description
See additional method definition at line 51.
-
#has_kw_args_in?(_args) ⇒ Boolean
Internal use only
If the last argument is Hash, Ruby will treat only symbol keys as keyword arguments the rest will be grouped in another Hash and passed as positional argument.
-
#invalid_kw_args_from(_given_kw_args)
See additional method definition at line 77.
-
#missing_kw_args_from(_given_kw_args)
See additional method definition at line 73.
- #non_kw_args_arity_description Internal use only
- #valid_non_kw_args?(positional_arg_count, optional_max_arg_count = positional_arg_count) ⇒ Boolean Internal use only
-
#classify_parameters
private
Alias for #classify_arity.
Instance Attribute Details
#arbitrary_kw_args? (readonly)
See additional method definition at line 98.
# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 151
def arbitrary_kw_args? @allows_any_kw_args end
#max_non_kw_args (readonly)
[ GitHub ]# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 14
attr_reader :min_non_kw_args, :max_non_kw_args, :optional_kw_args, :required_kw_args
#min_non_kw_args (readonly)
[ GitHub ]# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 14
attr_reader :min_non_kw_args, :max_non_kw_args, :optional_kw_args, :required_kw_args
#optional_kw_args (readonly)
[ GitHub ]# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 14
attr_reader :min_non_kw_args, :max_non_kw_args, :optional_kw_args, :required_kw_args
#required_kw_args (readonly)
[ GitHub ]# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 14
attr_reader :min_non_kw_args, :max_non_kw_args, :optional_kw_args, :required_kw_args
#unlimited_args? (readonly)
See additional method definition at line 102.
# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 155
def unlimited_args? @max_non_kw_args == INFINITY end
Instance Method Details
#classify_arity(arity = @method.arity) Also known as: #classify_parameters
[ GitHub ]# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 38
def classify_arity(arity=@method.arity) if arity < 0 # `~` inverts the one's complement and gives us the # number of required args @min_non_kw_args = ~arity @max_non_kw_args = INFINITY else @min_non_kw_args = arity @max_non_kw_args = arity end end
#classify_parameters (private)
Alias for #classify_arity.
See additional method definition at line 106.
See additional method definition at line 174.
# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 198
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? ⇒ Boolean
Without considering what the last arg is, could it contain keyword arguments?
See additional method definition at line 92.
# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 147
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
See additional method definition at line 51.
# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 131
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) ⇒ Boolean
If the last argument is Hash, Ruby will treat only symbol keys as keyword arguments the rest will be grouped in another Hash and passed as positional argument.
See additional method definition at line 84.
# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 143
def has_kw_args_in?(args) Hash === args.last && could_contain_kw_args?(args) && (RubyFeatures.kw_arg_separation? || args.last.empty? || args.last.keys.any? { |x| x.is_a?(Symbol) }) end
#invalid_kw_args_from(_given_kw_args)
See additional method definition at line 77.
# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 139
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)
See additional method definition at line 73.
# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 135
def missing_kw_args_from(given_kw_args) @required_kw_args - given_kw_args end
#non_kw_args_arity_description
[ GitHub ]# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 23
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, optional_max_arg_count = positional_arg_count) ⇒ Boolean
# File 'rspec-support/lib/rspec/support/method_signature_verifier.rb', line 31
def valid_non_kw_args?(positional_arg_count, optional_max_arg_count=positional_arg_count) return true if positional_arg_count.nil? min_non_kw_args <= positional_arg_count && optional_max_arg_count <= max_non_kw_args end