Sha256: d4c630129dcd1aa096429f3971488758bb298f480172ad1ee671dd438e47d1e9

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Matchi
  # *Type/class* matcher.
  class BeAnInstanceOf
    # Initialize the matcher with (the name of) a class or module.
    #
    # @example
    #   require "matchi/be_an_instance_of"
    #
    #   Matchi::BeAnInstanceOf.new(String)
    #
    # @param expected [#to_s] A (name of a) class or module.
    def initialize(expected)
      @expected = String(expected).to_sym
    end

    # Boolean comparison between the class of the actual value and the
    # expected class.
    #
    # @example
    #   require "matchi/be_an_instance_of"
    #
    #   matcher = Matchi::BeAnInstanceOf.new(String)
    #   matcher.matches? { "foo" } # => true
    #
    # @yieldreturn [#class] the actual value to compare to the expected one.
    #
    # @return [Boolean] Comparison between actual and expected values.
    def matches?(*, **)
      self.class.const_get(@expected).equal?(yield.class)
    end

    # A string containing a human-readable representation of the matcher.
    def inspect
      "#{self.class}(#{@expected})"
    end

    # Returns a string representing the matcher.
    def to_s
      "be an instance of #{@expected}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
matchi-3.0.0 lib/matchi/be_an_instance_of.rb