summaryrefslogtreecommitdiff
path: root/lib/rspec_flaky/flaky_examples_collection.rb
blob: dea23c325be9803a54f0fab450123ddd77479ada (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
28
29
30
31
32
33
34
35
require 'active_support/hash_with_indifferent_access'

require_relative 'flaky_example'

module RspecFlaky
  class FlakyExamplesCollection < SimpleDelegator
    def initialize(collection = {})
      unless collection.is_a?(Hash)
        raise ArgumentError, "`collection` must be a Hash, #{collection.class} given!"
      end

      collection_of_flaky_examples =
        collection.map do |uid, example|
          [
            uid,
            example.is_a?(RspecFlaky::FlakyExample) ? example : RspecFlaky::FlakyExample.new(example)
          ]
        end

      super(Hash[collection_of_flaky_examples])
    end

    def to_h
      Hash[map { |uid, example| [uid, example.to_h] }].deep_symbolize_keys
    end

    def -(other)
      unless other.respond_to?(:key)
        raise ArgumentError, "`other` must respond to `#key?`, #{other.class} does not!"
      end

      self.class.new(reject { |uid, _| other.key?(uid) })
    end
  end
end