Class: Sinclair::Matchers::AddMethod

Inherits:
RSpec::Matchers::BuiltIn::BaseMatcher
  • Object
show all
Defined in:
lib/sinclair/matchers/add_method.rb

Overview

AddMethod is able to build an instance of Sinclair::Matchers::AddMethodTo

Author:

  • darthjee

Instance Method Summary collapse

Constructor Details

#initialize(method) ⇒ AddMethod

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of AddMethod

Parameters:

  • method (String, Symbol)

    the method, to be checked, name



24
25
26
# File 'lib/sinclair/matchers/add_method.rb', line 24

def initialize(method)
  @method = method
end

Instance Method Details

#equal?(other) ⇒ Boolean Also known as: ==

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checkes if another instnce is equal self

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/sinclair/matchers/add_method.rb', line 93

def equal?(other)
  return unless other.class == self.class
  other.method == method
end

#matches?(_actual) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method is abstract.

Raise a warning on the usage as this is only a builder for AddMethodTo

Returns:

  • (Boolean)

Raises:

  • SyntaxError



14
15
16
17
# File 'lib/sinclair/matchers/add_method.rb', line 14

def matches?(_actual)
  raise SyntaxError, 'You should specify which instance the method is being added to' \
    "add_method(:#{method}).to(instance)"
end

#supports_block_expectations?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

definition needed for block matchers

Returns:

  • (Boolean)


84
85
86
# File 'lib/sinclair/matchers/add_method.rb', line 84

def supports_block_expectations?
  true
end

#to(klass) ⇒ AddMethodTo #to(instance) ⇒ AddMethodTo

Creates a matcher AddMethodTo

Examples:

Using inside RSpec and checking Class

RSpec.describe "MyBuilder" do
  let(:clazz)   { Class.new }
  let(:builder) { Sinclair.new(clazz) }

  before do
    builder.add_method(:new_method, "2")
  end

  it do
    expect { builder.build }.to add_method(:new_method).to(clazz)
  end
end

# Outputs
# 'should add method 'new_method' to #<Class:0x000056441bf46608> instances'

Using inside RSpec and checking instance

RSpec.describe "MyBuilder" do
  let(:clazz)    { Class.new }
  let(:builder)  { Sinclair.new(clazz) }
  let(:instance) { clazz.new }

  before do
    builder.add_method(:the_method, "true")
  end

  it do
    expect { builder.build }.to add_method(:the_method).to(instance)
  end
end

# Outputs
# 'should add method 'the_method' to #<Class:0x000056441bf46608> instances'

Overloads:

  • #to(klass) ⇒ AddMethodTo

    Parameters:

    • klass (Class)

      class where the method should be added to

  • #to(instance) ⇒ AddMethodTo

    Parameters:

    • instance (Object)

      instance of the class where the method should be added to

Returns:



75
76
77
# File 'lib/sinclair/matchers/add_method.rb', line 75

def to(target = nil)
  AddMethodTo.new(target, method)
end