Sha256: 076eb79d6f41de7a6d8f7dfa7a65e308528e739463aa1069981051b66928564f

Contents?: true

Size: 1.83 KB

Versions: 9

Compression:

Stored size: 1.83 KB

Contents

module Remarkable
  # This class holds the basic structure for Remarkable matchers. All matchers
  # must inherit from it.
  class Base
    include Remarkable::Messages
    extend  Remarkable::DSL

    # Optional to provide spec binding to matchers.
    def spec(binding)
      @spec = binding
      self
    end

    def positive?
      !negative?
    end

    def negative?
      false
    end

    protected

      # Returns the subject class unless it's a class object.
      def subject_class
        nil unless @subject
        @subject.is_a?(Class) ? @subject : @subject.class
      end

      # Returns the subject name based on its class.
      def subject_name
        nil unless @subject
        subject_class.name
      end

      # Iterates over the collection given yielding the block and return false
      # if any of them also returns false.
      def assert_collection(key, collection, inspect=true) #:nodoc:
        collection.each do |item|
          value = yield(item)

          if positive? == !value
            if key
              output = inspect ? item.inspect : item
              return negative?, key => output
            else
              return negative?
            end
          end
        end
        positive?
      end

      # Asserts that the given collection contains item x. If x is a regular
      # expression, ensure that at least one element from the collection matches x.
      #
      #   assert_contains(['a', '1'], /\d/) => passes
      #   assert_contains(['a', '1'], 'a') => passes
      #   assert_contains(['a', '1'], /not there/) => fails
      #
      def assert_contains(collection, x)
        collection = [collection] unless collection.is_a?(Array)

        case x
          when Regexp
            collection.detect { |e| e =~ x }
          else
            collection.include?(x)
        end
      end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
remarkable-4.0.0.alpha4 lib/remarkable/core/base.rb
remarkable-4.0.0.alpha3 lib/remarkable/core/base.rb
remarkable-4.0.0.alpha2 lib/remarkable/core/base.rb
remarkable-4.0.0.alpha1 lib/remarkable/base.rb
remarkable-3.1.13 lib/remarkable/base.rb
remarkable-3.1.12 lib/remarkable/base.rb
remarkable-3.1.11 lib/remarkable/base.rb
remarkable-3.1.10 lib/remarkable/base.rb
remarkable-3.1.9 lib/remarkable/base.rb