Skip to content

Commit

Permalink
Display expected condition class (#2003)
Browse files Browse the repository at this point in the history
In `expect_condition()` and friends, when `class` is supplied and the expectation fails.

Fixes #1987
  • Loading branch information
hadley authored Oct 29, 2024
1 parent ef5e293 commit 6166099
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# testthat (development version)

* `expect_condition()` and friends now include the `class` of the expected condition in the failure mesage, if used (#1987).
* `LANGUAGE` is now set to `"C"` in reprocucible environments (i.e.
`test_that()` blocks) to disable translations. This fixes warnings
about being unable to set the language to `"en"` (#1925).
Expand Down
10 changes: 7 additions & 3 deletions R/expect-condition.R
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ expect_condition_matching <- function(base_class,
)

expected <- !identical(regexp, NA)
msg <- compare_condition_3e(base_class, act$cap, act$lab, expected)
msg <- compare_condition_3e(base_class, class, act$cap, act$lab, expected)

# Access error fields with `[[` rather than `$` because the
# `$.Throwable` from the rJava package throws with unknown fields
Expand Down Expand Up @@ -375,10 +375,14 @@ capture_matching_condition <- function(expr, matches) {

# Helpers -----------------------------------------------------------------

compare_condition_3e <- function(cond_type, cond, lab, expected) {
compare_condition_3e <- function(cond_type, cond_class, cond, lab, expected) {
if (expected) {
if (is.null(cond)) {
sprintf("%s did not throw the expected %s.", lab, cond_type)
if (is.null(cond_class)) {
sprintf("%s did not throw the expected %s.", lab, cond_type)
} else {
sprintf("%s did not throw a %s with class <%s>.", lab, cond_type, cond_class)
}
} else {
NULL
}
Expand Down
3 changes: 1 addition & 2 deletions R/snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ expect_snapshot <- function(x,
)

# Use expect_error() machinery to confirm that error is as expected
msg <- compare_condition_3e("error", state$error, quo_label(x), error)
msg <- compare_condition_3e("error", NULL, state$error, quo_label(x), error)
if (!is.null(msg)) {
if (error) {
expect(FALSE, msg, trace = state$error[["trace"]])
Expand Down Expand Up @@ -362,4 +362,3 @@ with_is_snapshotting <- function(code) {
withr::local_envvar(TESTTHAT_IS_SNAPSHOT = "true")
code
}

4 changes: 4 additions & 0 deletions tests/testthat/_snaps/expect-condition.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
Message: dispatched!
Class: foobar/rlang_error/error/condition

# condition class is included in failure

`f1()` did not throw a condition with class <bar>.

# unused arguments generate a warning

Code
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-expect-condition.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ test_that("captured condition is muffled", {
expect_error(expect_condition(stop("Hi")), NA)
})

test_that("condition class is included in failure", {
f1 <- function() signal(class = "foo")
expect_snapshot_failure(expect_condition(f1(), class = "bar"))
})

test_that("only matching condition is captured, others bubble up", {
f1 <- function() {
message("Hi")
Expand Down

0 comments on commit 6166099

Please sign in to comment.