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

#inspect, #to_s, to_sym

Constructor Details

#initialize(expected) ⇒ BeAnInstanceOf

Initialize the matcher with an object.

Examples:

A string matcher

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

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 = self.class.const_get String(expected)
end

Instance Method Details

#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.



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

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