Class: SimpleCov::SourceFile
| Relationships & Source Files | |
| Namespace Children | |
|
Modules:
| |
|
Classes:
| |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Instance Chain:
self,
BuilderContext
|
|
| Inherits: | Object |
| Defined in: | lib/simplecov/source_file.rb, lib/simplecov/source_file/branch.rb, lib/simplecov/source_file/branch_builder.rb, lib/simplecov/source_file/builder_context.rb, lib/simplecov/source_file/line.rb, lib/simplecov/source_file/line_builder.rb, lib/simplecov/source_file/method.rb, lib/simplecov/source_file/method_builder.rb, lib/simplecov/source_file/ruby_data_parser.rb, lib/simplecov/source_file/skip_chunks.rb, lib/simplecov/source_file/source_loader.rb, lib/simplecov/source_file/statistics.rb |
Overview
Representation of a source file including it's coverage data, source code, source lines and featuring helpers to interpret that data.
Class Method Summary
Instance Attribute Summary
-
#coverage_data
readonly
The array of coverage data received from the
Coverage.result -
#filename
readonly
The full path to this source file (e.g.
- #no_branches? ⇒ Boolean readonly
- #no_lines? ⇒ Boolean readonly
-
#not_loaded? ⇒ Boolean
readonly
Whether this file was added via track_files but never loaded/required.
Instance Method Summary
-
#branches
Return all the branches inside current source file.
-
#branches_coverage_percent
DEPRECATED: use
covered_percent(:branch). - #branches_for_line(line_number)
-
#branches_report
Return hash with key of line number and branch coverage count as value.
-
#coverage_statistics(criterion = nil)
Returns a hash keyed by every supported coverage criterion.
-
#covered_branches
Select the covered branches.
-
#covered_lines
Returns all covered lines as
Line - #covered_methods
-
#covered_percent(criterion = :line)
The coverage for this file in percent, for the given criterion (line by default).
- #covered_strength(criterion = :line)
-
#line(number)
Access
Linesource lines by line number. -
#line_with_missed_branch?(line_number) ⇒ Boolean
Check if any branches missing on given line number.
-
#lines
(also: #source_lines)
Returns all source lines for this file as instances of
Line, and thus including coverage data. -
#lines_of_code
Returns the number of relevant lines (covered + missed).
-
#methods
Return all methods detected in this source file.
-
#methods_coverage_percent
DEPRECATED: use
covered_percent(:method). -
#missed_branches
Select the missed branches with coverage equal to zero.
-
#missed_lines
Returns all lines that should have been, but were not covered as instances of
Line - #missed_methods
-
#never_lines
Returns all lines that are not relevant for coverage as
Lineinstances. -
#project_filename
The path to this source file relative to the projects directory.
- #relevant_lines
-
#skipped_lines
Returns all lines that were skipped as
Lineinstances. -
#source
Alias for #src.
-
#source_lines
Alias for #lines.
-
#src
(also: #source)
The source code for this file.
-
#total_branches
Return the relevant branches to source file.
BuilderContext - Included
| #real_source_positions | Memoized set of real source positions (branch start lines, method name+line pairs) extracted via Prism. |
| #skip_chunks_for | Skip-chunk lookup for the named criterion ( |
Constructor Details
.new(filename, coverage_data, loaded: true) ⇒ SourceFile
# 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 (readonly)
The array of coverage data received from the Coverage.result
# File 'lib/simplecov/source_file.rb', line 25
attr_reader :coverage_data
#filename (readonly)
The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb)
# File 'lib/simplecov/source_file.rb', line 23
attr_reader :filename
#no_branches? ⇒ Boolean (readonly)
[ GitHub ]
# File 'lib/simplecov/source_file.rb', line 120
def no_branches? total_branches.empty? end
#no_lines? ⇒ Boolean (readonly)
[ GitHub ]
# File 'lib/simplecov/source_file.rb', line 107
def no_lines? lines.empty? || (lines.length == never_lines.size) end
#not_loaded? ⇒ Boolean (readonly)
Whether this file was added via track_files but never loaded/required.
# File 'lib/simplecov/source_file.rb', line 185
def not_loaded? !@loaded end
Instance Method Details
#branches
Return all the branches inside current source file
# File 'lib/simplecov/source_file.rb', line 116
def branches @branches ||= BranchBuilder.new(self).call end
#branches_coverage_percent
DEPRECATED: use covered_percent(:branch).
# File 'lib/simplecov/source_file.rb', line 125
def branches_coverage_percent warn "#{Kernel.caller.first}: [DEPRECATION] `SimpleCov::SourceFile#branches_coverage_percent` is deprecated. " \ "Use `covered_percent(:branch)`." covered_percent(:branch) end
#branches_for_line(line_number)
[ GitHub ]# File 'lib/simplecov/source_file.rb', line 155
def branches_for_line(line_number) branches_report.fetch(line_number, []) end
#branches_report
Return hash with key of line number and branch coverage count as value
# 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(criterion = nil)
Returns a hash keyed by every supported coverage criterion. Each
value is a CoverageStatistics, even for criteria that weren't
enabled during the run — those collapse to 0/0/0. Consumers
(FileList, formatters) decide which keys to surface based on
SimpleCov.coverage_criterion_enabled?.
The per-criterion coverage statistics for this file. With no argument
returns the {line:, branch:, method:} Hash; pass a criterion symbol
(:line / :branch / :method) to get that one CoverageStatistics.
# File 'lib/simplecov/source_file.rb', line 53
def coverage_statistics(criterion = nil) @coverage_statistics ||= Statistics.new(self).call criterion ? @coverage_statistics[criterion] : @coverage_statistics end
#covered_branches
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.
# File 'lib/simplecov/source_file.rb', line 146
def covered_branches @covered_branches ||= branches.select(&:covered?) end
#covered_lines
Returns all covered lines as SourceFile::Line
# File 'lib/simplecov/source_file.rb', line 66
def covered_lines @covered_lines ||= lines.select(&:covered?) end
#covered_methods
[ GitHub ]# File 'lib/simplecov/source_file.rb', line 169
def covered_methods @covered_methods ||= methods.select(&:covered?) end
#covered_percent(criterion = :line)
The coverage for this file in percent, for the given criterion (line by default). Returns nil if the criterion was not measured.
# File 'lib/simplecov/source_file.rb', line 99
def covered_percent(criterion = :line) coverage_statistics(criterion)&.percent end
#covered_strength(criterion = :line)
[ GitHub ]# File 'lib/simplecov/source_file.rb', line 103
def covered_strength(criterion = :line) coverage_statistics(criterion)&.strength end
#line(number)
Access SourceFile::Line source lines by line number
# 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
# 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 Also known as: #source_lines
Returns all source lines for this file as instances of SourceFile::Line,
and thus including coverage data. Aliased as :source_lines
# File 'lib/simplecov/source_file.rb', line 60
def lines @lines ||= LineBuilder.new(self).call end
#lines_of_code
Returns the number of relevant lines (covered + missed)
# File 'lib/simplecov/source_file.rb', line 88
def lines_of_code coverage_statistics[:line]&.total || 0 end
#methods
Return all methods detected in this source file
# File 'lib/simplecov/source_file.rb', line 165
def methods @methods ||= MethodBuilder.new(self).call end
#methods_coverage_percent
DEPRECATED: use covered_percent(:method).
# File 'lib/simplecov/source_file.rb', line 178
def methods_coverage_percent warn "#{Kernel.caller.first}: [DEPRECATION] `SimpleCov::SourceFile#methods_coverage_percent` is deprecated. " \ "Use `covered_percent(:method)`." covered_percent(:method) end
#missed_branches
Select the missed branches with coverage equal to zero
# File 'lib/simplecov/source_file.rb', line 151
def missed_branches @missed_branches ||= branches.select(&:missed?) end
#missed_lines
Returns all lines that should have been, but were not covered
as instances of SourceFile::Line
# File 'lib/simplecov/source_file.rb', line 72
def missed_lines @missed_lines ||= lines.select(&:missed?) end
#missed_methods
[ GitHub ]# File 'lib/simplecov/source_file.rb', line 173
def missed_methods @missed_methods ||= methods.select(&:missed?) end
#never_lines
Returns all lines that are not relevant for coverage as
SourceFile::Line instances
# File 'lib/simplecov/source_file.rb', line 78
def never_lines @never_lines ||= lines.select(&:never?) end
#project_filename
The path to this source file relative to the projects directory
#relevant_lines
[ GitHub ]# File 'lib/simplecov/source_file.rb', line 111
def relevant_lines lines.size - never_lines.size - skipped_lines.size end
#skipped_lines
Returns all lines that were skipped as SourceFile::Line instances
# File 'lib/simplecov/source_file.rb', line 83
def skipped_lines @skipped_lines ||= lines.select(&:skipped?) end
#source
Alias for #src.
# File 'lib/simplecov/source_file.rb', line 43
alias source src
#source_lines
Alias for #lines.
# File 'lib/simplecov/source_file.rb', line 63
alias source_lines lines
#src Also known as: #source
The source code for this file. Aliased as :source.
Intentionally read lazily to suppress reading unused source code.
# File 'lib/simplecov/source_file.rb', line 40
def src @src ||= SourceLoader.call(filename) end
#total_branches
Return the relevant branches to source file
# File 'lib/simplecov/source_file.rb', line 132
def total_branches @total_branches ||= covered_branches + missed_branches end