Sha256: 19c098fd2bc03fce44cf9cac3aa01ae424b3700b1f8a8cb7e722bea625506aac

Contents?: true

Size: 1.14 KB

Versions: 6

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Matchi
  # *Equivalence* matcher.
  class Eq
    # @return [#eql?] An expected equivalent object.
    attr_reader :expected

    # Initialize the matcher with an object.
    #
    # @example
    #   require "matchi/eq"
    #
    #   Matchi::Eq.new("foo")
    #
    # @param expected [#eql?] An expected equivalent object.
    def initialize(expected)
      @expected = expected
    end

    # Boolean comparison between the actual value and the expected value.
    #
    # @example
    #   require "matchi/eq"
    #
    #   matcher = Matchi::Eq.new("foo")
    #
    #   matcher.expected           # => "foo"
    #   matcher.matches? { "foo" } # => true
    #
    # @yieldreturn [#object_id] The actual value to compare to the expected
    #   one.
    #
    # @return [Boolean] Comparison between actual and expected values.
    def matches?
      expected.eql?(yield)
    end

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

    # Returns a string representing the matcher.
    def to_s
      "eq #{expected.inspect}"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

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