| testex {testex} | R Documentation |
A syntactic helper for writing quick and easy example tests
Description
A wrapper around stopifnot that allows you to use . to refer to
.Last.value and preserve the last non-test output from an example.
Usage
testex(
...,
srcref = NULL,
example_srcref = NULL,
value = get_example_value(),
envir = parent.frame(),
style = "standalone"
)
Arguments
... |
Expressions to evaluated. |
srcref |
An option |
example_srcref |
An option |
value |
A value to test against. By default, this will use the example's
|
envir |
An environment in which tests should be evaluated. By default the parent environment where tests are evaluated. |
style |
A syntactic style used by the test. Defaults to |
Value
Invisibly returns the .Last.value as it existed prior to evaluating
the test.
Documenting with testex
testex is a simple wrapper around execution that propagates the
.Last.value returned before running, allowing you to chain tests
more easily.
Use in Rd files:
\examples{
f <- function(a, b) a + b
f(3, 4)
\testonly{
testex::testex(
is.numeric(.),
identical(., 7)
)
}
}
But Rd files are generally regarded as being a bit cumbersome to author
directly. Instead, testex provide helpers that generate this style of
documentation, which use this function internally.
Use with roxygen2
Within a roxygen2 @examples block you can instead use the @test tag
which will generate Rd code as shown above.
#' @examples #' f <- function(a, b) a + b #' f(3, 4) #' @test is.numeric(.) #' @test identical(., 7)