Class Rcov::CodeCoverageAnalyzer
In: lib/rcov.rb
Parent: DifferentialAnalyzer

A CodeCoverageAnalyzer is responsible for tracing code execution and returning code coverage and execution count information.

Note that you must require ‘rcov‘ before the code you want to analyze is parsed (i.e. before it gets loaded or required). You can do that by either invoking ruby with the -rrcov command-line option or just:

 require 'rcov'
 require 'mycode'
 # ....

Example

 analyzer = Rcov::CodeCoverageAnalyzer.new
 analyzer.run_hooked do
   do_foo
   # all the code executed as a result of this method call is traced
 end
 # ....

 analyzer.run_hooked do
   do_bar
   # the code coverage information generated in this run is aggregated
   # to the previously recorded one
 end

 analyzer.analyzed_files   # => ["foo.rb", "bar.rb", ... ]
 lines, marked_info, count_info = analyzer.data("foo.rb")

In this example, two pieces of code are monitored, and the data generated in both runs are aggregated. lines is an array of strings representing the source code of foo.rb. marked_info is an array holding false, true values indicating whether the corresponding lines of code were reported as executed by Ruby. count_info is an array of integers representing how many times each line of code has been executed (more precisely, how many events where reported by Ruby — a single line might correspond to several events, e.g. many method calls).

You can have several CodeCoverageAnalyzer objects at a time, and it is possible to nest the run_hooked / install_hook/remove_hook blocks: each analyzer will manage its data separately. Note however that no special provision is taken to ignore code executed "inside" the CodeCoverageAnalyzer class. At any rate this will not pose a problem since it‘s easy to ignore it manually: just don‘t do

  lines, coverage, counts = analyzer.data("/path/to/lib/rcov.rb")

if you‘re not interested in that information.

Methods

Public Class methods

Public Instance methods

Return an array with the names of the files whose code was executed inside the block given to run_hooked or between install_hook and remove_hook.

Return the available data about the requested file, or nil if none of its code was executed or it cannot be found. The return value is an array with three elements:

 lines, marked_info, count_info = analyzer.data("foo.rb")

lines is an array of strings representing the source code of foo.rb. marked_info is an array holding false, true values indicating whether the corresponding lines of code were reported as executed by Ruby. count_info is an array of integers representing how many times each line of code has been executed (more precisely, how many events where reported by Ruby — a single line might correspond to several events, e.g. many method calls).

The returned data corresponds to the aggregation of all the statistics collected in each run_hooked or install_hook/remove_hook runs. You can reset the data at any time with reset to start from scratch.

Data for the first file matching the given regexp. See data.

Start monitoring execution to gather code coverage and execution count information. Such data will be collected until remove_hook is called.

Use run_hooked instead if possible.

Stop collecting code coverage and execution count information. remove_hook will also stop collecting info if it is run inside a run_hooked block.

Remove the data collected so far. The coverage and execution count "history" will be erased, and further collection will start from scratch: no code is considered executed, and therefore all execution counts are 0. Right after reset, analyzed_files will return an empty array, and data(filename) will return nil.

Execute the code in the given block, monitoring it in order to gather information about which code was executed.

[Validate]