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

Check existence of restarts before invokation #923

Merged
merged 1 commit into from
Aug 9, 2019
Merged
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,6 +1,10 @@

# testthat (development version)

* Existence of restarts is first checked before invokation. This makes
it possible to signal warnings or messages with a different
condition signaller (#874).

* Unexpected errors are now printed with a simplified backtrace.

* `expect_error()` and `expect_condition()` now display a backtrace
Expand Down
4 changes: 2 additions & 2 deletions R/capture-condition.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ capture_messages <- function(code) {
code,
message = function(condition) {
out$push(condition)
invokeRestart("muffleMessage")
maybe_restart("muffleMessage")
}
)

Expand All @@ -97,7 +97,7 @@ capture_warnings <- function(code) {
code,
warning = function(condition) {
out$push(condition)
invokeRestart("muffleWarning")
maybe_restart("muffleWarning")
}
)

Expand Down
4 changes: 2 additions & 2 deletions R/evaluate-promise.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ evaluate_promise <- function(code, print = FALSE) {
warnings <- Stack$new()
handle_warning <- function(condition) {
warnings$push(condition)
invokeRestart("muffleWarning")
maybe_restart("muffleWarning")
}

messages <- Stack$new()
handle_message <- function(condition) {
messages$push(condition)
invokeRestart("muffleMessage")
maybe_restart("muffleMessage")
}

temp <- file()
Expand Down
5 changes: 3 additions & 2 deletions R/test-that.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,12 @@ test_code <- function(test, code, env = test_env(), skip_on_empty = TRUE) {

handled <<- TRUE
register_expectation(e, 5)
invokeRestart("muffleWarning")

maybe_restart("muffleWarning")
}
handle_message <- function(e) {
handled <<- TRUE
invokeRestart("muffleMessage")
maybe_restart("muffleMessage")
}
handle_skip <- function(e) {
handled <<- TRUE
Expand Down
2 changes: 1 addition & 1 deletion R/try-again.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ try_again <- function(times, code) {
},
warning = function(e) {
if (identical(e$message, "restarting interrupted promise evaluation")) {
invokeRestart("muffleWarning")
maybe_restart("muffleWarning")
}
}
),
Expand Down
6 changes: 6 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,9 @@ paste_line <- function(...) {
maybe_root_dir <- function(path) {
tryCatch(pkgload::pkg_path(path), error = function(...) path)
}

maybe_restart <- function(restart) {
if (!is.null(findRestart(restart))) {
invokeRestart(restart)
}
}
7 changes: 7 additions & 0 deletions tests/testthat/test-test-that.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,10 @@ test_that("line numbers captured for stop()s", {
})
expect_equal(lines, 2)
})

test_that("can signal warnings and messages without restart", {
expect_null(signalCondition(message_cnd("foo")))

return("Skipping following test because it verbosely registers the warning")
expect_null(signalCondition(warning_cnd("foo")))
})