Class: SimpleCov::SourceFile
- Inherits:
-
Object
- Object
- SimpleCov::SourceFile
- 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
Instance Attribute Summary collapse
-
#coverage_data ⇒ Object
readonly
The array of coverage data received from the Coverage.result.
-
#filename ⇒ Object
readonly
The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb).
Instance Method Summary collapse
-
#branches ⇒ Object
Return all the branches inside current source file.
- #branches_coverage_percent ⇒ Object
- #branches_for_line(line_number) ⇒ Object
-
#branches_report ⇒ Object
Return hash with key of line number and branch coverage count as value.
- #coverage_statistics ⇒ Object
-
#covered_branches ⇒ Array
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.
-
#covered_lines ⇒ Object
Returns all covered lines as SimpleCov::SourceFile::Line.
-
#covered_percent ⇒ Object
The coverage for this file in percent.
- #covered_strength ⇒ Object
-
#initialize(filename, coverage_data) ⇒ SourceFile
constructor
A new instance of SourceFile.
-
#line(number) ⇒ Object
Access SimpleCov::SourceFile::Line source lines by line number.
-
#line_with_missed_branch?(line_number) ⇒ Boolean
Check if any branches missing on given line number.
-
#lines ⇒ Object
(also: #source_lines)
Returns all source lines for this file as instances of SimpleCov::SourceFile::Line, and thus including coverage data.
-
#lines_of_code ⇒ Object
Returns the number of relevant lines (covered + missed).
-
#missed_branches ⇒ Array
Select the missed branches with coverage equal to zero.
-
#missed_lines ⇒ Object
Returns all lines that should have been, but were not covered as instances of SimpleCov::SourceFile::Line.
-
#never_lines ⇒ Object
Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances.
- #no_branches? ⇒ Boolean
- #no_lines? ⇒ Boolean
-
#project_filename ⇒ Object
The path to this source file relative to the projects directory.
- #relevant_lines ⇒ Object
-
#skipped_lines ⇒ Object
Returns all lines that were skipped as SimpleCov::SourceFile::Line instances.
-
#src ⇒ Object
(also: #source)
The source code for this file.
-
#total_branches ⇒ Object
Return the relevant branches to source file.
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_data ⇒ Object (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 |
#filename ⇒ Object (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
#branches ⇒ Object
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_percent ⇒ Object
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_report ⇒ Object
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_statistics ⇒ Object
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_branches ⇒ Array
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
129 130 131 |
# File 'lib/simplecov/source_file.rb', line 129 def covered_branches @covered_branches ||= branches.select(&:covered?) end |
#covered_lines ⇒ Object
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_percent ⇒ Object
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_strength ⇒ Object
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
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 |
#lines ⇒ Object 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_code ⇒ Object
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_branches ⇒ Array
Select the missed branches with coverage equal to zero
138 139 140 |
# File 'lib/simplecov/source_file.rb', line 138 def missed_branches @missed_branches ||= branches.select(&:missed?) end |
#missed_lines ⇒ Object
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_lines ⇒ Object
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
102 103 104 |
# File 'lib/simplecov/source_file.rb', line 102 def no_branches? total_branches.empty? end |
#no_lines? ⇒ 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_filename ⇒ Object
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_lines ⇒ Object
92 93 94 |
# File 'lib/simplecov/source_file.rb', line 92 def relevant_lines lines.size - never_lines.size - skipped_lines.size end |
#skipped_lines ⇒ Object
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 |
#src ⇒ Object 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_branches ⇒ Object
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 |