Sha256: 61737692cac47afdf035b4df1e2ac367d83601c2e98f6fb6776e4530af2e6343

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Matchi
  # *Type/class* matcher.
  class BeAnInstanceOf
    # @return [Symbol] The expected class name.
    attr_reader :expected

    # Initialize the matcher with (the name of) a class or module.
    #
    # @example
    #   require "matchi/be_an_instance_of"
    #
    #   Matchi::BeAnInstanceOf.new(String)
    #
    # @param expected [Class, #to_s] The expected class name.
    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.expected           # => :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.1.0 lib/matchi/be_an_instance_of.rb