checkmate (version 1.7.2)

makeAssertion: Turn a Check into an Assertion

Description

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, 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 for x as passed to any assert* function.
collection
[AssertCollection] If an AssertCollection is provided, the error message is stored in it. If NULL (default), an exception is r
check.fun
[function] Function which checks the input. Must return TRUE on success and a string with the error message otherwise.
c.fun
[character(1)] If not NULL, instead of calling the function check.fun, use .Call to call a C function c.fun with the identical set of parameters. The C function must be registered as a n
env
[environment] The environment of the created function. Default is the parent.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.

Examples

Run this code
# Simple custom check function
checkFalse = function(x) if (!identical(x, FALSE)) "Must be FALSE" else TRUE

# Create the respective assert function
assertFalse = function(x, add = NULL, .var.name) {
  res = checkFalse(x)
  makeAssertion(x, res, .var.name, add)
}

# Alternative: Automatically create such a function
assertFalse = makeAssertionFunction(checkFalse)
print(assertFalse)

Run the code above in your browser using DataCamp Workspace