Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggestion: include examples of expect_s3_class() with exact = TRUE #2041

Open
zkamvar opened this issue Dec 12, 2024 · 0 comments
Open

suggestion: include examples of expect_s3_class() with exact = TRUE #2041

zkamvar opened this issue Dec 12, 2024 · 0 comments

Comments

@zkamvar
Copy link

zkamvar commented Dec 12, 2024

I recently discovered that my mental model for inherits() was not quite correct where I had assumed that the classes specified would be a subset of the classes that the object had instead of checking if the object had any of the classes specified.

It turns out that this is not an uncommon assumption as there are more than a few examples of R packages that test for the presence of multiple s3 classes without specifying exact = TRUE, which can lead to the side-effect of a test passing even though there is a class association missing, (which we found was a risk for us in hubverse-org/hubValidations#180)

Here's my suggestion for the examples to highlight this:

library(testthat)
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")
# The default checks that _any_ classes match, so be judicious
# this will pass even though it is not a `tbl_df` class
expect_s3_class(x, c("tbl_df", "tbl", "data.frame"))
# Using `exact = TRUE` will account for these failures. 
show_failure(expect_s3_class(x, c("tbl_df", "tbl", "data.frame"), exact = TRUE))
#> Failed expectation:
#> `x` has class 'data.frame', not 'tbl_df'/'tbl'/'data.frame'.

Created on 2024-12-12 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant