Validation - Language
test_language.RdTest if an object is a call (language object).
test_language() is the predicate test, while assert_language() validates
its input, aborting if it fails the test.
Usage
test_language(
x,
name = NULL,
ns = NULL,
n_args = NULL,
arg_names = NULL,
simple = NULL,
valid = NULL,
sentinels = NULL,
custom = NULL
)
assert_language(
x,
name = NULL,
ns = NULL,
n_args = NULL,
arg_names = NULL,
simple = NULL,
valid = 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.- name, ns
[
character(1)|NULL] Expected function name and namespace of the call, viarlang::call_name()andrlang::call_ns(). Set toNULLto not test.- n_args
[
numeric()|\(){}|NULL] Possible values for the NA. The options of each argument 'arg' are:NULLto not test.A single non-negative number to test for
. == arg. IfInf,. == length(x).A single negative number to test for
. == length(x) + arg.A vector of two non-negative numbers to test for
arg[1] <= . <= arg[2](Infis allowed).A vector of three or more non-negative numbers to test for
. %in% arg.A function that receives the value to test and the length of
x, and returns a singleTRUEorFALSE.
- arg_names
[
character()|NULL] Expected exact names of the call arguments. Set toNULLto not test.- simple
[
TRUE|FALSE|NULL] Test if the call is simple viarlang::is_call_simple(). Set toNULLto not test.- valid
[
TRUE|FALSE|NULL] Test if the call is parsable. 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_language().[
=x]invisible(x)forassert_language(), or aborts if the test fails.
Examples
x <- quote(rlang::env(a = 1, b = 2))
args <- list(
name = "fn", # Function name must be "fn" (will pass)
ns = "otherpkg", # Namespace must be "otherpkg" (will fail)
n_args = c(1, 5), # Number of arguments must be between 1 and 5 (will pass)
arg_names = c("a", "b"), # Argument names must match exact character vector (will pass)
simple = FALSE, # Must not be a simple call without names/namespace (will pass)
valid = TRUE, # Must be a parsable, valid call (will pass)
sentinels = c("null"), # Allow NULL call (not the case of x)
custom = NULL # No custom test predicate
)
rlang::exec(test_language, !!!c(list(x), args)) #> FALSE (not all tests passed)
#> [1] FALSE
try(rlang::exec(assert_language, !!!c(list(x), args, short_circuit = FALSE))) #> Error
#> Error in eval(expr, envir) :
#> `.Primitive("quote")(rlang::env(a = 1, b = 2))` failed
#> `assert_language()`:
#> ✔ (pass) sentinels: no sentinel values allowed.
#> ✔ (pass) type : must be of type "language".
#> ✖ (fail) name : must have call name "fn". Was "env".
#> ✖ (fail) ns : must have call namespace "otherpkg". Was "rlang".
#> ✔ (pass) n_args: #of arguments must be in range 1 to 5.
#> ✔ (pass) arg_names: must have argument names: "a" and "b".
#> ✖ (fail) simple: must be not a simple call. Was simple.
#> ✔ (pass) valid : must be a valid call.
#>
#> ℹ See `predicater::assert_language()` and this condition's `rs_assert_error`
#> attribute for details.