Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 79 lines (69 sloc) 2.363 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 72 73 74 75 76 77 78
module Spec
  module Matchers

    class ContainExactly #:nodoc:

      def initialize(*expecteds)
        @expecteds = expecteds
        @offending_objects = []
      end

      def matches?(given)
        @given = given
        @extra_items = @given.reject do |object|
          @expecteds.include?(object)
        end
        @missing_items = @expecteds.reject do |object|
          @given.include?(object)
        end
        @extra_items.empty? and @missing_items.empty?
      end

        def failure_message
          _message
        end
        
      # def negative_failure_message
      # _message("not ")
      # end
        
        def description
          "include #{_pretty_print(@expecteds)}"
        end
        
        private
          def _message(maybe_not="")
            message = "expected collection contained: #{@expecteds.sort.inspect}\n"
            message += "actual collection contained: #{@given.sort.inspect}\n"
            message += "the missing elements were: #{@missing_items.sort.inspect}\n" unless @missing_items.empty?
            message += "the extra elements were: #{@extra_items.sort.inspect}\n" unless @extra_items.empty?
            message
          end
          
      # def _pretty_print(array)
      # result = ""
      # array.each_with_index do |item, index|
      # if index < (array.length - 2)
      # result << "#{item.inspect}, "
      # elsif index < (array.length - 1)
      # result << "#{item.inspect} and "
      # else
      # result << "#{item.inspect}"
      # end
      # end
      # result
      # end
    end

    # :call-seq:
    # should include(expected)
    # should_not include(expected)
    #
    # Passes if given includes expected. This works for
    # collections and Strings. You can also pass in multiple args
    # and it will only pass if all args are found in collection.
    #
    # == Examples
    #
    # [1,2,3].should include(3)
    # [1,2,3].should include(2,3) #would pass
    # [1,2,3].should include(2,3,4) #would fail
    # [1,2,3].should_not include(4)
    # "spread".should include("read")
    # "spread".should_not include("red")
    def contain_exactly(*expected)
      Matchers::ContainExactly.new(*expected)
    end
  end
end
Something went wrong with that request. Please try again.