Class: RSpec::Support::MethodSignature
- Inherits:
-
Object
- Object
- RSpec::Support::MethodSignature
- Defined in:
- lib/rspec/support/method_signature_verifier.rb,
lib/rspec/support/method_signature_verifier.rb,
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
rubocop:disable Metrics/ClassLength.
-
#min_non_kw_args ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#optional_kw_args ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#required_kw_args ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
Instance Method Summary collapse
- #arbitrary_kw_args? ⇒ Boolean
- #classify_arity(arity = @method.arity) ⇒ Object (also: #classify_parameters)
-
#could_contain_kw_args? ⇒ Boolean
Without considering what the last arg is, could it contain keyword arguments?.
- #description ⇒ Object
-
#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.
-
#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
- #unlimited_args? ⇒ Boolean
- #valid_non_kw_args?(positional_arg_count, optional_max_arg_count = positional_arg_count) ⇒ Boolean
Constructor Details
#initialize(method) ⇒ MethodSignature
Returns a new instance of MethodSignature.
16 17 18 19 20 21 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 16 def initialize(method) @method = method @optional_kw_args = [] @required_kw_args = [] classify_parameters end |
Instance Attribute Details
#max_non_kw_args ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
14 15 16 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 14 def max_non_kw_args @max_non_kw_args end |
#min_non_kw_args ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
14 15 16 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 14 def min_non_kw_args @min_non_kw_args end |
#optional_kw_args ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
14 15 16 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 14 def optional_kw_args @optional_kw_args end |
#required_kw_args ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
14 15 16 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 14 def required_kw_args @required_kw_args end |
Instance Method Details
#arbitrary_kw_args? ⇒ Boolean
98 99 100 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 98 def arbitrary_kw_args? @allows_any_kw_args end |
#classify_arity(arity = @method.arity) ⇒ Object Also known as: classify_parameters
38 39 40 41 42 43 44 45 46 47 48 |
# File '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 |
#could_contain_kw_args? ⇒ Boolean
Without considering what the last arg is, could it contain keyword arguments?
92 93 94 95 96 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 92 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 51 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.
84 85 86 87 88 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 84 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) ⇒ Object
77 78 79 80 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 77 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
73 74 75 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 73 def missing_kw_args_from(given_kw_args) @required_kw_args - given_kw_args end |
#non_kw_args_arity_description ⇒ Object
23 24 25 26 27 28 29 |
# File '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 |
#unlimited_args? ⇒ Boolean
102 103 104 |
# File 'lib/rspec/support/method_signature_verifier.rb', line 102 def unlimited_args? @max_non_kw_args == INFINITY end |
#valid_non_kw_args?(positional_arg_count, optional_max_arg_count = positional_arg_count) ⇒ Boolean
31 32 33 34 35 36 |
# File '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 |