makeExpectation
is the internal function used to evaluate the result of a
check and turn it into an expectation
.
makeExceptionFunction
can be used to automatically create an expectation
function based on a check function (see example).
Usage
makeExpectation(x, res, info, label)
makeExpectationFunction(
check.fun,
c.fun = NULL,
use.namespace = 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.- info
[
character(1)
]
Seeexpect_that
- label
[
character(1)
]
Seeexpect_that
- 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
.- env
[
environment
]
The environment of the created function. Default is theparent.frame
.
Value
makeExpectation
invisibly returns the checked object.
makeExpectationFunction
returns a function
.
See also
Other CustomConstructors:
makeAssertion()
,
makeTest()
Examples
# Simple custom check function
checkFalse = function(x) if (!identical(x, FALSE)) "Must be FALSE" else TRUE
# Create the respective expect function
expect_false = function(x, info = NULL, label = vname(x)) {
res = checkFalse(x)
makeExpectation(x, res, info = info, label = label)
}
# Alternative: Automatically create such a function
expect_false = makeExpectationFunction(checkFalse)
print(expect_false)
#> function (x, info = NULL, label = vname(x))
#> {
#> if (missing(x))
#> stop(sprintf("Argument '%s' is missing", label))
#> res = checkFalse(x)
#> makeExpectation(x, res, info, label)
#> }
#> <environment: 0x55f7a0bcbb68>