Validation - Object-related attributes
test_object.RdTest if an input is an 'object' (see predicates-objects), and fits a
specific Object-Oriented (OO) system (see object_system()).
test_object() is the predicate test, while assert_object() validates
its input, aborting if it fails the test.
Usage
test_object(
x,
oo_system = NULL,
s4_bit = NULL,
tests_class = NULL,
sentinels = NULL,
custom = NULL
)
assert_object(
x,
oo_system = NULL,
s4_bit = NULL,
tests_class = 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.- oo_system
[
character(1)|NULL] Expected Object-Oriented system name, passed toobject_system(). Set toNULLto not test.- s4_bit
[
TRUE|FALSE|NULL] Whether the underlying S4 object bit must (TRUE) or mustn't be set. Set toNULLto not test.- tests_class
[
list] A list of additional class tests passed directly totest_class().- 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_object().[
=x]invisible(x)forassert_object(), or aborts if the test fails.
Examples
x <- structure(
list(a = 1),
class = c("my_s3_class")
)
args <- list(
oo_system = "S3", # Must be an S3 object (will pass)
s4_bit = FALSE, # Object S4 bit must not be set (will pass)
tests_class = list(
classes = list(all = c("my_s3_class", "another_class"))
),
# Class vector must contain all specified classes (will fail)
sentinels = c("null"), # Allow NULL/unclassed objects (not the case of x)
custom = \(x) is_list(x) # Underlying object structure must be a list (will pass)
)
do.call(test_object, c(list(x), args)) #> FALSE (not all tests passed)
#> [1] FALSE
try(do.call(assert_object, c(list(x), args, short_circuit = FALSE))) #> Error
#> Error in eval(expr, envir) : `x` failed `assert_object()`:
#> ✔ (pass) sentinels: no sentinel values allowed.
#> ✔ (pass) type : must have a consistent class (see
#> `predicater::is_object_like()`).
#> ✔ (pass) oo_system: must belong to the "S3" object system.
#> ✔ (pass) s4_bit: S4 bit must be "FALSE".
#> ✖ (fail) tests_class: must pass the custom `predicater::test_class()` test.
#> Failed "classes".
#> ✔ (pass) custom: must pass a custom test.
#>
#> ℹ See `predicater::assert_object()` and this condition's `rs_assert_error`
#> attribute for details.