Class: SimpleCov::SourceFile
- Inherits:
-
Object
- Object
- SimpleCov::SourceFile
- Defined in:
- lib/simplecov/source_file.rb
Overview
Representation of a source file including it’s coverage data, source code, source lines and featuring helpers to interpret that data.
Defined Under Namespace
Classes: Line
Instance Attribute Summary collapse
-
#coverage ⇒ Object
readonly
The array of coverage data received from the Coverage.result.
-
#filename ⇒ Object
readonly
The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb).
-
#src ⇒ Object
(also: #source)
readonly
The source code for this file.
Instance Method Summary collapse
-
#covered_lines ⇒ Object
Returns all covered lines as SimpleCov::SourceFile::Line.
-
#covered_percent ⇒ Object
The coverage for this file in percent.
- #covered_strength ⇒ Object
-
#initialize(filename, coverage) ⇒ SourceFile
constructor
A new instance of SourceFile.
-
#line(number) ⇒ Object
Access SimpleCov::SourceFile::Line source lines by line number.
-
#lines ⇒ Object
(also: #source_lines)
Returns all source lines for this file as instances of SimpleCov::SourceFile::Line, and thus including coverage data.
-
#lines_of_code ⇒ Object
Returns the number of relevant lines (covered + missed).
-
#missed_lines ⇒ Object
Returns all lines that should have been, but were not covered as instances of SimpleCov::SourceFile::Line.
-
#never_lines ⇒ Object
Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances.
-
#process_skipped_lines! ⇒ Object
Will go through all source files and mark lines that are wrapped within # :nocov: comment blocks as skipped.
-
#skipped_lines ⇒ Object
Returns all lines that were skipped as SimpleCov::SourceFile::Line instances.
Constructor Details
#initialize(filename, coverage) ⇒ SourceFile
Returns a new instance of SourceFile.
79 80 81 82 |
# File 'lib/simplecov/source_file.rb', line 79 def initialize(filename, coverage) @filename, @coverage = filename, coverage File.open(filename, "r:UTF-8") {|f| @src = f.readlines } end |
Instance Attribute Details
#coverage ⇒ Object (readonly)
The array of coverage data received from the Coverage.result
74 75 76 |
# File 'lib/simplecov/source_file.rb', line 74 def coverage @coverage end |
#filename ⇒ Object (readonly)
The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb)
72 73 74 |
# File 'lib/simplecov/source_file.rb', line 72 def filename @filename end |
#src ⇒ Object (readonly) Also known as: source
The source code for this file. Aliased as :source
76 77 78 |
# File 'lib/simplecov/source_file.rb', line 76 def src @src end |
Instance Method Details
#covered_lines ⇒ Object
Returns all covered lines as SimpleCov::SourceFile::Line
127 128 129 |
# File 'lib/simplecov/source_file.rb', line 127 def covered_lines @covered_lines ||= lines.select {|c| c.covered? } end |
#covered_percent ⇒ Object
The coverage for this file in percent. 0 if the file has no relevant lines
110 111 112 113 |
# File 'lib/simplecov/source_file.rb', line 110 def covered_percent return 100.0 if lines.length == 0 or lines.length == never_lines.count (covered_lines.count) * 100 / (lines.count - never_lines.count - skipped_lines.count).to_f end |
#covered_strength ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/simplecov/source_file.rb', line 115 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 strength = lines_strength / effective_lines_count round_float(strength, 1) end |
#line(number) ⇒ Object
Access SimpleCov::SourceFile::Line source lines by line number
105 106 107 |
# File 'lib/simplecov/source_file.rb', line 105 def line(number) lines[number-1] end |
#lines ⇒ Object Also known as: source_lines
Returns all source lines for this file as instances of SimpleCov::SourceFile::Line, and thus including coverage data. Aliased as :source_lines
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/simplecov/source_file.rb', line 86 def lines return @lines unless @lines.nil? # 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 |
#lines_of_code ⇒ Object
Returns the number of relevant lines (covered + missed)
149 150 151 |
# File 'lib/simplecov/source_file.rb', line 149 def lines_of_code covered_lines.count + missed_lines.count end |
#missed_lines ⇒ Object
Returns all lines that should have been, but were not covered as instances of SimpleCov::SourceFile::Line
133 134 135 |
# File 'lib/simplecov/source_file.rb', line 133 def missed_lines @missed_lines ||= lines.select {|c| c.missed? } end |
#never_lines ⇒ Object
Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances
139 140 141 |
# File 'lib/simplecov/source_file.rb', line 139 def never_lines @never_lines ||= lines.select {|c| c.never? } end |
#process_skipped_lines! ⇒ Object
Will go through all source files and mark lines that are wrapped within # :nocov: comment blocks as skipped.
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/simplecov/source_file.rb', line 155 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 |
#skipped_lines ⇒ Object
Returns all lines that were skipped as SimpleCov::SourceFile::Line instances
144 145 146 |
# File 'lib/simplecov/source_file.rb', line 144 def skipped_lines @skipped_lines ||= lines.select {|c| c.skipped? } end |