|
12 | 12 | #' * `expect_s4_class(x, class)` checks that `x` is an S4 object that |
13 | 13 | #' [is()] `class`. |
14 | 14 | #' * `expect_s4_class(x, NA)` checks that `x` isn't an S4 object. |
| 15 | +#' * `expect_r6_class(x, class)` checks that `x` an R6 object that |
| 16 | +#' inherits from `class`. |
15 | 17 | #' * `expect_s7_class(x, Class)` checks that `x` is an S7 object that |
16 | 18 | #' [S7::S7_inherits()] from `Class` |
17 | 19 | #' |
18 | 20 | #' See [expect_vector()] for testing properties of objects created by vctrs. |
19 | 21 | #' |
20 | 22 | #' @param type String giving base type (as returned by [typeof()]). |
21 | | -#' @param class |
22 | | -#' * `expect_type()`: a single string giving an R base type. |
23 | | -#' * `expect_s3_class()`: a character vector of class names or `NA` to assert |
24 | | -#' that `object` isn't an S3 object. If you provide multiple class names, |
25 | | -#' the test will pass if `object` inherits from any of them, unless |
26 | | -#' `exact = TRUE`. |
27 | | -#' * `expect_s4_class()`: a character vector of class names or `NA` to assert |
28 | | -#' that `object` isn't an S4 object. |
29 | | -#' * `expect_s7_class()`: an [S7::S7_class()] object. |
| 23 | +#' @param class The required type varies depending on the function: |
| 24 | +#' * `expect_type()`: a string. |
| 25 | +#' * `expect_s3_class()`: a string or character vector. The behaviour of |
| 26 | +#' multiple values (i.e. a character vector) is controlled by the |
| 27 | +#' `exact` argument. |
| 28 | +#' * `expect_s4_class()`: a string. |
| 29 | +#' * `expect_r6_class()`: a string. |
| 30 | +#' * `expect_s7_class()`: an [S7::S7_class()] object. |
| 31 | +#' |
| 32 | +#' For historical reasons, `expect_s3_class()` and `expect_s4_class()` also |
| 33 | +#' take `NA` to assert that the `object` is not an S3 or S4 object. |
30 | 34 | #' @inheritParams expect_that |
31 | 35 | #' @family expectations |
32 | 36 | #' @examples |
@@ -82,7 +86,7 @@ expect_type <- function(object, type) { |
82 | 86 | #' @rdname inheritance-expectations |
83 | 87 | #' @param exact If `FALSE`, the default, checks that `object` inherits |
84 | 88 | #' from any element of `class`. If `TRUE`, checks that object has a class |
85 | | -#' that's identical to `class`. |
| 89 | +#' that exactly matches `class`. |
86 | 90 | expect_s3_class <- function(object, class, exact = FALSE) { |
87 | 91 | check_bool(exact) |
88 | 92 |
|
@@ -154,6 +158,26 @@ expect_s4_class <- function(object, class) { |
154 | 158 | pass(act$val) |
155 | 159 | } |
156 | 160 |
|
| 161 | +#' @export |
| 162 | +#' @rdname inheritance-expectations |
| 163 | +expect_r6_class <- function(object, class) { |
| 164 | + act <- quasi_label(enquo(object)) |
| 165 | + check_string(class) |
| 166 | + |
| 167 | + if (!inherits(act$val, "R6")) { |
| 168 | + return(fail(sprintf("%s is not an R6 object.", act$lab))) |
| 169 | + } |
| 170 | + |
| 171 | + if (!inherits(act$val, class)) { |
| 172 | + act_class <- format_class(class(act$val)) |
| 173 | + exp_class <- format_class(class) |
| 174 | + msg <- sprintf("%s inherits from %s not %s.", act$lab, act_class, exp_class) |
| 175 | + return(fail(msg)) |
| 176 | + } |
| 177 | + |
| 178 | + pass(act$val) |
| 179 | +} |
| 180 | + |
157 | 181 | #' @export |
158 | 182 | #' @rdname inheritance-expectations |
159 | 183 | expect_s7_class <- function(object, class) { |
|
0 commit comments