Sha256: 2bb8e0837cfbbfa7ee7805d8b6a6b77181bac916c58793a837c6ae6e0e82b8ee

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module ElabsMatchers
  module Matchers
    module OnlyInclude
      rspec

      class OnlyIncludeMatcher < Struct.new(:elements)
        attr_reader :actual

        def matches?(actual)
          @actual = actual
          elements.uniq.length == elements.length and
          actual.length == elements.length and
          elements.all? { |element| actual.include?(element) }
        end

        def failure_message
          "Expected #{actual.inspect} to only include #{elements.inspect}."
        end
        alias_method :failure_message_for_should, :failure_message

        def failure_message_when_negated
          "Expected #{actual.inspect} to not only include #{elements.inspect}, but it did."
        end
        alias_method :failure_message_for_should_not, :failure_message_when_negated
      end

      ##
      #
      # Asserts if the array contains exactly the supplied elements.
      # The order of the element do not have to match.
      #
      # @param [*Array] elements      Comma seperated list of arguments
      #
      # Example:
      # ["foo", "bar"].should only_include("bar", "foo")
      # ["foo", "bar"].should_not only_include("foo")

      def only_include(*elements)
        OnlyIncludeMatcher.new(elements)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
elabs_matchers-2.0.0 lib/elabs_matchers/matchers/only_include.rb
elabs_matchers-1.0.1 lib/elabs_matchers/matchers/only_include.rb
elabs_matchers-1.0.0 lib/elabs_matchers/matchers/only_include.rb