Class: Rcov::BaseFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rcov/formatters/base_formatter.rb

Overview

:nodoc:

Direct Known Subclasses

Formatters::RubyAnnotation, FullTextReport, HTMLCoverage, RubyAnnotation, TextCoverageDiff, TextSummary

Defined Under Namespace

Classes: XRefHelper

Constant Summary

RCOV_IGNORE_REGEXPS =
['libdir']).cleanpath.to_s)}/, 
  /\btc_[^.]*.rb/, 
  /_test\.rb\z/, 
  /\btest\//, 
  /\bvendor\//, 
  /\A#{Regexp.escape(__FILE__)}\z/
]
DEFAULT_OPTS =
{ :ignore => RCOV_IGNORE_REGEXPS, :sort => :name, :sort_reverse => false,
:output_threshold => 101, :dont_ignore => [], :callsite_analyzer => nil, \
:comments_run_by_default => false }

Instance Method Summary (collapse)

Constructor Details

- (BaseFormatter) initialize(opts = {})



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rcov/formatters/base_formatter.rb', line 17

def initialize(opts = {})
  options = DEFAULT_OPTS.clone.update(opts)
  @failure_threshold = options[:failure_threshold]
  @files = {}
  @ignore_files = options[:ignore]
  @dont_ignore_files = options[:dont_ignore]
  @sort_criterium = case options[:sort]
  when :loc then lambda{|fname, finfo| finfo.num_code_lines}
  when :coverage then lambda{|fname, finfo| finfo.code_coverage}
  else lambda { |fname, finfo| fname }
  end
  @sort_reverse = options[:sort_reverse]
  @output_threshold = options[:output_threshold]
  @callsite_analyzer = options[:callsite_analyzer]
  @comments_run_by_default = options[:comments_run_by_default]
  @callsite_index = nil

  @mangle_filename = Hash.new{|h,base|
    h[base] = Pathname.new(base).cleanpath.to_s.gsub(%r{^\w:[/\\]}, "").gsub(/\./, "_").gsub(/[\\\/]/, "-") + ".html"
  }
end

Instance Method Details

- (Object) add_file(filename, lines, coverage, counts)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rcov/formatters/base_formatter.rb', line 39

def add_file(filename, lines, coverage, counts)
  old_filename = filename
  filename = normalize_filename(filename)
  SCRIPT_LINES__[filename] = SCRIPT_LINES__[old_filename]
  if @ignore_files.any?{|x| x === filename} &&
    !@dont_ignore_files.any?{|x| x === filename}
    return nil
  end
  if @files[filename]
    @files[filename].merge(lines, coverage, counts)
  else
    @files[filename] = FileStatistics.new(filename, lines, counts,
    @comments_run_by_default)
  end
end

- (Object) code_coverage



86
87
88
89
90
91
92
93
94
95
# File 'lib/rcov/formatters/base_formatter.rb', line 86

def code_coverage
  lines = 0
  total = 0.0
  @files.each do |k,f|
    total += f.num_code_lines * f.code_coverage
    lines += f.num_code_lines
  end
  return 0 if lines == 0
  total / lines
end

- (Object) each_file_pair_sorted(&b)



63
64
65
66
# File 'lib/rcov/formatters/base_formatter.rb', line 63

def each_file_pair_sorted(&b)
  return sorted_file_pairs unless block_given?
  sorted_file_pairs.each(&b)
end

- (Object) mangle_filename(base)



59
60
61
# File 'lib/rcov/formatters/base_formatter.rb', line 59

def mangle_filename(base)
  @mangle_filename[base]
end

- (Object) normalize_filename(filename)



55
56
57
# File 'lib/rcov/formatters/base_formatter.rb', line 55

def normalize_filename(filename)
  File.expand_path(filename).gsub(/^#{Regexp.escape(Dir.getwd)}\//, '')
end

- (Object) num_code_lines



97
98
99
100
101
# File 'lib/rcov/formatters/base_formatter.rb', line 97

def num_code_lines
  lines = 0
  @files.each{|k, f| lines += f.num_code_lines }
  lines
end

- (Object) num_lines



103
104
105
106
107
# File 'lib/rcov/formatters/base_formatter.rb', line 103

def num_lines
  lines = 0
  @files.each{|k, f| lines += f.num_lines }
  lines
end

- (Object) sorted_file_pairs



68
69
70
71
72
73
# File 'lib/rcov/formatters/base_formatter.rb', line 68

def sorted_file_pairs
  pairs = @files.sort_by do |fname, finfo|
    @sort_criterium.call(fname, finfo)
  end.select{|_, finfo| 100 * finfo.code_coverage < @output_threshold}
  @sort_reverse ? pairs.reverse : pairs
end

- (Object) total_coverage



75
76
77
78
79
80
81
82
83
84
# File 'lib/rcov/formatters/base_formatter.rb', line 75

def total_coverage
  lines = 0
  total = 0.0
  @files.each do |k,f|
    total += f.num_lines * f.total_coverage
    lines += f.num_lines
  end
  return 0 if lines == 0
  total / lines
end