Skip to content
This repository
David Chelimsky April 06, 2009
file 72 lines (59 sloc) 2.242 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
module Spec
  module Matchers

    class MatchArray #:nodoc:
      include Spec::Matchers::Pretty
      
      def initialize(expected)
        @expected = expected
      end

      def matches?(actual)
        @actual = actual
        @extra_items = difference_between_arrays(@actual, @expected)
        @missing_items = difference_between_arrays(@expected, @actual)
        @extra_items.empty? & @missing_items.empty?
      end

      def failure_message_for_should
        message = "expected collection contained: #{safe_sort(@expected).inspect}\n"
        message += "actual collection contained: #{safe_sort(@actual).inspect}\n"
        message += "the missing elements were: #{safe_sort(@missing_items).inspect}\n" unless @missing_items.empty?
        message += "the extra elements were: #{safe_sort(@extra_items).inspect}\n" unless @extra_items.empty?
        message
      end
      
      def failure_message_for_should_not
        "Matcher does not support should_not"
      end
      
      def description
        "contain exactly #{_pretty_print(@expected)}"
      end

      private

        def safe_sort(array)
          array.all?{|item| item.respond_to?(:<=>)} ? array.sort : array
        end

        def difference_between_arrays(array_1, array_2)
          difference = array_1.dup
          array_2.each do |element|
            if index = difference.index(element)
              difference.delete_at(index)
            end
          end
          difference
        end


    end

    # :call-seq:
    # should =~ expected
    #
    # Passes if actual contains all of the expected regardless of order.
    # This works for collections. Pass in multiple args and it will only
    # pass if all args are found in collection.
    #
    # NOTE: there is no should_not version of array.should =~ other_array
    #
    # == Examples
    #
    # [1,2,3].should =~ [1,2,3] # => would pass
    # [1,2,3].should =~ [2,3,1] # => would pass
    # [1,2,3,4].should =~ [1,2,3] # => would fail
    # [1,2,2,3].should =~ [1,2,3] # => would fail
    # [1,2,3].should =~ [1,2,3,4] # => would fail
    OperatorMatcher.register(Array, '=~', Spec::Matchers::MatchArray)
  end
end
Something went wrong with that request. Please try again.