Sha256: 134658116e56608f72d9ff17322c4ec75d9860318c858613b61374c073edf693

Contents?: true

Size: 1.52 KB

Versions: 169

Compression:

Stored size: 1.52 KB

Contents

module RSpec
  module Matchers
    module BuiltIn
      class MatchArray
        include RSpec::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.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

169 entries across 71 versions & 9 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/rspec-expectations-2.9.1/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.5 vendor/bundle/gems/rspec-expectations-2.9.0/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.5 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.4 vendor/bundle/gems/rspec-expectations-2.9.0/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.4 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.4 vendor/bundle/gems/rspec-expectations-2.9.1/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.3 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.3 vendor/bundle/gems/rspec-expectations-2.9.1/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.3 vendor/bundle/gems/rspec-expectations-2.9.0/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.2 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.2 vendor/bundle/gems/rspec-expectations-2.9.0/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.2 vendor/bundle/gems/rspec-expectations-2.9.1/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.1 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.1 vendor/bundle/gems/rspec-expectations-2.9.1/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.1 vendor/bundle/gems/rspec-expectations-2.9.0/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.0 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.0 vendor/bundle/gems/rspec-expectations-2.9.0/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.7.0 vendor/bundle/gems/rspec-expectations-2.9.1/lib/rspec/matchers/built_in/match_array.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/match_array.rb
classiccms-0.6.9 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/match_array.rb