Sha256: 9f8842f7fffe9f7eb635e6b369f1abfb16672b6e6e4919c67562bf7227c9af4f

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Matchi
  # *Type/class* matcher.
  class BeAnInstanceOf
    # @return [String] 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)
    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

5 entries across 5 versions & 1 rubygems

Version Path
matchi-3.3.2 lib/matchi/be_an_instance_of.rb
matchi-3.3.1 lib/matchi/be_an_instance_of.rb
matchi-3.3.0 lib/matchi/be_an_instance_of.rb
matchi-3.2.0 lib/matchi/be_an_instance_of.rb
matchi-3.1.1 lib/matchi/be_an_instance_of.rb