Class: Rcov::FileStatistics

Class | Rcov::FileStatistics
In: | lib/rcov.rb
Parent: | Object

A FileStatistics object associates a filename to:

1. its source code
2. the per-line coverage information after correction using rcov‘s heuristics
3. the per-line execution counts

A FileStatistics object can be therefore be built given the filename, the associated source code, and an array holding execution counts (i.e. how many times each line has been executed).

FileStatistics is relatively intelligent: it handles normal comments, `=begin/=end`, heredocs, many multiline-expressions… It uses a number of heuristics to determine what is code and what is a comment, and to refine the initial (incomplete) coverage information.

Basic usage is as follows:

     sf = FileStatistics.new("foo.rb", ["puts 1", "if true &&", "   false",
                                    "puts 2", "end"],  [1, 1, 0, 0, 0])
     sf.num_lines        # => 5
     sf.num_code_lines   # => 5
     sf.coverage[2]      # => true
     sf.coverage[3]      # => :inferred
     sf.code_coverage    # => 0.6

The array of strings representing the source code and the array of execution counts would normally be obtained from a Rcov::CodeCoverageAnalyzer.

### Methods

code_coverage is_code? merge new num_code_lines num_lines total_coverage

### Attributes

counts | [R] |
coverage | [R] |
lines | [R] |
name | [R] |

### Public Class methods

new(name, lines, counts, comments_run_by_default = false)

### Public Instance methods

code_coverage()

Code coverage rate: fraction of lines of code executed, relative to the total amount of lines of code (loc). Returns a float from 0 to 1.0.

is_code?(lineno)

Returns true if the given line number corresponds to code, as opposed to a comment (either # or =begin/=end blocks).

merge(lines, coverage, counts)

Merge code coverage and execution count information. As for code coverage, a line will be considered

- covered for sure (true) if it is covered in either `self` or in the `coverage` array
- considered `:inferred` if the neither `self` nor the `coverage` array indicate that it was definitely executed, but it was `inferred` in either one
- not covered (`false`) if it was uncovered in both

Execution counts are just summated on a per-line basis.

num_code_lines()

Number of lines of code (loc).

num_lines()

Total number of lines.

total_coverage()

Total coverage rate if comments are also considered "executable", given as a fraction, i.e. from 0 to 1.0. A comment is attached to the code following it (RDoc-style): it will be considered executed if the the next statement was executed.

[Validate]