Turn a Check into an Expectation

Share:

Description

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

1
2
3
makeExpectation(x, res, info, label)

makeExpectationFunction(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.

info

[character(1)]
See expect_that

label

[character(1)]
See expect_that

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 native symbol, see .Call. Useful if check.fun is just a simple wrapper.

env

[environment]
The environment of the created function. Default is the parent.frame.

Value

makeExpectation invisibly returns the checked object. makeExpectationFunction returns a function.

See Also

Other CustomConstructors: makeAssertion, makeTest

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 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)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.