Class: Gold::Coverage::Results

Inherits:
Object
  • Object
show all
Defined in:
lib/gold/coverage.rb

Overview

Holds the collected coverage data.

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ Results

Returns a new instance of Results.



41
42
43
44
# File 'lib/gold/coverage.rb', line 41

def initialize(machine)
  @machine = machine
  @covered = Set.new
end

Instance Method Details

#cover(from, to) ⇒ Object

Records when a transition is made from one state to another.



47
48
49
# File 'lib/gold/coverage.rb', line 47

def cover(from, to)
  @covered.add(Transition.new(from, to))
end

#to_hObject

Iterates over transitions that have been made thus far. This is in the same form as ‘Statesman::Machine#successors`, so the two can be compared to see where coverage is lacking.



54
55
56
57
58
59
# File 'lib/gold/coverage.rb', line 54

def to_h
  @covered.each_with_object(Hash.new { [] }) do |transition, hash|
    hash[transition.from] = hash[transition.from] << transition.to
    hash
  end
end

#to_svg(path) ⇒ Object

Generates an SVG diagram of the coverage that has been captured so far. Covered transitions are colored green and uncovered transitions are colored red.



64
65
66
67
68
69
70
71
72
73
# File 'lib/gold/coverage.rb', line 64

def to_svg(path)
  coverage = to_h
  callback = lambda do |edge|
    from = edge.tail_node
    to = edge.head_node
    edge[:color] =
      coverage.key?(from) && coverage[from].include?(to) ? "green" : "red"
  end
  Gold::Diagram.new(@machine, edge_callback: callback).render(path)
end