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).
Instance Method Summary collapse
- #build_lines ⇒ Object
-
#coverage_exceeding_source_warn ⇒ Object
Warning to identify condition from Issue #56.
-
#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).
- #lines_strength ⇒ Object
-
#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.
- #no_lines? ⇒ Boolean
-
#process_skipped_lines(lines) ⇒ Object
Will go through all source files and mark lines that are wrapped within # :nocov: comment blocks as skipped.
-
#project_filename ⇒ Object
The path to this source file relative to the projects directory.
- #relevant_lines ⇒ Object
-
#skipped_lines ⇒ Object
Returns all lines that were skipped as SimpleCov::SourceFile::Line instances.
-
#src ⇒ Object
(also: #source)
The source code for this file.
Constructor Details
#initialize(filename, coverage) ⇒ SourceFile
Returns a new instance of SourceFile.
80 81 82 83 |
# File 'lib/simplecov/source_file.rb', line 80 def initialize(filename, coverage) @filename = filename @coverage = coverage end |
Instance Attribute Details
#coverage ⇒ Object (readonly)
The array of coverage data received from the Coverage.result
78 79 80 |
# File 'lib/simplecov/source_file.rb', line 78 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)
76 77 78 |
# File 'lib/simplecov/source_file.rb', line 76 def filename @filename end |
Instance Method Details
#build_lines ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/simplecov/source_file.rb', line 105 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 |
#coverage_exceeding_source_warn ⇒ Object
Warning to identify condition from Issue #56
116 117 118 |
# File 'lib/simplecov/source_file.rb', line 116 def coverage_exceeding_source_warn $stderr.puts "Warning: coverage data provided by Coverage [#{coverage.size}] exceeds number of lines in #{filename} [#{src.size}]" end |
#covered_lines ⇒ Object
Returns all covered lines as SimpleCov::SourceFile::Line
153 154 155 |
# File 'lib/simplecov/source_file.rb', line 153 def covered_lines @covered_lines ||= lines.select(&:covered?) end |
#covered_percent ⇒ Object
The coverage for this file in percent. 0 if the file has no relevant lines
126 127 128 129 130 131 132 |
# File 'lib/simplecov/source_file.rb', line 126 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 |
#covered_strength ⇒ Object
134 135 136 137 138 |
# File 'lib/simplecov/source_file.rb', line 134 def covered_strength return 0.0 if relevant_lines.zero? round_float(lines_strength / relevant_lines.to_f, 1) end |
#line(number) ⇒ Object
Access SimpleCov::SourceFile::Line source lines by line number
121 122 123 |
# File 'lib/simplecov/source_file.rb', line 121 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
100 101 102 |
# File 'lib/simplecov/source_file.rb', line 100 def lines @lines ||= build_lines end |
#lines_of_code ⇒ Object
Returns the number of relevant lines (covered + missed)
175 176 177 |
# File 'lib/simplecov/source_file.rb', line 175 def lines_of_code covered_lines.size + missed_lines.size end |
#lines_strength ⇒ Object
144 145 146 |
# File 'lib/simplecov/source_file.rb', line 144 def lines_strength lines.map(&:coverage).compact.reduce(:+) end |
#missed_lines ⇒ Object
Returns all lines that should have been, but were not covered as instances of SimpleCov::SourceFile::Line
159 160 161 |
# File 'lib/simplecov/source_file.rb', line 159 def missed_lines @missed_lines ||= lines.select(&:missed?) end |
#never_lines ⇒ Object
Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances
165 166 167 |
# File 'lib/simplecov/source_file.rb', line 165 def never_lines @never_lines ||= lines.select(&:never?) end |
#no_lines? ⇒ Boolean
140 141 142 |
# File 'lib/simplecov/source_file.rb', line 140 def no_lines? lines.length.zero? || (lines.length == never_lines.size) end |
#process_skipped_lines(lines) ⇒ Object
Will go through all source files and mark lines that are wrapped within # :nocov: comment blocks as skipped.
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/simplecov/source_file.rb', line 181 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 |
#project_filename ⇒ Object
The path to this source file relative to the projects directory
86 87 88 |
# File 'lib/simplecov/source_file.rb', line 86 def project_filename @filename.sub(Regexp.new("^#{Regexp.escape(SimpleCov.root)}"), "") end |
#relevant_lines ⇒ Object
148 149 150 |
# File 'lib/simplecov/source_file.rb', line 148 def relevant_lines lines.size - never_lines.size - skipped_lines.size end |
#skipped_lines ⇒ Object
Returns all lines that were skipped as SimpleCov::SourceFile::Line instances
170 171 172 |
# File 'lib/simplecov/source_file.rb', line 170 def skipped_lines @skipped_lines ||= lines.select(&:skipped?) end |
#src ⇒ Object Also known as: source
The source code for this file. Aliased as :source
91 92 93 94 95 |
# File 'lib/simplecov/source_file.rb', line 91 def src # We intentionally read source code lazily to # suppress reading unused source code. @src ||= File.open(filename, "rb", &:readlines) end |