Class: Covered::Summary

Inherits:
Wrapper show all
Defined in:
lib/covered/summary.rb

Instance Attribute Summary

Attributes inherited from Wrapper

#output

Instance Method Summary collapse

Methods inherited from Wrapper

#disable, #enable, #mark, #to_h

Constructor Details

#initialize(output, threshold: 1.0) ⇒ Summary



28
29
30
31
32
33
34
# File 'lib/covered/summary.rb', line 28

def initialize(output, threshold: 1.0)
  super(output)
  
  @statistics = nil
  
  @threshold = threshold
end

Instance Method Details

#eachObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/covered/summary.rb', line 36

def each
  @statistics = Statistics.new
  
  super do |coverage|
    @statistics << coverage
    
    if coverage.ratio < @threshold
      yield coverage
    end
  end
end


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/covered/summary.rb', line 86

def print_partial_summary(output = $stdout, before: 4, after: 4)
  statistics = Statistics.new
  
  self.each do |coverage|
    line_offset = 1
    output.puts "", Rainbow(coverage.path).bold.underline
    
    counts = coverage.counts
    
    File.open(coverage.path, "r") do |file|
      file.each_line do |line|
        range = Range.new([line_offset - before, 0].max, line_offset+after)
        
        if counts[range]&.include?(0)
          count = counts[line_offset]
          
          prefix = "#{line_offset} ".rjust(8) + "#{count} ".rjust(8)
          
          if count == nil
            output.write prefix
            output.write Rainbow(line).faint
          elsif count == 0
            output.write Rainbow(prefix).background(:darkred)
            output.write Rainbow(line).red
          else
            output.write Rainbow(prefix).background(:darkgreen)
            output.write Rainbow(line).green
          end
          
          # If there was no newline at end of file, we add one:
          unless line.end_with? $/
            output.puts
          end
        end
        
        line_offset += 1
      end
    end
    
    coverage.print_summary(output)
  end
  
  output.puts
  @statistics.print_summary(output)
end

A coverage array gives, for each line, the number of line execution by the interpreter. A nil value means coverage is disabled for this line (lines like else and end).



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/covered/summary.rb', line 49

def print_summary(output = $stdout)
  self.each do |coverage|
    line_offset = 1
    output.puts "", Rainbow(coverage.path).bold.underline
    
    counts = coverage.counts
    
    File.open(coverage.path, "r") do |file|
      file.each_line do |line|
        count = counts[line_offset]
        
        output.write("#{line_offset}|".rjust(8))
        output.write("#{count}:".rjust(8))
        
        if count == nil
          output.write Rainbow(line).faint
        elsif count == 0
          output.write Rainbow(line).red
        else
          output.write Rainbow(line).green
        end
        
        # If there was no newline at end of file, we add one:
        unless line.end_with? $/
          output.puts
        end
        
        line_offset += 1
      end
    end
    
    coverage.print_summary(output)
  end
  
  @statistics.print_summary(output)
end