Skip to content

Commit 5572900

Browse files
committed
Improve expect_s3_class() documentation
Fixes #2401
1 parent e1b3833 commit 5572900

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

R/expect-inheritance.R

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@
1818
#' See [expect_vector()] for testing properties of objects created by vctrs.
1919
#'
2020
#' @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.
2431
#' @inheritParams expect_that
2532
#' @family expectations
2633
#' @examples
@@ -31,6 +38,15 @@
3138
#' # A data frame is built from a list:
3239
#' expect_type(x, "list")
3340
#'
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+
#'
3450
#' # An integer vector is an atomic vector of type "integer"
3551
#' expect_type(x$x, "integer")
3652
#' # It is not an S3 object
@@ -66,8 +82,8 @@ expect_type <- function(object, type) {
6682
#' @export
6783
#' @rdname inheritance-expectations
6884
#' @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`.
7187
expect_s3_class <- function(object, class, exact = FALSE) {
7288
act <- quasi_label(enquo(object))
7389
act$class <- format_class(class(act$val))
@@ -175,15 +191,16 @@ isS3 <- function(x) is.object(x) && !isS4(x)
175191
#'
176192
#' `expect_is()` is an older form that uses [inherits()] without checking
177193
#' 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.
180196
#'
181197
#' @section 3rd edition:
182198
#' `r lifecycle::badge("deprecated")`
183199
#'
184200
#' `expect_is()` is formally deprecated in the 3rd edition.
185201
#'
186202
#' @keywords internal
203+
#' @param class Class name passed to `inherits()`.
187204
#' @inheritParams expect_type
188205
#' @export
189206
expect_is <- function(object, class, info = NULL, label = NULL) {

man/expect_is.Rd

Lines changed: 3 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/inheritance-expectations.Rd

Lines changed: 21 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)