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

Fix custom handling of assert_that(inherits(.)) testing multiple inheritance #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# assertthat (development version)

* `assert_that(inherits(x, c("a", "b")))`, where inheritance is tested
against multiple target classes, correctly handles failed
assertions (@MichaelChirico, [#73](https://github.com/hadley/assertthat/issues/73))

# assertthat 0.2.1

* `assert_that()` no longer throws a fatal error on very long custom
Expand Down
8 changes: 6 additions & 2 deletions R/base-is.r
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ base_fs$is.recursive <- is_not("a recursive object")
base_fs$is.symbol <- is_not("a name")


# Catch all
base_fs$inherits <- function(call, env) {
class <- eval(call$what, env)
paste0(deparse(call$x), " does not inherit from class ", class)
x_str <- paste(deparse(call$x), collapse = " ")
if (length(class) == 1L) {
paste(x_str, "does not inherit from class", class)
} else {
paste(x_str, "does not inherit from any of these classes:", toString(class))
}
}
8 changes: 8 additions & 0 deletions tests/testthat/test-base.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ test_that("all message is useful", {
test_that("custom message is printed", {
expect_equal(validate_that(FALSE, msg = "Custom message"), "Custom message")
})

test_that("inherits on multiple classes fails gracefully", {
expect_error(
assert_that(inherits(NULL, c("a", "b"))),
"does not inherit from any of these classes",
fixed = TRUE
)
})