summaryrefslogtreecommitdiff
path: root/spec/lib/matchers/include.rb
blob: c34725856a5307ffe9f01e5458f7a702c422d72e (plain)
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
module Matchers; module Include
  extend RSpec::Matchers::DSL

  matcher :include_in_any_order do |*matchers|
    match do |enumerable|
      @not_matched = []
      expected.each do |matcher|
        if enumerable.empty?
          break
        end

        if found = enumerable.find { |elem| matcher.matches?(elem) }
          enumerable = enumerable.reject { |elem| elem == found }
        else
          @not_matched << matcher
        end
      end


      @not_matched.empty? && enumerable.empty?
    end

    failure_message_for_should do |enumerable|
      "did not match #{@not_matched.collect(&:description).join(', ')} in #{enumerable.inspect}: <#{@not_matched.collect(&:failure_message_for_should).join('>, <')}>"
    end
  end
end; end