-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement expect_no_warnings() and friends (#1690)
Fixes #1679
- Loading branch information
Showing
9 changed files
with
285 additions
and
3 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
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,127 @@ | ||
#' Does code run without error, warning, message, or other condition? | ||
#' | ||
#' @description | ||
#' `r lifecycle::badge("experimental")` | ||
#' | ||
#' These expectations are the opposite of [expect_error()], | ||
#' `expect_warning()`, `expect_message()`, and `expect_condition()`. They | ||
#' assert the absence of an error, warning, or message, respectively. | ||
#' | ||
#' @inheritParams expect_error | ||
#' @param message,class The default, `message = NULL, class = NULL`, | ||
#' will fail if there is any error/warning/message/condition. | ||
#' | ||
#' If many cases, particularly when testing warnings and message, you will | ||
#' want to be more specific about the condition you are hoping **not** to see, | ||
#' i.e. the condition that motivated you to write the test. Similar to | ||
#' `expect_error()` and friends, you can specify the `message` (a regular | ||
#' expression that the message of the condition must match) and/or the | ||
#' `class` (a class the condition must inherit from). This ensures that | ||
#' the message/warnings you don't want never recur, while allowing new | ||
#' messages/warnings to bubble up for you to deal with. | ||
#' | ||
#' Note that you should only use `message` with errors/warnings/messages | ||
#' that you generate, or that base R generates (which tend to be stable). | ||
#' Avoid tests that rely on the specific text generated by another package | ||
#' since this can easily change. If you do need to test text generated by | ||
#' another package, either protect the test with `skip_on_cran()` or | ||
#' use `expect_snapshot()`. | ||
#' @inheritParams rlang::args_dots_empty | ||
#' @export | ||
#' @examples | ||
#' expect_no_warning(1 + 1) | ||
#' | ||
#' foo <- function(x) { | ||
#' warning("This is a problem!") | ||
#' } | ||
#' | ||
#' # warning doesn't match so bubbles up: | ||
#' expect_no_warning(foo(), message = "bananas") | ||
#' | ||
#' # warning does match so causes a failure: | ||
#' try(expect_no_warning(foo(), message = "problem")) | ||
expect_no_error <- function(object, | ||
..., | ||
message = NULL, | ||
class = NULL) { | ||
check_dots_empty() | ||
expect_no_("error", {{ object }}, ..., regexp = message, class = class) | ||
} | ||
|
||
|
||
#' @export | ||
#' @rdname expect_no_error | ||
expect_no_warning <- function(object, | ||
..., | ||
message = NULL, | ||
class = NULL | ||
) { | ||
check_dots_empty() | ||
expect_no_("warning", {{ object }}, ..., regexp = message, class = class) | ||
} | ||
|
||
#' @export | ||
#' @rdname expect_no_error | ||
expect_no_message <- function(object, | ||
..., | ||
message = NULL, | ||
class = NULL | ||
) { | ||
check_dots_empty() | ||
expect_no_("messsage", {{ object }}, ..., regexp = message, class = class) | ||
} | ||
|
||
#' @export | ||
#' @rdname expect_no_error | ||
expect_no_condition <- function(object, | ||
..., | ||
message = NULL, | ||
class = NULL | ||
) { | ||
check_dots_empty() | ||
expect_no_("condition", {{ object }}, ..., regexp = message, class = class) | ||
} | ||
|
||
|
||
expect_no_ <- function(base_class, | ||
object, | ||
regexp = NULL, | ||
class = NULL, | ||
..., | ||
error_call = caller_env()) { | ||
|
||
check_dots_used(action = warn, call = error_call) | ||
matcher <- cnd_matcher(class %||% base_class, regexp, ...) | ||
|
||
capture <- function(code) { | ||
try_fetch( | ||
code, | ||
!!base_class := function(cnd) { | ||
if (!matcher(cnd)) { | ||
return(zap()) | ||
} | ||
|
||
expected <- paste0( | ||
"Expected ", quo_label(enquo(object)), " to run without any ", base_class, "s", | ||
if (!is.null(class)) paste0(" of class '", class, "'"), | ||
if (!is.null(regexp)) paste0(" matching pattern '", regexp, "'"), | ||
"." | ||
) | ||
actual <- paste0( | ||
"Actually got a <", class(cnd)[[1]], ">:\n", | ||
indent_lines(rlang::cnd_message(cnd, prefix = TRUE)) | ||
) | ||
message <- format_error_bullets(c(expected, i = actual)) | ||
fail(message, trace_env = error_call) | ||
} | ||
) | ||
} | ||
|
||
act <- quasi_capture(enquo(object), NULL, capture) | ||
succeed() | ||
invisible(act$val) | ||
} | ||
|
||
indent_lines <- function(x) { | ||
paste0(" ", gsub("\n", "\n ", x)) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,35 @@ | ||
# matched conditions give informative message | ||
|
||
Code | ||
expect_no_warning(foo()) | ||
Condition | ||
Error: | ||
! Expected `foo()` to run without any warnings. | ||
i Actually got a <test>: | ||
Warning: | ||
This is a problem! | ||
Code | ||
expect_no_warning(foo(), message = "problem") | ||
Condition | ||
Error: | ||
! Expected `foo()` to run without any warnings matching pattern 'problem'. | ||
i Actually got a <test>: | ||
Warning: | ||
This is a problem! | ||
Code | ||
expect_no_warning(foo(), class = "test") | ||
Condition | ||
Error: | ||
! Expected `foo()` to run without any warnings of class 'test'. | ||
i Actually got a <test>: | ||
Warning: | ||
This is a problem! | ||
Code | ||
expect_no_warning(foo(), message = "problem", class = "test") | ||
Condition | ||
Error: | ||
! Expected `foo()` to run without any warnings of class 'test' matching pattern 'problem'. | ||
i Actually got a <test>: | ||
Warning: | ||
This is a problem! | ||
|
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,26 @@ | ||
test_that("unmatched conditions bubble up", { | ||
expect_error(expect_no_error(abort("foo"), message = "bar"), "foo") | ||
expect_warning(expect_no_warning(warn("foo"), message = "bar"), "foo") | ||
expect_message(expect_no_message(inform("foo"), message = "bar"), "foo") | ||
expect_condition(expect_no_condition(signal("foo", "x"), message = "bar"), "foo") | ||
}) | ||
|
||
test_that("only matches conditions of specified type", { | ||
foo <- function() { | ||
warn("This is a problem!", class = "test") | ||
} | ||
expect_warning(expect_no_error(foo(), class = "test"), class = "test") | ||
}) | ||
|
||
test_that("matched conditions give informative message", { | ||
foo <- function() { | ||
warn("This is a problem!", class = "test") | ||
} | ||
|
||
expect_snapshot(error = TRUE, { | ||
expect_no_warning(foo()) | ||
expect_no_warning(foo(), message = "problem") | ||
expect_no_warning(foo(), class = "test") | ||
expect_no_warning(foo(), message = "problem", class = "test") | ||
}) | ||
}) |