Validation - Class attribute
test_class.RdTest if an object inherits from specific classes or has a valid class vector.
Invalid classes fail the test: non-character vectors, empty character
vectors, or a vector with NA values.
test_class() is the predicate test, while assert_class() validates
its input, aborting if it fails the test.
Usage
test_class(
x,
classes = NULL,
tests_char = NULL,
how = "class",
sentinels = NULL,
custom = NULL
)
assert_class(
x,
classes = NULL,
tests_char = NULL,
how = "class",
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.- classes
[
list()|character()|NULL] A named list specifying possible class inheritance criteria. The elements are the classes to test against, and the names are which test to do:"any"forrlang::inherits_any(),"all"forrlang::inherits_all(),"only"forrlang::inherits_only(), and"none"for!inherits_any(). If any of the test passes, the overall test passes. If a single character, it is tested withinherits_any(). Set toNULLto not test.- tests_char
[
list] A list of additional character tests passed totest_character().- how
[
"class"|"x"|"attr"] How to extract class names fortests_char:"x"forxdirectly;"class"forclass(); and"attr"forattr(x, "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_class().[
=x]invisible(x)forassert_class(), or aborts if the test fails.
Examples
x <- structure(
list(a = 1),
class = c("another_class", "custom_df", "data.frame")
)
args <- list(
classes = list(
all = c("custom_df", "data.frame"),
none = "matrix"
),
# Must inherit from both custom_df and data.frame, and not matrix (will pass)
tests_char = list(n_na = 0, n_dup = 0),
# Class names vector must contain no NAs or duplicates (will pass)
how = "class", # Extract class vector via `class(x)` (will pass)
sentinels = c("null"), # Allow NULL class attribute (not the case of x)
custom = \(x) has_dim(x)
# Object must have a dim() value (will fail)
)
do.call(test_class, c(list(x), args)) #> FALSE (not all tests passed)
#> [1] FALSE
try(do.call(assert_class, c(list(x), args, short_circuit = FALSE))) #> Error
#> Error in eval(expr, envir) : `x` failed `assert_class()`:
#> ✔ (pass) sentinels: no sentinel values allowed.
#> ✔ (pass) type : class must be a non-empty no-na character vector.
#> ✔ (pass) classes: class must satisfy class inheritance constraints.
#> ✔ (pass) tests_char: class must pass the specified character tests.
#> ✖ (fail) custom: must pass a custom test. Did not.
#>
#> ℹ See `predicater::assert_class()` and this condition's `rs_assert_error`
#> attribute for details.