Class: RSpec::Support::MethodSignature
- Inherits:
-
Object
- Object
- RSpec::Support::MethodSignature
- Defined in:
- lib/rspec/support/method_signature_verifier.rb
Overview
Extracts info about the number of arguments and allowed/required keyword args of a given method.
Direct Known Subclasses
Constant Summary collapse
- INFINITY =
1 / 0.0
Instance Attribute Summary collapse
-
#max_non_kw_args ⇒ Object
readonly
Returns the value of attribute max_non_kw_args.
-
#min_non_kw_args ⇒ Object
readonly
Returns the value of attribute min_non_kw_args.
Instance Method Summary collapse
- #classify_parameters ⇒ Object
-
#could_contain_kw_args?(args) ⇒ Boolean
Without considering what the last arg is, could it contain keyword arguments?.
- #description ⇒ Object
- #has_kw_args_in?(_args) ⇒ Boolean
-
#initialize(method) ⇒ MethodSignature
constructor
A new instance of MethodSignature.
- #invalid_kw_args_from(_given_kw_args) ⇒ Object
- #missing_kw_args_from(_given_kw_args) ⇒ Object
- #non_kw_args_arity_description ⇒ Object
- #valid_non_kw_args?(positional_arg_count) ⇒ Boolean
Constructor Details
#initialize(method) ⇒ MethodSignature
Returns a new instance of MethodSignature.
14 15 16 17 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 14 def initialize(method) @method = method classify_parameters end |
Instance Attribute Details
#max_non_kw_args ⇒ Object (readonly)
Returns the value of attribute max_non_kw_args.
12 13 14 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 12 def max_non_kw_args @max_non_kw_args end |
#min_non_kw_args ⇒ Object (readonly)
Returns the value of attribute min_non_kw_args.
12 13 14 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 12 def min_non_kw_args @min_non_kw_args end |
Instance Method Details
#classify_parameters ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 75 def classify_parameters optional_non_kw_args = @min_non_kw_args = 0 @optional_kw_args, @required_kw_args = [], [] @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) ⇒ Boolean
Without considering what the last arg is, could it contain keyword arguments?
70 71 72 73 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 70 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 ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 33 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
64 65 66 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 64 def has_kw_args_in?(args) Hash === args.last && could_contain_kw_args?(args) end |
#invalid_kw_args_from(_given_kw_args) ⇒ Object
59 60 61 62 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 59 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) ⇒ Object
55 56 57 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 55 def missing_kw_args_from(given_kw_args) @required_kw_args - given_kw_args end |
#non_kw_args_arity_description ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 19 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) ⇒ Boolean
27 28 29 30 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 27 def valid_non_kw_args?(positional_arg_count) min_non_kw_args <= positional_arg_count && positional_arg_count <= max_non_kw_args end |