Validation - Code Objects
test_code.RdTest if an input is a R language code object (symbol, language/call, or syntactic literal), and optionally check if it is valid parsable code or an empty symbol.
test_code() is the predicate test, while assert_code() validates
its input, aborting if it fails the test.
Usage
test_code(
x,
sym = TRUE,
lang = TRUE,
literal = TRUE,
valid = NULL,
empty = NULL,
sentinels = NULL,
custom = NULL
)
assert_code(
x,
sym = TRUE,
lang = TRUE,
literal = TRUE,
valid = NULL,
empty = 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.- sym, lang, literal
[
TRUE|FALSE|NULL] Whether to allow symbols, language objects (calls), or syntactic literals.- valid
[
logical(1)|NULL] Whether the code object must be parsable code.- empty
[
logical(1)|NULL] Whether empty symbols are permitted.- 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_code().[
=x]invisible(x)forassert_code(), or aborts if the test fails.
Examples
x <- 42L
args <- list(
sym = FALSE, # Disallow symbols (will pass)
lang = FALSE, # Disallow language objects/calls (will pass)
literal = TRUE, # Allow syntactic literals (will pass)
valid = TRUE, # Must be parsable code (will pass)
empty = FALSE, # Disallow empty symbols (will pass)
sentinels = c("null"), # Allow NULL code object (not the case of x)
custom = \(x) x > 100 # Literal value must be > 100 (will fail)
)
rlang::exec(test_code, !!!c(list(x), args)) #> FALSE (not all tests passed)
#> [1] FALSE
try(rlang::exec(assert_code, !!!c(list(x), args, short_circuit = FALSE))) #> Error
#> Error in eval(expr, envir) : `42` failed `assert_code()`:
#> ✔ (pass) sentinels: no sentinel values allowed.
#> ✔ (pass) type : must be of 'type' "syntactic literal".
#> ✔ (pass) valid : must be valid code.
#> ✔ (pass) empty :
#> ✖ (fail) custom: must pass a custom test. Did not.
#>
#> ℹ See `predicater::assert_code()` and this condition's `rs_assert_error`
#> attribute for details.