Class: Matchi::BeAnInstanceOf

Inherits:
Object
  • Object
show all
Defined in:
lib/matchi/be_an_instance_of.rb

Overview

Type/class matcher.

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ BeAnInstanceOf

Initialize the matcher with (the name of) a class or module.

Examples:

require "matchi/be_an_instance_of"

Matchi::BeAnInstanceOf.new(String)

Parameters:

  • expected (#to_s)

    A (name of a) class or module.



14
15
16
# File 'lib/matchi/be_an_instance_of.rb', line 14

def initialize(expected)
  @expected = String(expected).to_sym
end

Instance Method Details

#inspectObject

A string containing a human-readable representation of the matcher.



35
36
37
# File 'lib/matchi/be_an_instance_of.rb', line 35

def inspect
  "#{self.class}(#{@expected})"
end

#matches?Boolean

Boolean comparison between the class of the actual value and the expected class.

Examples:

require "matchi/be_an_instance_of"

matcher = Matchi::BeAnInstanceOf.new(String)
matcher.matches? { "foo" } # => true

Yield Returns:

  • (#class)

    the actual value to compare to the expected one.

Returns:

  • (Boolean)

    Comparison between actual and expected values.



30
31
32
# File 'lib/matchi/be_an_instance_of.rb', line 30

def matches?(*, **)
  self.class.const_get(@expected).equal?(yield.class)
end

#to_sObject

Returns a string representing the matcher.



40
41
42
# File 'lib/matchi/be_an_instance_of.rb', line 40

def to_s
  "be an instance of #{@expected}"
end