Sha256: 72e047e52c12a4961c426fed3a42897ecb30be1837bba192be764e81fab13737

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

RSpec::Matchers.define :have_method do |method_name|
  match do |object|
    @method_name = method_name
    @arity ||= '0'.to_i
    if %r{^(?<prefix>\.|\#)(?<normalized_method_name>.*)} =~ method_name.to_s
      @normalized_method_name = normalized_method_name
      @klass = object.is_a?(Class) ? object : object.class
      if prefix == '.'
        @klass.method(@normalized_method_name).arity == @arity
      elsif prefix == '#'
        @klass.instance_method(@normalized_method_name).arity == @arity
      end
    else
      @method_name = "##{method_name}"
      @klass = object.class
      @normalized_method_name = method_name
      @object = object
      @object.method(@normalized_method_name).arity == @arity
    end
  end

  description do
    "have_method #{@method_name} [arity:#{@arity}]"
  end

  failure_message_for_should do |text|
    "expected #{@klass} to have_method #{@method_name} [arity:#{@arity}]"
  end

  failure_message_for_should_not do |text|
    "expected #{@klass} not to delegate #{@method_name} [arity:#{@arity}]"
  end

  chain(:with_arity) { |arity| @arity = arity }
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ndlib-on-rspec-0.0.2 lib/ndlib-on-rspec/have_method_matcher.rb