Sha256: e98b0b31ef98d147985d7985bf0daa8cb5731b0eb9203326f8c6c40159d337ad

Contents?: true

Size: 1.61 KB

Versions: 51

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 #{_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.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

51 entries across 51 versions & 16 rubygems

Version Path
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/match_array.rb
vagrant-unbundled-1.9.5.1 vendor/bundle/ruby/2.4.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/match_array.rb
vagrant-unbundled-1.9.1.1 vendor/bundle/ruby/2.4.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/match_array.rb
vagrant-unbundled-1.8.5.2 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/match_array.rb
vagrant-unbundled-1.8.5.1 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/match_array.rb
vagrant-unbundled-1.8.4.2 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/match_array.rb
vagrant-unbundled-1.8.4.1 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/match_array.rb
vagrant-unbundled-1.8.1.1 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/match_array.rb
opal-rspec-cj-0.4.4 vendor_lib/rspec/matchers/built_in/match_array.rb
opal-rspec-0.4.3 vendor_lib/rspec/matchers/built_in/match_array.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/match_array.rb
opal-rspec-0.4.2 vendor_lib/rspec/matchers/built_in/match_array.rb
opal-rspec-0.4.1 vendor_lib/rspec/matchers/built_in/match_array.rb
opal-rspec-0.4.0 vendor_lib/rspec/matchers/built_in/match_array.rb
opal-rspec-0.4.0.beta4 vendor_lib/rspec/matchers/built_in/match_array.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/rspec-expectations-2.13.0/lib/rspec/matchers/built_in/match_array.rb
dxruby_rp5-0.0.4 spec/vendor/rspec-expectations-2.14.4/lib/rspec/matchers/built_in/match_array.rb
rspec-expectations-2.99.0.beta2 lib/rspec/matchers/built_in/match_array.rb
rspec-expectations-2.14.5 lib/rspec/matchers/built_in/match_array.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/rspec-expectations-2.14.4/lib/rspec/matchers/built_in/match_array.rb