
Do you expect an S3/S4/R6/S7 object that inherits from this class?
Source:R/expect-inheritance.R
inheritance-expectations.RdSee https://adv-r.hadley.nz/oo.html for an overview of R's OO systems, and the vocabulary used here.
expect_type(x, type)checks thattypeof(x)istype.expect_s3_class(x, class)checks thatxis an S3 object thatinherits()fromclassexpect_s3_class(x, NA)checks thatxisn't an S3 object.expect_s4_class(x, class)checks thatxis an S4 object thatis()class.expect_s4_class(x, NA)checks thatxisn't an S4 object.expect_r6_class(x, class)checks thatxan R6 object that inherits fromclass.expect_s7_class(x, Class)checks thatxis an S7 object thatS7::S7_inherits()fromClass
See expect_vector() for testing properties of objects created by vctrs.
Usage
expect_type(object, type)
expect_s3_class(object, class, exact = FALSE)
expect_s4_class(object, class)
expect_r6_class(object, class)
expect_s7_class(object, class)Arguments
- object
Object to test.
Supports limited unquoting to make it easier to generate readable failures within a function or for loop. See quasi_label for more details.
- type
String giving base type (as returned by
typeof()).- class
The required type varies depending on the function:
expect_type(): a string.expect_s3_class(): a string or character vector. The behaviour of multiple values (i.e. a character vector) is controlled by theexactargument.expect_s4_class(): a string.expect_r6_class(): a string.expect_s7_class(): anS7::S7_class()object.
For historical reasons,
expect_s3_class()andexpect_s4_class()also takeNAto assert that theobjectis not an S3 or S4 object.- exact
If
FALSE, the default, checks thatobjectinherits from any element ofclass. IfTRUE, checks that object has a class that exactly matchesclass.
Examples
x <- data.frame(x = 1:10, y = "x", stringsAsFactors = TRUE)
# A data frame is an S3 object with class data.frame
expect_s3_class(x, "data.frame")
show_failure(expect_s4_class(x, "data.frame"))
#> Failed expectation:
#> Expected `x` to be an S4 object.
#> Actual OO type: S3.
# A data frame is built from a list:
expect_type(x, "list")
f <- factor(c("a", "b", "c"))
o <- ordered(f)
# Using multiple class names tests if the object inherits from any of them
expect_s3_class(f, c("ordered", "factor"))
# Use exact = TRUE to test for exact match
show_failure(expect_s3_class(f, c("ordered", "factor"), exact = TRUE))
#> Failed expectation:
#> Expected `f` to have class "ordered"/"factor".
#> Actual class: "factor".
expect_s3_class(o, c("ordered", "factor"), exact = TRUE)
# An integer vector is an atomic vector of type "integer"
expect_type(x$x, "integer")
# It is not an S3 object
show_failure(expect_s3_class(x$x, "integer"))
#> Failed expectation:
#> Expected `x$x` to be an S3 object.
#> Actual OO type: none.
# Above, we requested data.frame() converts strings to factors:
show_failure(expect_type(x$y, "character"))
#> Failed expectation:
#> Expected `x$y` to have type "character".
#> Actual type: "integer"
expect_s3_class(x$y, "factor")
expect_type(x$y, "integer")