Class: Pf2::Reporter::Annotate

Inherits:
Object
  • Object
show all
Defined in:
lib/pf2/reporter/annotate.rb

Defined Under Namespace

Classes: HitCount, SourceCodeHits

Instance Method Summary collapse

Constructor Details

#initialize(profile, source_directory) ⇒ Annotate

Returns a new instance of Annotate.

Parameters:

  • profile (Hash)
  • source_directory (String)


11
12
13
14
# File 'lib/pf2/reporter/annotate.rb', line 11

def initialize(profile, source_directory)
  @profile = profile
  @source_directory = source_directory
end

Instance Method Details

#annotateObject



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
53
54
55
56
57
58
# File 'lib/pf2/reporter/annotate.rb', line 16

def annotate
  tallied = tally_by_source_code_line(@profile)

  # Print the source code with hit counts
  tallied.each do |path, source_code_hits|
    expanded_path = File.expand_path(path, @source_directory)
    if !File.exist?(expanded_path)
      if ignorable_path?(path)
        puts "Ignoring file: #{path}"
      else
        puts "File not found: #{path}"
      end
      puts ""
      puts ""
      next
    end
    source_file = File.open(expanded_path, "r")

    puts expanded_path
    puts ""

    # Print in tabular format

    # Header row
    puts "  ttl  self │"

    source_file.each_line.with_index(1) do |line, lineno|
      hits = source_code_hits.line_count[lineno]

      if !hits.nil?
        # If any samples are captured for this line
        puts "%5d %5d │ %s" % [hits.total, hits.self, line.chomp]
      else
        puts "%5s %5s │ %s" % ["", "", line.chomp]
      end
    end

    puts ""
    puts ""
  ensure
    source_file.close if source_file
  end
end