Validation - Environment
test_environment.RdTest if an object is an environment.
test_environment() is the predicate test, while assert_environment()
validates its input, aborting if it fails the test.
Usage
test_environment(
x,
len = NULL,
env_has = NULL,
env_sees = NULL,
parents = NULL,
namespace = NULL,
sentinels = NULL,
custom = NULL,
custom_map = NULL
)
assert_environment(
x,
len = NULL,
env_has = NULL,
env_sees = NULL,
parents = NULL,
namespace = NULL,
sentinels = NULL,
custom = NULL,
custom_map = 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.- len
[
numeric()|\(){}|NULL] Possible values for the length. 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.
- env_has, env_sees
[
character()|NULL] Symbol names that must exist directly inx, or inherited from one of its parents, respectively (seerlang::env_has()). Set toNULLto not test.- parents
[
environment()|list()|NULL] Environment or list of environments to test as parents ofx. If a single environment is supplied, tests ifxinherits from it. If a list is supplied, tests ifrlang::env_parents()matches the list identically. Set toNULLto not test.- namespace
[
TRUE|FALSE|NULL] Test ifxis a namespace environment viarlang::is_namespace(). 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.- custom_map
[
function(x)|NULL] A custom function that is applied to each element ofx. Must return a singleTRUEorFALSEfor each element. 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_environment().[
=x]invisible(x)forassert_environment(), or aborts if the test fails.
Examples
x <- rlang::new_environment(list(a = 1, b = 2), parent = rlang::global_env())
args <- list(
len = c(1, 5), # Length (number of bindings) must be between 1 and 5 (will pass)
env_has = c("a", "b"), # Environment must directly bind "a" and "b" (will pass)
env_sees = "__x__", # Environment or its parents must see symbol "__x__" (will fail)
parents = rlang::global_env(),
# Must inherit from the global environment (will pass)
namespace = TRUE, # Must be a package namespace (will fail)
sentinels = c("null"), # Allow NULL environment (not the case of x)
custom = NULL, # Not test
custom_map = \(val) is.numeric(val)
# All binding values must be numeric (will pass)
)
do.call(test_environment, c(list(x), args)) #> FALSE (not all tests passed)
#> [1] FALSE
try(do.call(assert_environment, c(list(x), args, short_circuit = FALSE))) #> Error
#> Error in eval(expr, envir) : `x` failed `assert_environment()`:
#> ✔ (pass) sentinels: no sentinel values allowed.
#> ✔ (pass) type : must be of type "environment".
#> ✔ (pass) len : length must be in range 1 to 5.
#> ✖ (fail) namespace:
#> ✔ (pass) parents: must be child of specific parents.
#> ✔ (pass) env_has: must contain "a" and "b".
#> ✖ (fail) env_sees: must contain or inherit "__x__". Is missing "__x__".
#> ✔ (pass) custom_map: all elements must pass a custom test.
#>
#> ℹ See `predicater::assert_environment()` and this condition's `rs_assert_error`
#> attribute for details.