class Piggly::Compiler::CoverageReport

Produces HTML output to report coverage of tagged nodes in the tree

Public Class Methods

new(config) click to toggle source
# File lib/piggly/compiler/coverage_report.rb, line 10
def initialize(config)
  @config = config
end

Public Instance Methods

compile(procedure, profile) click to toggle source
# File lib/piggly/compiler/coverage_report.rb, line 14
def compile(procedure, profile)
  trace = Compiler::TraceCompiler.new(@config)

  if trace.stale?(procedure)
    raise StaleCacheError,
      "stale cached syntax tree for #{procedure.name}"
  end

  # Get (copies of) the tagged nodes from the compiled tree
  data = trace.compile(procedure)

  return :html  => traverse(data[:tree], profile),
         :lines => 1 .. procedure.source(@config).count("\n") + 1
end

Protected Instance Methods

traverse(node, profile, string = "") click to toggle source

@return [String]

# File lib/piggly/compiler/coverage_report.rb, line 32
def traverse(node, profile, string = "")
  if node.tagged?
    tag = profile[node.tag_id]

    if tag.complete?
      string << %[<span class="#{tag.style}" id="T#{tag.id}">]
    else
      string << %[<span class="#{tag.style}" id="T#{tag.id}" title="#{tag.description}">]
    end
  end

  if node.terminal?
    if style = node.style
      string << %[<span class="#{style}">#{e(node.text_value)}</span>]
    else
      string << e(node.text_value)
    end
  else
    # Non-terminals never write their text_value
    node.elements.each{|child| traverse(child, profile, string) }
  end

  if node.tagged?
    string << %[</span>]
  end

  string
end