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

Make S7_inherits() more flexible #347

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions R/inherits.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#' Does this object inherit from an S7 class?
#'
#' * `S7_inherits()` returns `TRUE` or `FALSE`.
#' * `check_is_S7()` throws an error.
#' * `check_is_S7()` throws an error if `x` isn't the specified `class`.
#'
#' @param x An object
#' @param class An S7 class. Can be omitted in `check_is_S7()`.
#' @param class An S7 class or `NULL`. If `NULL`, tests whether `x` is an
#' S7 object without testing for a specific class.
#' @param arg Argument name used in error message.
#' @returns `S7_inherits()` returns a single `TRUE` or `FALSE`;
#' `check_is_S7()` returns nothing; it's called for its side-effects.
#' @returns
#' * `S7_inherits()` returns a single `TRUE` or `FALSE`.
#' * `check_is_S7()` returns nothing; it's called for its side-effects.
#' @export
#' @examples
#' foo1 <- new_class("foo1")
Expand All @@ -19,12 +21,13 @@
#'
#' S7_inherits(foo1(), foo2)
#' try(check_is_S7(foo1(), foo2))
S7_inherits <- function(x, class) {
if (!inherits(class, "S7_class")) {
stop("`class` is not an <S7_class>")
S7_inherits <- function(x, class = NULL) {
if (!inherits(class, "S7_class") && !is.null(class)) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
stop("`class` must be an <S7_class> or NULL")
}

inherits(x, "S7_object") && inherits(x, S7_class_name(class))
inherits(x, "S7_object") &&
(is.null(class) || inherits(x, S7_class_name(class)))
}

#' @export
Expand Down
13 changes: 8 additions & 5 deletions man/S7_inherits.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/inherits.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Code
S7_inherits(1:10, "x")
Error <simpleError>
`class` is not an <S7_class>
`class` must be an <S7_class> or NULL

# throws informative error

Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-inherits.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ test_that("it works", {
foo1 <- new_class("foo1")
foo2 <- new_class("foo2", parent = foo1)

expect_true(S7_inherits(foo1(), NULL))
expect_true(S7_inherits(foo1(), foo1))
expect_true(S7_inherits(foo2(), foo1))
expect_false(S7_inherits(foo1(), foo2))
expect_false(S7_inherits(1, NULL))
})

test_that("checks that input is a class", {
Expand Down
Loading