-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement R7_inherits() and check_R7_inherits()
Fixes #193
- Loading branch information
Showing
6 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#' Does this object inherit from an R7 class? | ||
#' | ||
#' * `R7_inherits()` returns `TRUE` or `FALSE` | ||
#' * `check_R7_inherits()` throws an error. | ||
#' | ||
#' @param x An object | ||
#' @param class An R7 class | ||
#' @param arg Argument name used in error message. | ||
#' @export | ||
#' @examples | ||
#' foo1 <- new_class("foo1") | ||
#' foo2 <- new_class("foo2") | ||
#' | ||
#' R7_inherits(foo1(), foo1) | ||
#' check_R7_inherits(foo1(), foo1) | ||
#' | ||
#' R7_inherits(foo1(), foo2) | ||
#' try(check_R7_inherits(foo1(), foo2)) | ||
R7_inherits <- function(x, class) { | ||
if (!inherits(class, "R7_class")) { | ||
stop("`class` is not an <R7_class>") | ||
} | ||
|
||
inherits(x, "R7_object") && inherits(x, R7_class_name(class)) | ||
} | ||
|
||
#' @export | ||
#' @rdname R7_inherits | ||
check_R7_inherits <- function(x, class, arg = deparse(substitute(x))) { | ||
if (!R7_inherits(x, class)) { | ||
stop(sprintf("`%s` is not an %s", arg, class_desc(class)), call. = FALSE) | ||
} | ||
invisible() | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# checks that input is a class | ||
|
||
Code | ||
R7_inherits(1:10, "x") | ||
Error <simpleError> | ||
`class` is not an <R7_class> | ||
|
||
# throws informative error | ||
|
||
Code | ||
foo1 <- new_class("foo1") | ||
foo2 <- new_class("foo2") | ||
check_R7_inherits(foo1(), foo2) | ||
Error <simpleError> | ||
`foo1()` is not an <foo2> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
test_that("it works", { | ||
foo1 <- new_class("foo1") | ||
foo2 <- new_class("foo2", parent = foo1) | ||
|
||
expect_true(R7_inherits(foo1(), foo1)) | ||
expect_true(R7_inherits(foo2(), foo1)) | ||
expect_false(R7_inherits(foo1(), foo2)) | ||
}) | ||
|
||
test_that("checks that input is a class", { | ||
expect_snapshot(R7_inherits(1:10, "x"), error = TRUE) | ||
}) | ||
|
||
test_that("throws informative error", { | ||
expect_snapshot(error = TRUE, { | ||
foo1 <- new_class("foo1") | ||
foo2 <- new_class("foo2") | ||
check_R7_inherits(foo1(), foo2) | ||
}) | ||
}) |