Sha256: 7af6e75dcf4e35ef73c2b48eda344ed2850fb501fab4b5f48deaab9053546d63

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Matchi
  # Common matcher methods.
  module MatchersBase
    # Abstract matcher class.
    #
    # @raise [NotImplementedError] Override me inside a Matcher subclass please.
    def matches?
      raise NotImplementedError, 'The matcher MUST respond to matches? method.'
    end

    # Returns a string representing the matcher.
    #
    # @example The readable definition of a FooBar matcher.
    #   matcher = Matchi::Matchers::FooBar::Matcher.new(42)
    #   matcher.to_s # => "foo_bar 42"
    #
    # @return [String] A string representing the matcher.
    def to_s
      s = matcher_name
          .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
          .gsub(/([a-z\d])([A-Z])/, '\1_\2')
          .downcase

      defined?(@expected) ? [s, @expected.inspect].join(' ') : s
    end

    # Returns a hash of one key-value pair with a key corresponding to the
    #   matcher and a value corresponding to its initialize parameters.
    #
    # @example A FooBar matcher serialized into a hash.
    #   matcher = Matchi::Matchers::FooBar::Matcher.new(42)
    #   matcher.to_h # => { FooBar: 42 }
    #
    # @return [Hash] A hash of one key-value pair.
    def to_h
      { matcher_name.to_sym => (defined?(@expected) ? Array(@expected) : []) }
    end

    private

    def matcher_name
      self
        .class
        .name
        .gsub(/\AMatchi::Matchers::/, '')
        .gsub(/::Matcher\z/, '')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
matchi-1.0.4 lib/matchi/matchers_base.rb