summaryrefslogtreecommitdiff
path: root/spec/lib/matchers/containment_matchers.rb
blob: bc412fc5e9c81b184167f74b6328df5881f974b0 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module ContainmentMatchers
  class ContainClass
    def initialize(containee)
      @containee = containee
    end

    def in(container)
      @container = container
      self
    end

    def matches?(catalog)
      @catalog = catalog

      raise ArgumentError, "You must set the container using #in" unless @container

      @container_resource = catalog.resource("Class", @container)
      @containee_resource = catalog.resource("Class", @containee)

      if @containee_resource && @container_resource
        catalog.edge?(@container_resource, @containee_resource)
      else
        false
      end
    end

    def failure_message_for_should
      message = "Expected #{@catalog.to_dot} to contain Class #{@containee.inspect} inside of Class #{@container.inspect} but "

      missing = []
      if @container_resource.nil?
        missing << @container
      end
      if @containee_resource.nil?
        missing << @containee
      end

      if ! missing.empty?
        message << "the catalog does not contain #{missing.map(&:inspect).join(' or ')}"
      else
        message << "no containment relationship exists"
      end

      message
    end
  end

  # expect(catalog).to contain_class(containee).in(container)
  def contain_class(containee)
    ContainClass.new(containee)
  end
end