Representation of a source file including it's coverage data, source code, source lines and featuring helpers to interpret that data.
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
# File lib/simplecov/source_file.rb, line 80 def initialize(filename, coverage) @filename, @coverage = filename, coverage File.open(filename, "r:UTF-8") {|f| @src = f.readlines } end
Returns all covered lines as SimpleCov::SourceFile::Line
# File lib/simplecov/source_file.rb, line 140 def covered_lines @covered_lines ||= lines.select {|c| c.covered? } end
The coverage for this file in percent. 0 if the file has no relevant lines
# File lib/simplecov/source_file.rb, line 111 def covered_percent return 100.0 if lines.length == 0 or lines.length == never_lines.count relevant_lines = lines.count - never_lines.count - skipped_lines.count if relevant_lines == 0 0 else (covered_lines.count) * 100 / relevant_lines.to_f end end
# File lib/simplecov/source_file.rb, line 121 def covered_strength return 0 if lines.length == 0 or lines.length == never_lines.count lines_strength = 0 lines.each do |c| lines_strength += c.coverage if c.coverage end effective_lines_count = (lines.count - never_lines.count - skipped_lines.count).to_f if effective_lines_count == 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 106 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 87 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 162 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 146 def missed_lines @missed_lines ||= lines.select {|c| c.missed? } end
Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances
# File lib/simplecov/source_file.rb, line 152 def never_lines @never_lines ||= lines.select {|c| c.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 168 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 157 def skipped_lines @skipped_lines ||= lines.select {|c| c.skipped? } end
ruby 1.9 could use Float#round(places) instead
# File lib/simplecov/source_file.rb, line 182 def round_float(float, places) factor = (10 * places).to_f (float * factor).round / factor end