Sha256: 5194403bcfbdfee67602d710078e82a0625e19acffe1f28a9c4a9cd0f374a777

Contents?: true

Size: 1.17 KB

Versions: 6

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module Matchi
  # *Regular expressions* matcher.
  class Match
    # @return [#match] A regular expression.
    attr_reader :expected

    # 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)
      @expected = expected
    end

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

Version data entries

6 entries across 6 versions & 1 rubygems

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