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.


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

def initialize(filename, coverage)
  @filename, @coverage = filename, coverage
  File.open(filename, "rb") {|f| @src = f.readlines }
end

Instance Attribute Details

#coverageObject (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

#filenameObject (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

#srcObject (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_linesObject

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


139
140
141
# File 'lib/simplecov/source_file.rb', line 139

def covered_lines
  @covered_lines ||= lines.select {|c| c.covered? }
end

#covered_percentObject

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


110
111
112
113
114
115
116
117
118
# File 'lib/simplecov/source_file.rb', line 110

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.0
  else
    Float((covered_lines.count) * 100.0 / relevant_lines.to_f)
  end
end

#covered_strengthObject


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/simplecov/source_file.rb', line 120

def covered_strength
  return 0.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 = Float(lines.count - never_lines.count - skipped_lines.count)

  if effective_lines_count == 0
    0.0
  else
    strength = lines_strength / effective_lines_count
    round_float(strength, 1)
  end
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

#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


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

#lines_of_codeObject

Returns the number of relevant lines (covered + missed)


161
162
163
# File 'lib/simplecov/source_file.rb', line 161

def lines_of_code
  covered_lines.count + missed_lines.count
end

#missed_linesObject

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


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

def missed_lines
  @missed_lines ||= lines.select {|c| c.missed? }
end

#never_linesObject

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


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

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.


167
168
169
170
171
172
173
174
175
176
# File 'lib/simplecov/source_file.rb', line 167

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_linesObject

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


156
157
158
# File 'lib/simplecov/source_file.rb', line 156

def skipped_lines
  @skipped_lines ||= lines.select {|c| c.skipped? }
end