summaryrefslogtreecommitdiff
path: root/spec/lib/matchers/relationship_graph_matchers.rb
blob: 3d08cdd8780fe6aadb7c27bd9039d95c34e133a4 (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
module RelationshipGraphMatchers
  class EnforceOrderWithEdge
    def initialize(before, after)
      @before = before
      @after = after
    end

    def matches?(actual_graph)
      @actual_graph = actual_graph

      @reverse_edge = actual_graph.edge?(
          vertex_called(actual_graph, @after),
          vertex_called(actual_graph, @before))

      @forward_edge = actual_graph.edge?(
          vertex_called(actual_graph, @before),
          vertex_called(actual_graph, @after))

      @forward_edge && !@reverse_edge
    end

    def failure_message_for_should
      "expect #{@actual_graph.to_dot_graph} to only contain an edge from #{@before} to #{@after} but #{[forward_failure_message, reverse_failure_message].compact.join(' and ')}"
    end

    def forward_failure_message
      if !@forward_edge
        "did not contain an edge from #{@before} to #{@after}"
      end
    end

    def reverse_failure_message
      if @reverse_edge
        "contained an edge from #{@after} to #{@before}"
      end
    end

    private

    def vertex_called(graph, name)
      graph.vertices.find { |v| v.ref =~ /#{Regexp.escape(name)}/ }
    end
  end

  def enforce_order_with_edge(before, after)
    EnforceOrderWithEdge.new(before, after)
  end
end