Class: SimpleCov::SourceFile
- Inherits:
-
Object
- Object
- SimpleCov::SourceFile
- Includes:
- BuilderContext
- Defined in:
- lib/simplecov/source_file.rb,
lib/simplecov/source_file/line.rb,
lib/simplecov/source_file/branch.rb,
lib/simplecov/source_file/method.rb,
lib/simplecov/source_file/statistics.rb,
lib/simplecov/source_file/skip_chunks.rb,
lib/simplecov/source_file/line_builder.rb,
lib/simplecov/source_file/source_loader.rb,
lib/simplecov/source_file/branch_builder.rb,
lib/simplecov/source_file/method_builder.rb,
lib/simplecov/source_file/builder_context.rb,
lib/simplecov/source_file/ruby_data_parser.rb,
sig/simplecov.rbs,
sig/simplecov.rbs,
sig/simplecov.rbs,
sig/simplecov.rbs,
sig/simplecov.rbs,
sig/simplecov.rbs
Overview
A source file with its coverage data, source lines, and helpers to interpret that data.
Defined Under Namespace
Modules: BuilderContext, RubyDataParser, SourceLoader Classes: Branch, BranchBuilder, Line, LineBuilder, Method, MethodBuilder, SkipChunks, Statistics
Instance Attribute Summary collapse
-
#coverage_data ⇒ Hash[String, untyped]
readonly
Coverage.result-shaped data for this file, e.g.
-
#filename ⇒ String
readonly
Full absolute path to the file.
Instance Method Summary collapse
-
#branches ⇒ Array[Branch]
Return all the branches inside current source file.
-
#branches_coverage_percent ⇒ Float?
DEPRECATED: use
covered_percent(:branch). - #branches_for_line(line_number) ⇒ Array[[ Symbol, Integer ]]
-
#branches_report ⇒ Hash[Integer, Array[[ Symbol, Integer ]]]
line number => Array of [branch type, hit count] pairs.
-
#coverage_statistics(criterion = nil) ⇒ Object
With no argument returns a Hash keyed by every supported criterion (disabled criteria collapse to 0/0/0); pass a criterion to get that one CoverageStatistics.
-
#covered_branches ⇒ Array[Branch]
Select the covered branches.
-
#covered_lines ⇒ Array[Line]
Returns all covered lines as SimpleCov::SourceFile::Line.
- #covered_methods ⇒ Array[Method]
-
#covered_percent(criterion = :line) ⇒ Float?
The coverage for this file in percent, for the given criterion (line by default).
- #covered_strength(criterion = :line) ⇒ Float?
-
#initialize(filename, coverage_data, loaded: true) ⇒ SourceFile
constructor
A new instance of SourceFile.
-
#line(number) ⇒ Line?
1-based line lookup.
-
#line_with_missed_branch?(line_number) ⇒ Boolean
Check if any branches missing on given line number.
-
#lines ⇒ Array[Line]
(also: #source_lines)
Returns all source lines for this file as instances of SimpleCov::SourceFile::Line, and thus including coverage data.
-
#lines_of_code ⇒ Integer
Returns the number of relevant lines (covered + missed).
-
#methods ⇒ Array[Method]
Return all methods detected in this source file.
-
#methods_coverage_percent ⇒ Float?
DEPRECATED: use
covered_percent(:method). -
#missed_branches ⇒ Array[Branch]
Select the missed branches with coverage equal to zero.
-
#missed_lines ⇒ Array[Line]
Returns all lines that should have been, but were not covered as instances of SimpleCov::SourceFile::Line.
- #missed_methods ⇒ Array[Method]
-
#never_lines ⇒ Array[Line]
Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances.
- #no_branches? ⇒ Boolean
- #no_lines? ⇒ Boolean
-
#not_loaded? ⇒ Boolean
Whether this file was added via
cover/track_filesbut never loaded during the run. -
#project_filename ⇒ String
Project-relative path with no leading separator (e.g. "lib/foo.rb").
- #relevant_lines ⇒ Integer
-
#skipped_lines ⇒ Array[Line]
Returns all lines that were skipped as SimpleCov::SourceFile::Line instances.
-
#src ⇒ Array[String]
(also: #source)
Source lines, read lazily.
-
#total_branches ⇒ Array[Branch]
All relevant (covered + missed) branches, not a count.
Methods included from BuilderContext
#real_source_positions, #skip_chunks_for
Constructor Details
#initialize(filename, coverage_data, loaded: true) ⇒ SourceFile
Returns a new instance of SourceFile.
27 28 29 30 31 |
# File 'lib/simplecov/source_file.rb', line 27 def initialize(filename, coverage_data, loaded: true) @filename = filename @coverage_data = coverage_data @loaded = loaded end |
Instance Attribute Details
#coverage_data ⇒ Hash[String, untyped] (readonly)
Coverage.result-shaped data for this file, e.g. => [...], "branches" => {..., "methods" => ...}.
25 26 27 |
# File 'lib/simplecov/source_file.rb', line 25 def coverage_data @coverage_data end |
#filename ⇒ String (readonly)
Full absolute path to the file.
23 24 25 |
# File 'lib/simplecov/source_file.rb', line 23 def filename @filename end |
Instance Method Details
#branches ⇒ Array[Branch]
Return all the branches inside current source file
116 117 118 |
# File 'lib/simplecov/source_file.rb', line 116 def branches @branches ||= BranchBuilder.new(self).call end |
#branches_coverage_percent ⇒ Float?
DEPRECATED: use covered_percent(:branch).
125 126 127 128 129 |
# File 'lib/simplecov/source_file.rb', line 125 def branches_coverage_percent SimpleCov::Deprecation.warn("`SimpleCov::SourceFile#branches_coverage_percent` is deprecated. " \ "Use `covered_percent(:branch)`.") covered_percent(:branch) end |
#branches_for_line(line_number) ⇒ Array[[ Symbol, Integer ]]
155 156 157 |
# File 'lib/simplecov/source_file.rb', line 155 def branches_for_line(line_number) branches_report.fetch(line_number, []) end |
#branches_report ⇒ Hash[Integer, Array[[ Symbol, Integer ]]]
line number => Array of [branch type, hit count] pairs.
137 138 139 140 |
# File 'lib/simplecov/source_file.rb', line 137 def branches_report @branches_report ||= branches.reject(&:skipped?).group_by(&:report_line).transform_values { |bs| bs.map(&:report) } end |
#coverage_statistics ⇒ Hash[criterion, CoverageStatistics] #coverage_statistics(arg0) ⇒ CoverageStatistics?
With no argument returns a Hash keyed by every supported criterion (disabled criteria collapse to 0/0/0); pass a criterion to get that one CoverageStatistics.
53 54 55 56 |
# File 'lib/simplecov/source_file.rb', line 53 def coverage_statistics(criterion = nil) stats = (@coverage_statistics ||= Statistics.new(self).call) criterion ? stats[criterion] : stats end |
#covered_branches ⇒ Array[Branch]
Select the covered branches. We use a tree schema here because
some conditions like case may have an additional else that
isn't declared in code but is given by default by the coverage
report.
146 147 148 |
# File 'lib/simplecov/source_file.rb', line 146 def covered_branches @covered_branches ||= branches.select(&:covered?) end |
#covered_lines ⇒ Array[Line]
Returns all covered lines as SimpleCov::SourceFile::Line
66 67 68 |
# File 'lib/simplecov/source_file.rb', line 66 def covered_lines @covered_lines ||= lines.select(&:covered?) end |
#covered_methods ⇒ Array[Method]
169 170 171 |
# File 'lib/simplecov/source_file.rb', line 169 def covered_methods @covered_methods ||= methods.select(&:covered?) end |
#covered_percent(criterion = :line) ⇒ Float?
The coverage for this file in percent, for the given criterion (line by default). Returns nil if the criterion was not measured.
99 100 101 |
# File 'lib/simplecov/source_file.rb', line 99 def covered_percent(criterion = :line) coverage_statistics(criterion)&.percent end |
#covered_strength(criterion = :line) ⇒ Float?
103 104 105 |
# File 'lib/simplecov/source_file.rb', line 103 def covered_strength(criterion = :line) coverage_statistics(criterion)&.strength end |
#line(number) ⇒ Line?
1-based line lookup.
93 94 95 |
# File 'lib/simplecov/source_file.rb', line 93 def line(number) lines[number - 1] end |
#line_with_missed_branch?(line_number) ⇒ Boolean
Check if any branches missing on given line number
160 161 162 |
# File 'lib/simplecov/source_file.rb', line 160 def line_with_missed_branch?(line_number) branches_for_line(line_number).any? { |_type, count| count.zero? } end |
#lines ⇒ Array[Line] 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
60 61 62 |
# File 'lib/simplecov/source_file.rb', line 60 def lines @lines ||= LineBuilder.new(self).call end |
#lines_of_code ⇒ Integer
Returns the number of relevant lines (covered + missed)
88 89 90 |
# File 'lib/simplecov/source_file.rb', line 88 def lines_of_code coverage_statistics[:line]&.total || 0 end |
#methods ⇒ Array[Method]
Return all methods detected in this source file
165 166 167 |
# File 'lib/simplecov/source_file.rb', line 165 def methods @methods ||= MethodBuilder.new(self).call end |
#methods_coverage_percent ⇒ Float?
DEPRECATED: use covered_percent(:method).
178 179 180 181 182 |
# File 'lib/simplecov/source_file.rb', line 178 def methods_coverage_percent SimpleCov::Deprecation.warn("`SimpleCov::SourceFile#methods_coverage_percent` is deprecated. " \ "Use `covered_percent(:method)`.") covered_percent(:method) end |
#missed_branches ⇒ Array[Branch]
Select the missed branches with coverage equal to zero
151 152 153 |
# File 'lib/simplecov/source_file.rb', line 151 def missed_branches @missed_branches ||= branches.select(&:missed?) end |
#missed_lines ⇒ Array[Line]
Returns all lines that should have been, but were not covered as instances of SimpleCov::SourceFile::Line
72 73 74 |
# File 'lib/simplecov/source_file.rb', line 72 def missed_lines @missed_lines ||= lines.select(&:missed?) end |
#missed_methods ⇒ Array[Method]
173 174 175 |
# File 'lib/simplecov/source_file.rb', line 173 def missed_methods @missed_methods ||= methods.select(&:missed?) end |
#never_lines ⇒ Array[Line]
Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances
78 79 80 |
# File 'lib/simplecov/source_file.rb', line 78 def never_lines @never_lines ||= lines.select(&:never?) end |
#no_branches? ⇒ Boolean
120 121 122 |
# File 'lib/simplecov/source_file.rb', line 120 def no_branches? total_branches.empty? end |
#no_lines? ⇒ Boolean
107 108 109 |
# File 'lib/simplecov/source_file.rb', line 107 def no_lines? lines.empty? || (lines.length == never_lines.size) end |
#not_loaded? ⇒ Boolean
Whether this file was added via cover / track_files but never
loaded during the run.
185 186 187 |
# File 'lib/simplecov/source_file.rb', line 185 def not_loaded? !@loaded end |
#project_filename ⇒ String
Project-relative path with no leading separator (e.g. "lib/foo.rb").
34 35 36 |
# File 'lib/simplecov/source_file.rb', line 34 def project_filename @filename.delete_prefix(SimpleCov.root).sub(%r{\A[/\\]}, "") end |
#relevant_lines ⇒ Integer
111 112 113 |
# File 'lib/simplecov/source_file.rb', line 111 def relevant_lines lines.size - never_lines.size - skipped_lines.size end |
#skipped_lines ⇒ Array[Line]
Returns all lines that were skipped as SimpleCov::SourceFile::Line instances
83 84 85 |
# File 'lib/simplecov/source_file.rb', line 83 def skipped_lines @skipped_lines ||= lines.select(&:skipped?) end |
#src ⇒ Array[String] Also known as: source
Source lines, read lazily.
40 41 42 |
# File 'lib/simplecov/source_file.rb', line 40 def src @src ||= SourceLoader.call(filename) end |
#total_branches ⇒ Array[Branch]
All relevant (covered + missed) branches, not a count.
132 133 134 |
# File 'lib/simplecov/source_file.rb', line 132 def total_branches @total_branches ||= covered_branches + missed_branches end |