Sha256: 9bb71479b2d44ae56538f6862197588edeb0d769084a98a5f46e5c9b579713c9

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Matchi
  # *Identity* matcher.
  class Be
    # Initialize the matcher with an object.
    #
    # @example
    #   require "matchi/be"
    #
    #   Matchi::Be.new(:foo)
    #
    # @param expected [#equal?] The expected identical object.
    def initialize(expected)
      @expected = expected
    end

    # Boolean comparison between the actual value and the expected value.
    #
    # @example
    #   require "matchi/be"
    #
    #   matcher = Matchi::Be.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.equal?(yield)
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

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