class Hiptest::ProjectGrapher
Builds a graph based on calls and computes longest path from the root.
Attributes
distance_index[R]
graph[R]
Public Class Methods
distances_index(project)
click to toggle source
# File lib/hiptest-publisher/project_grapher.rb, line 10 def self.distances_index(project) instance = ProjectGrapher.new(project) instance.compute_graph instance.add_distances instance.index_by_distances return instance.distance_index end
new(project)
click to toggle source
# File lib/hiptest-publisher/project_grapher.rb, line 19 def initialize(project) @project = project @graph = {} end
Public Instance Methods
add_distances()
click to toggle source
# File lib/hiptest-publisher/project_grapher.rb, line 29 def add_distances add_node_weight(@graph[:root], [:root]) end
compute_graph()
click to toggle source
# File lib/hiptest-publisher/project_grapher.rb, line 24 def compute_graph add_nodes add_root end
index_by_distances()
click to toggle source
# File lib/hiptest-publisher/project_grapher.rb, line 33 def index_by_distances @distance_index = Hash.new { |hash, key| hash[key] = [] } @graph.each_value do |value| @distance_index[value[:distance_from_root]] << value[:item] if value[:item] end end
Private Instance Methods
add_node_weight(node, path)
click to toggle source
# File lib/hiptest-publisher/project_grapher.rb, line 70 def add_node_weight(node, path) path << node[:name] node[:calls].each do |item_name| next if path.include?(item_name) called = @graph[item_name] next if called.nil? if called[:distance_from_root] <= node[:distance_from_root] called[:distance_from_root] = node[:distance_from_root] + 1 add_node_weight(called, path) end end path.pop end
add_nodes()
click to toggle source
# File lib/hiptest-publisher/project_grapher.rb, line 42 def add_nodes @project.each_sub_nodes(Hiptest::Nodes::Scenario, Hiptest::Nodes::Actionword) do |item| name = node_name(item) @graph[name] = { name: name, item: item, calls: [], distance_from_root: -1 } item.each_sub_nodes(Hiptest::Nodes::Call) do |call| aw_name = node_name(call.children[:actionword], Hiptest::Nodes::Actionword) @graph[name][:calls] << aw_name end end end
add_root()
click to toggle source
# File lib/hiptest-publisher/project_grapher.rb, line 59 def add_root @graph[:root] = { calls: [], distance_from_root: 0 } @project.each_sub_nodes(Hiptest::Nodes::Scenario) do |scenario| @graph[:root][:calls] << node_name(scenario) end end
node_name(node, cls = nil)
click to toggle source
# File lib/hiptest-publisher/project_grapher.rb, line 88 def node_name(node, cls = nil) cls ||= node.class name = node.is_a?(Hiptest::Nodes::Node) ? node.children[:name] : node "#{cls}-#{name}" end