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 for a SourceFile regardless of which criteria were enabled during the run: disabled or empty criteria collapse to 0/0/0 so downstream consumers don't have to special-case enable-state.

Instance Method Summary collapse

Constructor Details

#initialize(source_file) ⇒ Statistics

Returns a new instance of Statistics.

Parameters:



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

def initialize(source_file)
  @source_file = source_file
end

Instance Method Details

#branch_statistics ⇒ Hash[criterion, CoverageStatistics]

Files added via track_files but never loaded have no branch or method data, and report 0% instead of the empty-set default of 100% (#902). A file with missed entries and none covered already computes to 0%, so only a file that really has covered entries keeps its computed percentage.

Returns:



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

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

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

#call ⇒ Hash[criterion, CoverageStatistics]

Returns:



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

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

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

Parameters:

Returns:



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

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_statistics ⇒ Hash[criterion, CoverageStatistics]

Returns:



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

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

#method_statistics ⇒ Hash[criterion, CoverageStatistics]

Returns:



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

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

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