Class: SimpleCov::SourceFile

### Home Classes Methods

### In Files

- lib/simplecov/source_file.rb

### Parent

Object

### Namespace

- CLASS SimpleCov::SourceFile::Line

### Methods

- ::new
- #covered_lines
- #covered_percent
- #covered_strength
- #line
- #lines
- #lines_of_code
- #missed_lines
- #never_lines
- #process_skipped_lines!
- #skipped_lines
- #source_lines

### Class/Module Index

Quicksearch

- SimpleCov
- SimpleCov::ArrayFilter
- SimpleCov::ArrayMergeHelper
- SimpleCov::BlockFilter
- SimpleCov::CommandGuesser
- SimpleCov::Configuration
- SimpleCov::ExitCodes
- SimpleCov::FileList
- SimpleCov::Filter
- SimpleCov::Formatter
- SimpleCov::Formatter::MultiFormatter
- SimpleCov::Formatter::SimpleFormatter
- SimpleCov::HashMergeHelper
- SimpleCov::LastRun
- SimpleCov::Profiles
- SimpleCov::Railtie
- SimpleCov::Result
- SimpleCov::ResultMerger
- SimpleCov::SourceFile
- SimpleCov::SourceFile::Line
- SimpleCov::StringFilter
- SimpleCov::VERSION
- Coverage

No matching classes.

# SimpleCov::SourceFile

Representation of a source file including it's coverage data, source code, source lines and featuring helpers to interpret that data.

### Attributes

coverage[R]

The array of coverage data received from the Coverage.result

filename[R]

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

source[R]

The source code for this file. Aliased as :source

src[R]

The source code for this file. Aliased as :source

### Public Class Methods

new(filename, coverage) click to toggle source

    # 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

### Public Instance Methods

covered_lines() click to toggle source

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

    # File lib/simplecov/source_file.rb, line 139
    def covered_lines
      @covered_lines ||= lines.select(&:covered?)
    end

covered_percent() click to toggle source

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

    # File lib/simplecov/source_file.rb, line 110
    def covered_percent
      return 100.0 if lines.length.zero? || lines.length == never_lines.count
      relevant_lines = lines.count - never_lines.count - skipped_lines.count
      if relevant_lines.zero?
        0.0
      else
        Float((covered_lines.count) * 100.0 / relevant_lines.to_f)
      end
    end

covered_strength() click to toggle source

    # File lib/simplecov/source_file.rb, line 120
    def covered_strength
      return 0.0 if lines.length.zero? || 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.zero?
        0.0
      else
        strength = lines_strength / effective_lines_count
        round_float(strength, 1)
      end
    end

line(number) click to toggle source

Access SimpleCov::SourceFile::Line source lines by line number

    # File lib/simplecov/source_file.rb, line 105
    def line(number)
      lines[number - 1]
    end

lines() click to toggle source

Returns all source lines for this file as instances of SimpleCov::SourceFile::Line, and thus including coverage data. Aliased as :source_lines

    # 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

Also aliased as: source_lines
lines_of_code() click to toggle source

Returns the number of relevant lines (covered + missed)

    # File lib/simplecov/source_file.rb, line 161
    def lines_of_code
      covered_lines.count + missed_lines.count
    end

missed_lines() click to toggle source

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

    # File lib/simplecov/source_file.rb, line 145
    def missed_lines
      @missed_lines ||= lines.select(&:missed?)
    end

never_lines() click to toggle source

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

    # File lib/simplecov/source_file.rb, line 151
    def never_lines
      @never_lines ||= lines.select(&:never?)
    end

process_skipped_lines!() click to toggle source

Will go through all source files and mark lines that are wrapped within # :nocov: comment blocks as skipped.

    # 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_lines() click to toggle source

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

    # File lib/simplecov/source_file.rb, line 156
    def skipped_lines
      @skipped_lines ||= lines.select(&:skipped?)
    end

source_lines() click to toggle source
Alias for: lines

[Validate]

Generated with the Darkfish Rdoc Generator 2.