Class: SimpleCov::SourceFile::Statistics

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov/source_file/statistics.rb,
sig/simplecov.rbs

Overview

Builds the CoverageStatistics triple (:line/:branch/:method) for a SourceFile; disabled or empty criteria collapse to 0/0/0.

Instance Method Summary collapse

Constructor Details

#initialize(source_file) ⇒ Statistics

Returns a new instance of Statistics.

Parameters:



10
11
12
# File 'lib/simplecov/source_file/statistics.rb', line 10

def initialize(source_file)
  @source_file = source_file
end

Instance Method Details

#branch_statisticsHash[criterion, CoverageStatistics]

Files added via track_files but never loaded have no branch/method data. Report 0% instead of the empty-set default of 100% (see #902).

Returns:



36
37
38
39
40
41
42
43
# File 'lib/simplecov/source_file/statistics.rb', line 36

def branch_statistics
  sf = @source_file
  covered = sf.covered_branches
  missed = sf.missed_branches
  percent = 0.0 if sf.not_loaded? && covered.empty? && missed.empty?

  {branch: coverage_statistics(covered, missed, percent: percent)}
end

#callHash[criterion, CoverageStatistics]

Returns:



14
15
16
17
18
19
20
# File 'lib/simplecov/source_file/statistics.rb', line 14

def call
  {
    **line_statistics,
    **branch_statistics,
    **method_statistics
  }
end

#coverage_statistics(covered, missed, omitted: 0, percent: nil) ⇒ CoverageStatistics

Parameters:

Returns:



54
55
56
57
58
59
60
61
62
# File 'lib/simplecov/source_file/statistics.rb', line 54

def coverage_statistics(covered, missed, omitted: 0, percent: nil)
  CoverageStatistics.new(
    total_strength: covered.sum { |item| item.coverage.to_i },
    covered: covered.size,
    missed: missed.size,
    omitted: omitted,
    percent: percent
  )
end

#line_statisticsHash[criterion, CoverageStatistics]

Returns:



24
25
26
27
28
29
30
31
32
# File 'lib/simplecov/source_file/statistics.rb', line 24

def line_statistics
  {
    line: coverage_statistics(
      @source_file.covered_lines,
      @source_file.missed_lines,
      omitted: @source_file.never_lines.size
    )
  }
end

#method_statisticsHash[criterion, CoverageStatistics]

Returns:



45
46
47
48
49
50
51
52
# File 'lib/simplecov/source_file/statistics.rb', line 45

def method_statistics
  sf = @source_file
  covered = sf.covered_methods
  missed = sf.missed_methods
  percent = 0.0 if sf.not_loaded? && covered.empty? && missed.empty?

  {method: coverage_statistics(covered, missed, percent: percent)}
end