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)
-
- (Object) coverage
readonly
The array of coverage data received from the Coverage.result.
-
- (Object) filename
readonly
The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb).
-
- (Object) src
(also: #source)
readonly
The source code for this file.
Instance Method Summary (collapse)
-
- (Object) covered_lines
Returns all covered lines as SimpleCov::SourceFile::Line.
-
- (Object) covered_percent
The coverage for this file in percent.
- - (Object) covered_strength
-
- (SourceFile) initialize(filename, coverage)
constructor
A new instance of SourceFile.
-
- (Object) line(number)
Access SimpleCov::SourceFile::Line source lines by line number.
-
- (Object) lines
(also: #source_lines)
Returns all source lines for this file as instances of SimpleCov::SourceFile::Line, and thus including coverage data.
-
- (Object) lines_of_code
Returns the number of relevant lines (covered + missed).
-
- (Object) missed_lines
Returns all lines that should have been, but were not covered as instances of SimpleCov::SourceFile::Line.
-
- (Object) never_lines
Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances.
-
- (Object) process_skipped_lines!
Will go through all source files and mark lines that are wrapped within # :nocov: comment blocks as skipped.
-
- (Object) skipped_lines
Returns all lines that were skipped as SimpleCov::SourceFile::Line instances.
Constructor Details
- (SourceFile) initialize(filename, coverage)
A new instance of SourceFile
80 81 82 83 |
# 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 |
Instance Attribute Details
- (Object) coverage (readonly)
The array of coverage data received from the Coverage.result
75 76 77 |
# File 'lib/simplecov/source_file.rb', line 75 def coverage @coverage end |
- (Object) filename (readonly)
The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb)
73 74 75 |
# File 'lib/simplecov/source_file.rb', line 73 def filename @filename end |
- (Object) src (readonly) Also known as: source
The source code for this file. Aliased as :source
77 78 79 |
# File 'lib/simplecov/source_file.rb', line 77 def src @src end |
Instance Method Details
- (Object) covered_lines
Returns all covered lines as SimpleCov::SourceFile::Line
140 141 142 |
# File 'lib/simplecov/source_file.rb', line 140 def covered_lines @covered_lines ||= lines.select {|c| c.covered? } end |
- (Object) covered_percent
The coverage for this file in percent. 0 if the file has no relevant lines
111 112 113 114 115 116 117 118 119 |
# 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 |
- (Object) covered_strength
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# 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 |
- (Object) line(number)
Access SimpleCov::SourceFile::Line source lines by line number
106 107 108 |
# File 'lib/simplecov/source_file.rb', line 106 def line(number) lines[number-1] end |
- (Object) lines 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# 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 |
- (Object) lines_of_code
Returns the number of relevant lines (covered + missed)
162 163 164 |
# File 'lib/simplecov/source_file.rb', line 162 def lines_of_code covered_lines.count + missed_lines.count end |
- (Object) missed_lines
Returns all lines that should have been, but were not covered as instances of SimpleCov::SourceFile::Line
146 147 148 |
# File 'lib/simplecov/source_file.rb', line 146 def missed_lines @missed_lines ||= lines.select {|c| c.missed? } end |
- (Object) never_lines
Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances
152 153 154 |
# File 'lib/simplecov/source_file.rb', line 152 def never_lines @never_lines ||= lines.select {|c| c.never? } end |
- (Object) process_skipped_lines!
Will go through all source files and mark lines that are wrapped within # :nocov: comment blocks as skipped.
168 169 170 171 172 173 174 175 176 177 |
# 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 |
- (Object) skipped_lines
Returns all lines that were skipped as SimpleCov::SourceFile::Line instances
157 158 159 |
# File 'lib/simplecov/source_file.rb', line 157 def skipped_lines @skipped_lines ||= lines.select {|c| c.skipped? } end |