Class: Covered::Summary

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

Direct Known Subclasses

BriefSummary, PartialSummary

Instance Method Summary collapse

Constructor Details

#initialize(threshold: 1.0) ⇒ Summary

Returns a new instance of Summary.



28
29
30
# File 'lib/covered/summary.rb', line 28

def initialize(threshold: 1.0)
  @threshold = threshold
end

Instance Method Details

#call(wrapper, output = $stdout) ⇒ Object

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).



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/covered/summary.rb', line 59

def call(wrapper, output = $stdout)
  statistics = self.each(wrapper) do |coverage|
    line_offset = 1
    
    path = wrapper.relative_path(coverage.path)
    output.puts "", Rainbow(path).bold.underline
    
    counts = coverage.counts
    
    coverage.read do |file|
      file.each_line do |line|
        count = counts[line_offset]
        
        print_annotations(output, coverage, line, 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(output)
  end
  
  statistics.print(output)
end

#each(wrapper) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/covered/summary.rb', line 32

def each(wrapper)
  statistics = Statistics.new
  
  wrapper.each do |coverage|
    statistics << coverage
    
    if @threshold.nil? or coverage.ratio < @threshold
      yield coverage
    end
  end
  
  return statistics
end


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/covered/summary.rb', line 46

def print_annotations(output, coverage, line, line_offset)
  if annotations = coverage.annotations[line_offset]
    output.write("#{line_offset}|".rjust(8))
    output.write("*|".rjust(8))
    
    output.write line.match(/^\s+/)
    output.write '# '
    
    output.puts Rainbow(annotations.join(", ")).bright
  end
end