Sha256: 7d543359fd54b042334273b084cea3453036e6aac2437719b6ed2bc2278a178d

Contents?: true

Size: 1.38 KB

Versions: 10

Compression:

Stored size: 1.38 KB

Contents

module RSpec
  module Matchers
    module BuiltIn
      class MatchArray < BaseMatcher
        def match(expected, 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.sort rescue 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
    end
  end
end

Version data entries

10 entries across 10 versions & 5 rubygems

Version Path
tnargav-1.3.3 vendor/bundle/ruby/1.9.1/gems/rspec-expectations-2.11.3/lib/rspec/matchers/built_in/match_array.rb
tnargav-1.2.3 vendor/bundle/ruby/1.9.1/gems/rspec-expectations-2.11.3/lib/rspec/matchers/built_in/match_array.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/rspec-expectations-2.11.3/lib/rspec/matchers/built_in/match_array.rb
gem_repackager-0.1.0 support/gems/rspec-expectations-2.11.2/lib/rspec/matchers/built_in/match_array.rb
librarian-puppet-0.9.4 vendor/gems/ruby/1.8/gems/rspec-expectations-2.11.2/lib/rspec/matchers/built_in/match_array.rb
rspec-expectations-2.11.3 lib/rspec/matchers/built_in/match_array.rb
librarian-puppet-0.9.3 vendor/gems/ruby/1.8/gems/rspec-expectations-2.11.2/lib/rspec/matchers/built_in/match_array.rb
rspec-expectations-2.11.2 lib/rspec/matchers/built_in/match_array.rb
rspec-expectations-2.11.1 lib/rspec/matchers/built_in/match_array.rb
rspec-expectations-2.11.0 lib/rspec/matchers/built_in/match_array.rb