count_tests: Number of Regression Tests in Source Code

View source: R/count_tests.R

count_testsR Documentation

Number of Regression Tests in Source Code

Description

Count the number of calls to stopifnot at the top level of an R script file.

Usage

count_tests(file, encoding, keep.source = getOption("keep.source"))

Arguments

file

a connection object or a character.

encoding

encoding to be assumed for input strings.

keep.source

a logical value; if TRUE, keep source reference information.

Details

A test is an R expression included in either of these forms of stopifnot usage: stopifnot(A, B), or stopifnot(exprs= {A ; B}). Usage as stopifnot(exprObject = Obj) is not supported.

Calls to stopifnot in functions are ignored. Calls inside a group are also currently omitted.

Value

A number of tests.

Examples

## Keep parse data in non interactive sessions.
if (!interactive())
    op <- options(keep.source = TRUE)

fil <- tempfile(fileext = ".R")
cat("stopifnot(1 == 1)",
    "stopifnot(2 == 2, 3 == 3)",
    "stopifnot(exprs = {",
    "    4 == 4",
    "    5 == 5",
    "    6 == 6",
    "})",
    "## 'stopifnot' in a function is ignored",
    "f <- function() stopifnot(6 == 6)",
    file = fil, sep = "\n")
count_tests(fil)

roger documentation built on Oct. 15, 2026, 1:07 a.m.

Related to count_tests in roger...