Class: SimpleCov::SourceFile

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(filename, coverage) ⇒ SourceFile

Returns a new instance of SourceFile.



78
79
80
81
# File 'lib/simplecov/source_file.rb', line 78

def initialize(filename, coverage)
  @filename = filename
  @coverage = coverage
end

Instance Attribute Details

#coverageObject (readonly)

The array of coverage data received from the Coverage.result



76
77
78
# File 'lib/simplecov/source_file.rb', line 76

def coverage
  @coverage
end

#filenameObject (readonly)

The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb)



74
75
76
# File 'lib/simplecov/source_file.rb', line 74

def filename
  @filename
end

Instance Method Details

#build_linesObject



98
99
100
101
102
103
104
105
106
# File 'lib/simplecov/source_file.rb', line 98

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_warnObject

Warning to identify condition from Issue #56



109
110
111
# File 'lib/simplecov/source_file.rb', line 109

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_linesObject

Returns all covered lines as SimpleCov::SourceFile::Line



146
147
148
# File 'lib/simplecov/source_file.rb', line 146

def covered_lines
  @covered_lines ||= lines.select(&:covered?)
end

#covered_percentObject

The coverage for this file in percent. 0 if the file has no relevant lines



119
120
121
122
123
124
125
# File 'lib/simplecov/source_file.rb', line 119

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_strengthObject



127
128
129
130
131
# File 'lib/simplecov/source_file.rb', line 127

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



114
115
116
# File 'lib/simplecov/source_file.rb', line 114

def line(number)
  lines[number - 1]
end

#linesObject 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



93
94
95
# File 'lib/simplecov/source_file.rb', line 93

def lines
  @lines ||= build_lines
end

#lines_of_codeObject

Returns the number of relevant lines (covered + missed)



168
169
170
# File 'lib/simplecov/source_file.rb', line 168

def lines_of_code
  covered_lines.size + missed_lines.size
end

#lines_strengthObject



137
138
139
# File 'lib/simplecov/source_file.rb', line 137

def lines_strength
  lines.map(&:coverage).compact.reduce(:+)
end

#missed_linesObject

Returns all lines that should have been, but were not covered as instances of SimpleCov::SourceFile::Line



152
153
154
# File 'lib/simplecov/source_file.rb', line 152

def missed_lines
  @missed_lines ||= lines.select(&:missed?)
end

#never_linesObject

Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances



158
159
160
# File 'lib/simplecov/source_file.rb', line 158

def never_lines
  @never_lines ||= lines.select(&:never?)
end

#no_lines?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/simplecov/source_file.rb', line 133

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.



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/simplecov/source_file.rb', line 174

def process_skipped_lines(lines)
  skipping = false

  lines.each do |line|
    if line.src =~ /^([\s]*)#([\s]*)(\:#{SimpleCov.nocov_token}\:)/
      skipping = !skipping
      line.skipped!
    elsif skipping
      line.skipped!
    end
  end
end

#relevant_linesObject



141
142
143
# File 'lib/simplecov/source_file.rb', line 141

def relevant_lines
  lines.size - never_lines.size - skipped_lines.size
end

#skipped_linesObject

Returns all lines that were skipped as SimpleCov::SourceFile::Line instances



163
164
165
# File 'lib/simplecov/source_file.rb', line 163

def skipped_lines
  @skipped_lines ||= lines.select(&:skipped?)
end

#srcObject Also known as: source

The source code for this file. Aliased as :source



84
85
86
87
88
# File 'lib/simplecov/source_file.rb', line 84

def src
  # We intentionally read source code lazily to
  # suppress reading unused source code.
  @src ||= File.open(filename, "rb", &:readlines)
end