Skip to content

Conversation

@jameslamb
Copy link
Contributor

Per discussion in #59 . In #46 I made has_name() throw an error if you tried to check multiple names in one call, e.g.

x <- list(a = TRUE, b = "hello")
has_name(x, c("a", "b"))

Four packages on CRAN have code (covered by tests) that uses has_name() with multiple names, and all of them do this:

all(has_name(x, c("a", "b"))

Instead of breaking their code, I think the spirit of #46 (all assertions return exactly one TRUE/FALSE) can still be accomplished by using an all() inside has_name()! That is what I've proposed in this PR.

on_failure(has_name) <- function(call, env) {
paste0(deparse(call$x), " does not have name ", eval(call$which, env))
out_names <- paste0("'", paste0(eval(call$which, env), collapse = "', '"), "'")
paste0(deparse(call$x), " does not have all of these name(s): ", out_names)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because of the possibility that call$which is a vector with length greater than 1, I had to do this paste0() stuff to avoid getting

Error in stop(assertError(attr(res, "msg"))) : bad error message

has_args <- function(f, args, exact = FALSE) {
assert_that(is.function(f))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I have my RStudio set to trim trailing whitespace when I save a file. Let me know if you'd like me to revert all of these

)
})

test_that("assert_that handles has_name failures with multiple missing names", {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added this test to catch the condition I mentioned above, where failures with a call$which that has length > 1 could produce a bad error message

@jameslamb jameslamb mentioned this pull request Mar 20, 2019
13 tasks
@hadley hadley merged commit b83c3d0 into hadley:master Mar 20, 2019
@hadley
Copy link
Owner

hadley commented Mar 20, 2019

Thanks!

@jameslamb jameslamb changed the title has_name now always returns a sginel logical value has_name now always returns a single logical value Mar 21, 2019
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

Successfully merging this pull request may close these issues.

2 participants