Sha256: 32b5af1a0067e1172454c742c309a82dadf4c0fe7ea6a6167193247b5b7dfa23

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Matchi
  # *Equivalence* matcher.
  class Eq
    # 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.match? { "foo" } # => true
    #
    # @yieldreturn [#object_id] The actual value to compare to the expected
    #   one.
    #
    # @return [Boolean] Comparison between actual and expected values.
    def match?
      raise ::ArgumentError, "a block must be provided" unless block_given?

      @expected.eql?(yield)
    end

    # Returns a string representing the matcher.
    #
    # @return [String] a human-readable description of the matcher
    def to_s
      "eq #{@expected.inspect}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
matchi-4.1.1 lib/matchi/eq.rb
matchi-4.1.0 lib/matchi/eq.rb
matchi-4.0.0 lib/matchi/eq.rb