Class: Mocktail::RecreatesMessage

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mocktail/simulates_argument_error/recreates_message.rb,
lib/mocktail/sorbet/mocktail/simulates_argument_error/recreates_message.rb

Instance Method Summary collapse

Instance Method Details

#recreate(signature) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mocktail/simulates_argument_error/recreates_message.rb', line 5

def recreate(signature)
  req_args = signature.positional_params.required.size
  allowed_args = signature.positional_params.allowed.size
  rest_args = signature.positional_params.rest?
  req_kwargs = signature.keyword_params.required

  if signature.positional_args.size < req_args || (!rest_args && signature.positional_args.size > allowed_args)
    expected_desc = if rest_args
      "#{req_args}+"
    elsif allowed_args != req_args
      "#{req_args}..#{allowed_args}"
    else
      req_args.to_s
    end

    "wrong number of arguments (given #{signature.positional_args.size}, expected #{expected_desc}#{"; required keyword#{"s" if req_kwargs.size > 1}: #{req_kwargs.join(", ")}" unless req_kwargs.empty?})"

  elsif !(missing_kwargs = req_kwargs.reject { |name| signature.keyword_args.key?(name) }).empty?
    "missing keyword#{"s" if missing_kwargs.size > 1}: #{missing_kwargs.map { |name| name.inspect }.join(", ")}"
  elsif !(unknown_kwargs = signature.keyword_args.keys.reject { |name| signature.keyword_params.all.include?(name) }).empty?
    "unknown keyword#{"s" if unknown_kwargs.size > 1}: #{unknown_kwargs.map { |name| name.inspect }.join(", ")}"
  else
    "unknown cause (this is probably a bug in Mocktail)"
  end
end