Learn R Programming

covr (version 4.0.0)

tally_coverage: Tally coverage by line, expression, function or branch

Description

Tally coverage by line, expression, function or branch

Usage

tally_coverage(x, by = c("line", "expression", "function", "branch"))

Value

A data.frame. With by = "expression" it is the same as as.data.frame.coverage(). With by = "line" it has one row for each function on each line of code (excluding blank lines, comments and lines with only punctuation such as }) and the columns filename, functions, line and value, where value is the smallest execution count of the expressions on that line, so a line only counts as covered if all its expressions were run. An expression spanning several lines, such as a loop, only counts for the lines that none of the expressions inside it are on. percent_coverage(), report() and the other line based outputs count each line once, with the smallest count of its functions.

With by = "function" it has one row for each top level R function and the columns filename, functions, first_line, last_line and value, where first_line and last_line span the function's traced code and value is the largest execution count of its expressions, so a function counts as covered if any of its code was run. Compiled code has no function names, so it is left out.

With by = "branch" it has one row for each branch measured with options(covr.branch_coverage = TRUE) (see covr.branch_coverage), and the columns filename, functions, line and column (where the if

or switch() starts), branch (the index of the branch in its if or switch()), label and value, the number of times the branch was taken. It has no rows if branch coverage wasn't measured.

Arguments

x

the coverage object returned from package_coverage()

by

whether to tally coverage by line, expression, function or branch

Examples

Run this code
cov <- code_coverage(
  "f <- function(x) {\n  if (x) 'yes' else 'no'\n}\ng <- function() 1",
  "f(TRUE)"
)
tally_coverage(cov)
tally_coverage(cov, by = "expression")
tally_coverage(cov, by = "function")

Run the code above in your browser using DataLab