Validation - Function
test_function.RdTest if an object is a function, with options to inspect function type, argument names, environments, S3 generics, and methods.
Usage
test_function(
x,
mode = "function",
args_names = NULL,
fn_env = NULL,
dots = NULL,
sentinels = NULL,
custom = NULL
)
assert_function(
x,
mode = "function",
args_names = NULL,
fn_env = NULL,
dots = NULL,
sentinels = NULL,
custom = NULL,
action = "abort",
env = caller_env(),
x_name = NULL,
short_circuit = TRUE,
report_untested = TRUE,
args_cnd = list()
)Arguments
- x
[
any] An object to test.- mode
[
"function"|"closure"|"builtin"|"special"|"primitive"|NULL] Expected function type(s) out of"function"(any function),"closure","builtin","special", or"primitive"(any of the former two). Set toNULLto not test.- args_names
[
character()|NULL] Expected exact argument names of the function. Set toNULLto not test.- fn_env
[
environment|NULL] Expected environment of the function. Checks iffn_env(x)is identical toenvor inherits from it viarlang::env_inherits(). Set toNULLto not test.- dots
[
TRUE|FALSE|NULL] Test if the function has...in its arguments. Set toNULLto not test.- sentinels
[
character()|NULL] Each entry in this character vector allowsxto also be some scalar sentinel below. Set toNULLto disconsider sentinels."null"forNULL."empty"for any zero-length object."na"for anyNAtype, or"na_logical"forNA,"na_integer"forNA_integer_,"na_real"forNA_real_,"na_complex"forNA_complex_, and"na_character"forNA_character_."nan"forNaN."+inf"for+Inf,"-inf"for-Inf, and"inf"for both."true"/"t"forTRUE, and"false"/"f"forFALSE.
- custom
[
function(x)|NULL] A custom function that takesxas first argument and returns a singleTRUEorFALSE. Set toNULLto not test.- action
[
"abort"|"warn"|"inform"] Action to take when the test fails:"abort"to stop execution and throw an error."warning"to issue a warning and returninvisible(x)."message"to issue a message and returninvisible(x).
- env
[
environment()|call()|NULL|missing_arg()] The call to inform as the origin of the error, passed torlang::abort():An environment in the call stack or a hard-coded defused call.
NULLfor no information.missing_arg()to use the assert function itself.The default is
caller_env(), to display the function where the assertion was called.
- x_name
[
character(1)|NULL] The name of the object to use in the error message. IfNULL, the name is inferred from the expression passed tox.- short_circuit
[
TRUE|FALSE] IfTRUE, the tests results will be reported up to the first failure. Else, all tests results are reported. The former is more efficient, while the latter is more informative.- report_untested
[
TRUE|FALSE] IfTRUE, the tests that were not run due to short- circuiting will be reported as untested, else, ignored.- args_cnd
[
list()] Additional arguments passed tocli::cli_abort(),cli::cli_warn(), orcli::cli_inform(), based on the chosenaction.
Value
[
TRUE|FALSE] fortest_function().[
=x]invisible(x)forassert_function(), or aborts if the test fails.
Examples
x <- function(a, b = 1, ...) {
a + b
}
args <- list(
mode = "closure", # Must be a standard closure function (will pass)
args_names = c("a", "b"), # Exact argument names must match (will fail)
fn_env = rlang::global_env(),
# Function environment must equal or inherit from global env (will pass)
dots = TRUE, # Function must accept `...` in arguments (will pass)
sentinels = c("null"), # Allow NULL function (not the case of x)
custom = NULL # No custom predicate
)
do.call(test_function, c(list(x), args)) #> FALSE (not all tests passed)
#> [1] FALSE
try(do.call(assert_function, c(list(x), args, short_circuit = FALSE))) #> Error
#> Error in eval(expr, envir) : `x` failed `assert_function()`:
#> ✔ (pass) sentinels: no sentinel values allowed.
#> ✔ (pass) type : must be of type "closure".
#> ✖ (fail) args_names: must have argument names "a" and "b". Had "a", "b", and
#> "...".
#> ✔ (pass) fn_env: must have the expected environment.
#> ✔ (pass) dots : must accept `...`.
#>
#> ℹ See `predicater::assert_function()` and this condition's `rs_assert_error`
#> attribute for details.