Skip to content

Commit 6166099

Browse files
authored
Display expected condition class (#2003)
In `expect_condition()` and friends, when `class` is supplied and the expectation fails. Fixes #1987
1 parent ef5e293 commit 6166099

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* `expect_condition()` and friends now include the `class` of the expected condition in the failure mesage, if used (#1987).
34
* `LANGUAGE` is now set to `"C"` in reprocucible environments (i.e.
45
`test_that()` blocks) to disable translations. This fixes warnings
56
about being unable to set the language to `"en"` (#1925).

R/expect-condition.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ expect_condition_matching <- function(base_class,
278278
)
279279

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

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

376376
# Helpers -----------------------------------------------------------------
377377

378-
compare_condition_3e <- function(cond_type, cond, lab, expected) {
378+
compare_condition_3e <- function(cond_type, cond_class, cond, lab, expected) {
379379
if (expected) {
380380
if (is.null(cond)) {
381-
sprintf("%s did not throw the expected %s.", lab, cond_type)
381+
if (is.null(cond_class)) {
382+
sprintf("%s did not throw the expected %s.", lab, cond_type)
383+
} else {
384+
sprintf("%s did not throw a %s with class <%s>.", lab, cond_type, cond_class)
385+
}
382386
} else {
383387
NULL
384388
}

R/snapshot.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ expect_snapshot <- function(x,
8989
)
9090

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

tests/testthat/_snaps/expect-condition.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
Message: dispatched!
2626
Class: foobar/rlang_error/error/condition
2727

28+
# condition class is included in failure
29+
30+
`f1()` did not throw a condition with class <bar>.
31+
2832
# unused arguments generate a warning
2933

3034
Code

tests/testthat/test-expect-condition.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ test_that("captured condition is muffled", {
149149
expect_error(expect_condition(stop("Hi")), NA)
150150
})
151151

152+
test_that("condition class is included in failure", {
153+
f1 <- function() signal(class = "foo")
154+
expect_snapshot_failure(expect_condition(f1(), class = "bar"))
155+
})
156+
152157
test_that("only matching condition is captured, others bubble up", {
153158
f1 <- function() {
154159
message("Hi")

0 commit comments

Comments
 (0)