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)
The source code for this file. Aliased as :source
The source code for this file. Aliased as :source
Public Class Methods
# File lib/simplecov/source_file.rb, line 79 def initialize(filename, coverage) @filename, @coverage = filename, coverage File.open(filename, "rb") { |f| @src = f.readlines } end
Public Instance Methods
Returns all covered lines as SimpleCov::SourceFile::Line
# File lib/simplecov/source_file.rb, line 139 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 110 def covered_percent return 100.0 if lines.length.zero? || lines.length == never_lines.count relevant_lines = lines.count - never_lines.count - skipped_lines.count if relevant_lines.zero? 0.0 else Float((covered_lines.count) * 100.0 / relevant_lines.to_f) end end
# File lib/simplecov/source_file.rb, line 120 def covered_strength return 0.0 if lines.length.zero? || lines.length == never_lines.count lines_strength = 0 lines.each do |c| lines_strength += c.coverage if c.coverage end effective_lines_count = Float(lines.count - never_lines.count - skipped_lines.count) if effective_lines_count.zero? 0.0 else strength = lines_strength / effective_lines_count round_float(strength, 1) end end
Access SimpleCov::SourceFile::Line source lines by line number
# File lib/simplecov/source_file.rb, line 105 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 86 def lines return @lines if defined? @lines # Warning to identify condition from Issue #56 if coverage.size > src.size $stderr.puts "Warning: coverage data provided by Coverage [#{coverage.size}] exceeds number of lines in #{filename} [#{src.size}]" end # Initialize lines @lines = [] src.each_with_index do |src, i| @lines << SimpleCov::SourceFile::Line.new(src, i + 1, coverage[i]) end process_skipped_lines! @lines end
Returns the number of relevant lines (covered + missed)
# File lib/simplecov/source_file.rb, line 161 def lines_of_code covered_lines.count + missed_lines.count 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 145 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 151 def never_lines @never_lines ||= lines.select(&:never?) 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 167 def process_skipped_lines! skipping = false lines.each do |line| if line.src =~ /^([\s]*)#([\s]*)(\:#{SimpleCov.nocov_token}\:)/ skipping = !skipping else line.skipped! if skipping end end end
Returns all lines that were skipped as SimpleCov::SourceFile::Line instances
# File lib/simplecov/source_file.rb, line 156 def skipped_lines @skipped_lines ||= lines.select(&:skipped?) end
Private Instance Methods
ruby 1.9 could use Float#round(places) instead @return [Float]
# File lib/simplecov/source_file.rb, line 182 def round_float(float, places) factor = Float(10 * places) Float((float * factor).round / factor) end