Sha256: 03ee777fddcb29584e70bdde31474cb41eb64a187882c0f17c25436572666c9c

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

class Bogus::VerifiesStubDefinition
  extend Bogus::Takes

  takes :method_stringifier

  def verify!(object, method_name, args)
    stubbing_non_existent_method!(object, method_name) unless object.respond_to?(method_name)
    return unless object.methods.include?(method_name)
    method = object.method(method_name)
    wrong_number_of_arguments!(method, args) if under_number_of_required_arguments?(method, args.size)
    wrong_number_of_arguments!(method, args) if over_number_of_allowed_arguments?(method, args.size)
  end

  private

  def wrong_number_of_arguments!(method, args)
    args_string = method_stringifier.arguments_as_string(method.parameters)
    raise ArgumentError, "tried to stub #{method.name}(#{args_string}) with #{args.size} arguments"
  end

  def stubbing_non_existent_method!(object, method_name)
    raise NameError, "#{object.inspect} does not respond to #{method_name}"
  end

  def under_number_of_required_arguments?(method, args_count)
    number_of_arguments = method.arity
    number_of_arguments = -number_of_arguments - 1 if number_of_arguments < 0

    args_count < number_of_arguments
  end

  def over_number_of_allowed_arguments?(method, args_count)
    return false if method.parameters.find{|type, name| type == :rest}
    number_of_arguments = method.parameters.count{|type, name| [:opt, :req].include?(type) }

    args_count > number_of_arguments
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bogus-0.0.2 lib/bogus/verifies_stub_definition.rb