class SimpleCov::SourceFile
Representation of a source file including it's coverage data, source code, source lines and featuring helpers to interpret that data.
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
# File lib/simplecov/source_file.rb, line 79 def initialize(filename, coverage) @filename = filename @coverage = coverage end
Public Instance Methods
# File lib/simplecov/source_file.rb, line 104 def build_lines coverage_exceeding_source_warn if coverage.size > src.size lines = src.map.with_index(1) do |src, i| SimpleCov::SourceFile::Line.new(src, i, coverage[i - 1]) end process_skipped_lines(lines) end
Warning to identify condition from Issue #56
# File lib/simplecov/source_file.rb, line 115 def coverage_exceeding_source_warn $stderr.puts "Warning: coverage data provided by Coverage [#{coverage.size}] exceeds number of lines in #{filename} [#{src.size}]" end
Returns all covered lines as SimpleCov::SourceFile::Line
# File lib/simplecov/source_file.rb, line 152 def covered_lines @covered_lines ||= lines.select(&:covered?) end
The coverage for this file in percent. 0 if the file has no relevant lines
# File lib/simplecov/source_file.rb, line 125 def covered_percent return 100.0 if no_lines? return 0.0 if relevant_lines.zero? Float(covered_lines.size * 100.0 / relevant_lines.to_f) end
# File lib/simplecov/source_file.rb, line 133 def covered_strength return 0.0 if relevant_lines.zero? round_float(lines_strength / relevant_lines.to_f, 1) end
Access SimpleCov::SourceFile::Line source lines by line number
# File lib/simplecov/source_file.rb, line 120 def line(number) lines[number - 1] end
Returns all source lines for this file as instances of SimpleCov::SourceFile::Line, and thus including coverage data. Aliased as :source_lines
# File lib/simplecov/source_file.rb, line 99 def lines @lines ||= build_lines end
Returns the number of relevant lines (covered + missed)
# File lib/simplecov/source_file.rb, line 174 def lines_of_code covered_lines.size + missed_lines.size end
# File lib/simplecov/source_file.rb, line 143 def lines_strength lines.map(&:coverage).compact.reduce(:+) end
Returns all lines that should have been, but were not covered as instances of SimpleCov::SourceFile::Line
# File lib/simplecov/source_file.rb, line 158 def missed_lines @missed_lines ||= lines.select(&:missed?) end
Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances
# File lib/simplecov/source_file.rb, line 164 def never_lines @never_lines ||= lines.select(&:never?) end
# File lib/simplecov/source_file.rb, line 139 def no_lines? lines.length.zero? || (lines.length == never_lines.size) end
Will go through all source files and mark lines that are wrapped within # :nocov: comment blocks as skipped.
# File lib/simplecov/source_file.rb, line 180 def process_skipped_lines(lines) skipping = false lines.each do |line| if SimpleCov::LinesClassifier.no_cov_line?(line.src) skipping = !skipping line.skipped! elsif skipping line.skipped! end end end
The path to this source file relative to the projects directory
# File lib/simplecov/source_file.rb, line 85 def project_filename @filename.sub(Regexp.new("^#{Regexp.escape(SimpleCov.root)}"), "") end
# File lib/simplecov/source_file.rb, line 147 def relevant_lines lines.size - never_lines.size - skipped_lines.size end
Returns all lines that were skipped as SimpleCov::SourceFile::Line instances
# File lib/simplecov/source_file.rb, line 169 def skipped_lines @skipped_lines ||= lines.select(&:skipped?) end
The source code for this file. Aliased as :source
# File lib/simplecov/source_file.rb, line 90 def src # We intentionally read source code lazily to # suppress reading unused source code. @src ||= File.open(filename, "rb", &:readlines) end
Private Instance Methods
ruby 1.9 could use Float#round(places) instead @return [Float]
# File lib/simplecov/source_file.rb, line 197 def round_float(float, places) factor = Float(10 * places) Float((float * factor).round / factor) end