class SimpleCov::SourceFile
Representation of a source file including it’s coverage data, source code, source lines and featuring helpers to interpret that data.
Constants
- RUBY_FILE_ENCODING_MAGIC_COMMENT_REGEX
- SHEBANG_REGEX
Attributes
The array of coverage data received from the Coverage.result
The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb)
Public Class Methods
Source
# File lib/simplecov/source_file.rb, line 14 def initialize(filename, coverage_data) @filename = filename @coverage_data = coverage_data end
Public Instance Methods
Source
# File lib/simplecov/source_file.rb, line 98 def branches @branches ||= build_branches end
Return all the branches inside current source file
Source
# File lib/simplecov/source_file.rb, line 106 def branches_coverage_percent coverage_statistics[:branch]&.percent end
Source
# File lib/simplecov/source_file.rb, line 142 def branches_for_line(line_number) branches_report.fetch(line_number, []) end
Source
# File lib/simplecov/source_file.rb, line 118 def branches_report @branches_report ||= build_branches_report end
Return hash with key of line number and branch coverage count as value
Source
# File lib/simplecov/source_file.rb, line 32 def coverage_statistics @coverage_statistics ||= { **line_coverage_statistics, **branch_coverage_statistics } end
Source
# File lib/simplecov/source_file.rb, line 129 def covered_branches @covered_branches ||= branches.select(&:covered?) end
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
@return [Array]
Source
# File lib/simplecov/source_file.rb, line 48 def covered_lines @covered_lines ||= lines.select(&:covered?) end
Returns all covered lines as SimpleCov::SourceFile::Line
Source
# File lib/simplecov/source_file.rb, line 80 def covered_percent coverage_statistics[:line]&.percent end
The coverage for this file in percent. 0 if the file has no coverage lines
Source
# File lib/simplecov/source_file.rb, line 84 def covered_strength coverage_statistics[:line]&.strength end
Source
# File lib/simplecov/source_file.rb, line 75 def line(number) lines[number - 1] end
Access SimpleCov::SourceFile::Line source lines by line number
Source
# 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
Check if any branches missing on given line number
@param [Integer] line_number
@return [Boolean]
Source
# File lib/simplecov/source_file.rb, line 42 def lines @lines ||= build_lines end
Returns all source lines for this file as instances of SimpleCov::SourceFile::Line, and thus including coverage data. Aliased as :source_lines
Source
# File lib/simplecov/source_file.rb, line 70 def lines_of_code coverage_statistics[:line]&.total end
Returns the number of relevant lines (covered + missed)
Source
# File lib/simplecov/source_file.rb, line 138 def missed_branches @missed_branches ||= branches.select(&:missed?) end
Select the missed branches with coverage equal to zero
@return [Array]
Source
# File lib/simplecov/source_file.rb, line 54 def missed_lines @missed_lines ||= lines.select(&:missed?) end
Returns all lines that should have been, but were not covered as instances of SimpleCov::SourceFile::Line
Source
# File lib/simplecov/source_file.rb, line 60 def never_lines @never_lines ||= lines.select(&:never?) end
Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances
Source
# File lib/simplecov/source_file.rb, line 102 def no_branches? total_branches.empty? end
Source
# File lib/simplecov/source_file.rb, line 88 def no_lines? lines.length.zero? || (lines.length == never_lines.size) end
Source
# File lib/simplecov/source_file.rb, line 20 def project_filename @filename.delete_prefix(SimpleCov.root) end
The path to this source file relative to the projects directory
Source
# File lib/simplecov/source_file.rb, line 92 def relevant_lines lines.size - never_lines.size - skipped_lines.size end
Source
# File lib/simplecov/source_file.rb, line 65 def skipped_lines @skipped_lines ||= lines.select(&:skipped?) end
Returns all lines that were skipped as SimpleCov::SourceFile::Line instances
Source
# 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
The source code for this file. Aliased as :source
Source
# File lib/simplecov/source_file.rb, line 112 def total_branches @total_branches ||= covered_branches + missed_branches end
Return the relevant branches to source file