Sha256: 20e6c51679b09dfc360cadc2d81327f22b9137b55b72d60d7307bc86c4653962

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module Matchi
  # *Regular expressions* matcher.
  class Match
    # Initialize the matcher with an instance of Regexp.
    #
    # @example
    #   require "matchi/match"
    #
    #   Matchi::Match.new(/^foo$/)
    #
    # @param expected [#match] A regular expression.
    def initialize(expected)
      raise ::ArgumentError, "expected must respond to match?" unless expected.respond_to?(:match?)

      @expected = expected
    end

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

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