Sha256: 9f134e533b968c46416b1b5f4da7c4224267b833f125ac458d059f7306271528

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

module RSpec
  module Matchers
    module BuiltIn
      class MatchArray < BaseMatcher
        def match(expected, actual)
          return false unless actual.respond_to? :to_ary
          @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
          if actual.respond_to? :to_ary
            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?
          else
            message = "expected an array, actual collection was #{actual.inspect}"
          end

          message
        end

        def failure_message_for_should_not
          "Matcher does not support should_not"
        end

        def description
          "contain exactly#{to_sentence(expected)}"
        end

        private

        def safe_sort(array)
          array.sort rescue array
        end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rspec-expectations-2.99.2 lib/rspec/matchers/built_in/match_array.rb
rspec-expectations-2.99.1 lib/rspec/matchers/built_in/match_array.rb
rspec-expectations-2.99.0 lib/rspec/matchers/built_in/match_array.rb
rspec-expectations-2.99.0.rc1 lib/rspec/matchers/built_in/match_array.rb