Class: Matchi::Matcher::BeAnInstanceOf

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

Overview

Type/class matcher.

Instance Attribute Summary

Attributes inherited from Base

#expected

Instance Method Summary collapse

Methods inherited from Base

to_sym

Constructor Details

#initialize(expected) ⇒ BeAnInstanceOf

Initialize the matcher with an object.

Examples:

A duck matcher

Matchi::Matcher::BeAnInstanceOf.new(:Duck)

Parameters:

  • expected (#to_s)

    The name of a module.



15
16
17
18
# File 'lib/matchi/matcher/be_an_instance_of.rb', line 15

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

Instance Method Details

#inspectString

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

Examples:

The human-readable representation of a FooBar matcher instance.

matcher = Matchi::Matcher::FooBar.new(42)
matcher.inspect # => "Matchi::Matcher::FooBar(42)"

Returns:

  • (String)

    The human-readable representation of the matcher.



21
22
23
# File 'lib/matchi/matcher/be_an_instance_of.rb', line 21

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

#matches?Boolean

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

Examples:

Is it an instance of string?

be_an_instance_of = Matchi::Matcher::BeInstanceOf.new(String)
be_an_instance_of.matches? { "foo" } # => true

be_an_instance_of = Matchi::Matcher::BeInstanceOf.new(:String)
be_an_instance_of.matches? { "foo" } # => true

be_an_instance_of = Matchi::Matcher::BeInstanceOf.new("String")
be_an_instance_of.matches? { "foo" } # => true

Yield Returns:

  • (#class)

    the actual value to compare to the expected one.

Returns:

  • (Boolean)

    Comparison between actual and expected values.



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

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

#to_sString

Returns a string representing the matcher instance.

Examples:

The readable definition of a FooBar matcher instance.

matcher = Matchi::Matcher::FooBar.new(42)
matcher.to_s # => "foo_bar 42"

Returns:

  • (String)

    A string representing the matcher instance.



46
47
48
# File 'lib/matchi/matcher/be_an_instance_of.rb', line 46

def to_s
  "#{self.class.to_sym} #{expected}"
end