|
18 | 18 | #' See [expect_vector()] for testing properties of objects created by vctrs. |
19 | 19 | #' |
20 | 20 | #' @param type String giving base type (as returned by [typeof()]). |
21 | | -#' @param class Either a character vector of class names, or |
22 | | -#' for `expect_s3_class()` and `expect_s4_class()`, an `NA` to assert |
23 | | -#' that `object` isn't an S3 or S4 object. |
| 21 | +#' @param class |
| 22 | +#' |
| 23 | +#' * `expect_type()` a single string giving an R base type. |
| 24 | +#' * `expect_s3_class()` a character vector of class names or `NA` to assert |
| 25 | +#' that `object` isn't an S3 object. If you provide multiple class names, |
| 26 | +#' the test will pass if `object` inherits from any of them, unless |
| 27 | +#' `exact = TRUE`. |
| 28 | +#' * `expect_s4_class()` a character vector of class names or `NA` to assert |
| 29 | +#' that `object` isn't an S4 object. |
| 30 | +#' * `expect_s7_class()` an [S7::S7_class()] object. |
24 | 31 | #' @inheritParams expect_that |
25 | 32 | #' @family expectations |
26 | 33 | #' @examples |
|
31 | 38 | #' # A data frame is built from a list: |
32 | 39 | #' expect_type(x, "list") |
33 | 40 | #' |
| 41 | +#' f <- factor(c("a", "b", "c")) |
| 42 | +#' o <- ordered(f) |
| 43 | +#' |
| 44 | +#' # Using multiple class names tests if the object inherits from any of them |
| 45 | +#' expect_s3_class(f, c("ordered", "factor")) |
| 46 | +#' # Use exact = TRUE to test for exact match |
| 47 | +#' show_failure(expect_s3_class(f, c("ordered", "factor"), exact = TRUE)) |
| 48 | +#' expect_s3_class(o, c("ordered", "factor"), exact = TRUE) |
| 49 | +#' |
34 | 50 | #' # An integer vector is an atomic vector of type "integer" |
35 | 51 | #' expect_type(x$x, "integer") |
36 | 52 | #' # It is not an S3 object |
@@ -66,8 +82,8 @@ expect_type <- function(object, type) { |
66 | 82 | #' @export |
67 | 83 | #' @rdname inheritance-expectations |
68 | 84 | #' @param exact If `FALSE`, the default, checks that `object` inherits |
69 | | -#' from `class`. If `TRUE`, checks that object has a class that's identical |
70 | | -#' to `class`. |
| 85 | +#' from any element of `class`. If `TRUE`, checks that object has a class |
| 86 | +#' that's identical to `class`. |
71 | 87 | expect_s3_class <- function(object, class, exact = FALSE) { |
72 | 88 | act <- quasi_label(enquo(object)) |
73 | 89 | act$class <- format_class(class(act$val)) |
@@ -175,15 +191,16 @@ isS3 <- function(x) is.object(x) && !isS4(x) |
175 | 191 | #' |
176 | 192 | #' `expect_is()` is an older form that uses [inherits()] without checking |
177 | 193 | #' whether `x` is S3, S4, or neither. Instead, I'd recommend using |
178 | | -#' [expect_type()], [expect_s3_class()] or [expect_s4_class()] to more clearly |
179 | | -#' convey your intent. |
| 194 | +#' [expect_type()], [expect_s3_class()], or [expect_s4_class()] to more clearly |
| 195 | +#' convey your intent. |
180 | 196 | #' |
181 | 197 | #' @section 3rd edition: |
182 | 198 | #' `r lifecycle::badge("deprecated")` |
183 | 199 | #' |
184 | 200 | #' `expect_is()` is formally deprecated in the 3rd edition. |
185 | 201 | #' |
186 | 202 | #' @keywords internal |
| 203 | +#' @param class Class name passed to `inherits()`. |
187 | 204 | #' @inheritParams expect_type |
188 | 205 | #' @export |
189 | 206 | expect_is <- function(object, class, info = NULL, label = NULL) { |
|
0 commit comments