Sha256: f84b16ce5aeb78b8d7fd1e36cac76c1a330412871a1552f90c5fdfdc3201df7a

Contents?: true

Size: 1.29 KB

Versions: 17

Compression:

Stored size: 1.29 KB

Contents

require 'set'
require 'imw/utils'

module IMWTest
  module CustomMatchers

    # Match the contents of two arrays without regard to the order
    # of their elements by treating each as a set.
    class WithoutRegardToOrder

      private
      def initialize known_array
        @known_array = known_array.to_set
      end

      public
      def matches? array_to_test
        @array_to_test = array_to_test.to_set
        @array_to_test == @known_array
      end

      def failure_message
        missing_from_array_to_test = "missing from array to test: #{(@known_array - @array_to_test).to_a.quote_items_with "and"}\n"
        missing_from_known_array = "missing from known array: #{(@array_to_test - @known_array).to_a.quote_items_with "and"}\n"
        common_to_both = "common to both: #{(@array_to_test & @known_array).to_a.quote_items_with "and"}\n"
        "expected contents of the arrays to be identical:\n\n#{missing_from_array_to_test}\n#{missing_from_known_array}\n#{common_to_both}"
      end

      def negative_failure_message
        "expected contents of the arrays to differ."
      end
    end

    # Check that the contents of one array match another without
    # regard to ordering.
    def match_without_regard_to_order known_array
      WithoutRegardToOrder.new(known_array)
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
imw-0.2.18 spec/support/without_regard_to_order_matcher.rb
imw-0.2.17 spec/support/without_regard_to_order_matcher.rb
imw-0.2.16 spec/support/without_regard_to_order_matcher.rb
imw-0.2.15 spec/support/without_regard_to_order_matcher.rb
imw-0.2.14 spec/support/without_regard_to_order_matcher.rb
imw-0.2.13 spec/support/without_regard_to_order_matcher.rb
imw-0.2.12 spec/support/without_regard_to_order_matcher.rb
imw-0.2.11 spec/support/without_regard_to_order_matcher.rb
imw-0.2.10 spec/support/without_regard_to_order_matcher.rb
imw-0.2.9 spec/support/without_regard_to_order_matcher.rb
imw-0.2.8 spec/support/without_regard_to_order_matcher.rb
imw-0.2.7 spec/support/without_regard_to_order_matcher.rb
imw-0.2.6 spec/support/without_regard_to_order_matcher.rb
imw-0.2.5 spec/support/without_regard_to_order_matcher.rb
imw-0.2.4 spec/support/without_regard_to_order_matcher.rb
imw-0.2.3 spec/support/without_regard_to_order_matcher.rb
imw-0.2.2 spec/support/without_regard_to_order_matcher.rb