Sha256: b1ac0d33a4aa7be2eefa870fa53db8d2111a86f476565ef9251ff67e3bbc42c9

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 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)
    return if any_args?(args)
    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

  def any_args?(args)
    [Bogus::AnyArgs] == args
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bogus-0.0.3 lib/bogus/verifies_stub_definition.rb
bogus-0.0.3.rc.2 lib/bogus/verifies_stub_definition.rb
bogus-0.0.3.rc.1 lib/bogus/verifies_stub_definition.rb