makeAssertion
is the internal function used to evaluate the result of a
check and throw an exception if necessary.
makeAssertionFunction
can be used to automatically create an assertion
function based on a check function (see example).
Usage
makeAssertion(x, res, var.name, collection)
makeAssertionFunction(
check.fun,
c.fun = NULL,
use.namespace = TRUE,
coerce = FALSE,
env = parent.frame()
)
Arguments
- x
[any]
Object to check.- res
[
TRUE
|character(1)
]
The result of a check function:TRUE
for successful checks, and an error message as string otherwise.- var.name
[
character(1)
]
The custom name forx
as passed to anyassert*
function. Defaults to a heuristic name lookup.- collection
[
AssertCollection
]
If anAssertCollection
is provided, the error message is stored in it. IfNULL
, an exception is raised ifres
is notTRUE
.- check.fun
[
function
]
Function which checks the input. Must returnTRUE
on success and a string with the error message otherwise.- c.fun
[
character(1)
]
If notNULL
, instead of calling the functioncheck.fun
, use.Call
to call a C function “c.fun” with the identical set of parameters. The C function must be registered as a native symbol, see.Call
. Useful ifcheck.fun
is just a simple wrapper.- use.namespace
[
logical(1)
]
Call functions of checkmate using its namespace explicitly. Can be set toFALSE
so save some microseconds, but the checkmate package needs to be imported. Default isTRUE
.- coerce
[
logical(1)
]
IfTRUE
, injects some lines of code to convert numeric values to integer after an successful assertion. Currently used inassertCount
,assertInt
andassertIntegerish
.- env
[
environment
]
The environment of the created function. Default is theparent.frame
.
Value
makeAssertion
invisibly returns the checked object if the check was successful,
and an exception is raised (or its message stored in the collection) otherwise.
makeAssertionFunction
returns a function
.
See also
Other CustomConstructors:
makeExpectation()
,
makeTest()
Examples
# Simple custom check function
checkFalse = function(x) if (!identical(x, FALSE)) "Must be FALSE" else TRUE
# Create the respective assert function
assertFalse = function(x, .var.name = vname(x), add = NULL) {
res = checkFalse(x)
makeAssertion(x, res, .var.name, add)
}
# Alternative: Automatically create such a function
assertFalse = makeAssertionFunction(checkFalse)
print(assertFalse)
#> function (x, .var.name = checkmate::vname(x), add = NULL)
#> {
#> if (missing(x))
#> stop(sprintf("argument \"%s\" is missing, with no default",
#> .var.name))
#> res = checkFalse(x)
#> checkmate::makeAssertion(x, res, .var.name, add)
#> }
#> <environment: 0x55f7a1324a38>