Class: SimpleCov::SourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov/source_file.rb,
lib/simplecov/source_file/line.rb,
lib/simplecov/source_file/branch.rb

Overview

Representation of a source file including it’s coverage data, source code, source lines and featuring helpers to interpret that data.

Defined Under Namespace

Classes: Branch, Line

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, coverage_data) ⇒ SourceFile

Returns a new instance of SourceFile.



14
15
16
17
# File 'lib/simplecov/source_file.rb', line 14

def initialize(filename, coverage_data)
  @filename = filename
  @coverage_data = coverage_data
end

Instance Attribute Details

#coverage_dataObject (readonly)

The array of coverage data received from the Coverage.result



12
13
14
# File 'lib/simplecov/source_file.rb', line 12

def coverage_data
  @coverage_data
end

#filenameObject (readonly)

The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb)



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

def filename
  @filename
end

Instance Method Details

#branchesObject

Return all the branches inside current source file



98
99
100
# File 'lib/simplecov/source_file.rb', line 98

def branches
  @branches ||= build_branches
end

#branches_coverage_percentObject



106
107
108
# File 'lib/simplecov/source_file.rb', line 106

def branches_coverage_percent
  coverage_statistics[:branch]&.percent
end

#branches_for_line(line_number) ⇒ Object



142
143
144
# File 'lib/simplecov/source_file.rb', line 142

def branches_for_line(line_number)
  branches_report.fetch(line_number, [])
end

#branches_reportObject

Return hash with key of line number and branch coverage count as value



118
119
120
# File 'lib/simplecov/source_file.rb', line 118

def branches_report
  @branches_report ||= build_branches_report
end

#coverage_statisticsObject



32
33
34
35
36
37
38
# File 'lib/simplecov/source_file.rb', line 32

def coverage_statistics
  @coverage_statistics ||=
    {
      **line_coverage_statistics,
      **branch_coverage_statistics
    }
end

#covered_branchesArray

Select the covered branches Here we user tree schema because some conditions like case may have additional else that is not in declared inside the code but given by default by coverage report

Returns:

  • (Array)


129
130
131
# File 'lib/simplecov/source_file.rb', line 129

def covered_branches
  @covered_branches ||= branches.select(&:covered?)
end

#covered_linesObject

Returns all covered lines as SimpleCov::SourceFile::Line



48
49
50
# File 'lib/simplecov/source_file.rb', line 48

def covered_lines
  @covered_lines ||= lines.select(&:covered?)
end

#covered_percentObject

The coverage for this file in percent. 0 if the file has no coverage lines



80
81
82
# File 'lib/simplecov/source_file.rb', line 80

def covered_percent
  coverage_statistics[:line]&.percent
end

#covered_strengthObject



84
85
86
# File 'lib/simplecov/source_file.rb', line 84

def covered_strength
  coverage_statistics[:line]&.strength
end

#line(number) ⇒ Object

Access SimpleCov::SourceFile::Line source lines by line number



75
76
77
# File 'lib/simplecov/source_file.rb', line 75

def line(number)
  lines[number - 1]
end

#line_with_missed_branch?(line_number) ⇒ Boolean

Check if any branches missing on given line number

Parameters:

  • line_number (Integer)

Returns:

  • (Boolean)


153
154
155
# File 'lib/simplecov/source_file.rb', line 153

def line_with_missed_branch?(line_number)
  branches_for_line(line_number).select { |_type, count| count.zero? }.any?
end

#linesObject Also known as: source_lines

Returns all source lines for this file as instances of SimpleCov::SourceFile::Line, and thus including coverage data. Aliased as :source_lines



42
43
44
# File 'lib/simplecov/source_file.rb', line 42

def lines
  @lines ||= build_lines
end

#lines_of_codeObject

Returns the number of relevant lines (covered + missed)



70
71
72
# File 'lib/simplecov/source_file.rb', line 70

def lines_of_code
  coverage_statistics[:line]&.total
end

#missed_branchesArray

Select the missed branches with coverage equal to zero

Returns:

  • (Array)


138
139
140
# File 'lib/simplecov/source_file.rb', line 138

def missed_branches
  @missed_branches ||= branches.select(&:missed?)
end

#missed_linesObject

Returns all lines that should have been, but were not covered as instances of SimpleCov::SourceFile::Line



54
55
56
# File 'lib/simplecov/source_file.rb', line 54

def missed_lines
  @missed_lines ||= lines.select(&:missed?)
end

#never_linesObject

Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances



60
61
62
# File 'lib/simplecov/source_file.rb', line 60

def never_lines
  @never_lines ||= lines.select(&:never?)
end

#no_branches?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/simplecov/source_file.rb', line 102

def no_branches?
  total_branches.empty?
end

#no_lines?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/simplecov/source_file.rb', line 88

def no_lines?
  lines.length.zero? || (lines.length == never_lines.size)
end

#project_filenameObject

The path to this source file relative to the projects directory



20
21
22
# File 'lib/simplecov/source_file.rb', line 20

def project_filename
  @filename.delete_prefix(SimpleCov.root)
end

#relevant_linesObject



92
93
94
# File 'lib/simplecov/source_file.rb', line 92

def relevant_lines
  lines.size - never_lines.size - skipped_lines.size
end

#skipped_linesObject

Returns all lines that were skipped as SimpleCov::SourceFile::Line instances



65
66
67
# File 'lib/simplecov/source_file.rb', line 65

def skipped_lines
  @skipped_lines ||= lines.select(&:skipped?)
end

#srcObject Also known as: source

The source code for this file. Aliased as :source



25
26
27
28
29
# File 'lib/simplecov/source_file.rb', line 25

def src
  # We intentionally read source code lazily to
  # suppress reading unused source code.
  @src ||= load_source
end

#total_branchesObject

Return the relevant branches to source file



112
113
114
# File 'lib/simplecov/source_file.rb', line 112

def total_branches
  @total_branches ||= covered_branches + missed_branches
end