Skip to content

Commit 5a8200a

Browse files
authored
Improve ... checking logic for 3e expect_error() (and friends) (#2014)
Fixes #1932
1 parent 6ba4397 commit 5a8200a

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* `expect_error()` and friends now error if you supply `...` but not `pattern` (#1932).
34
* New `expect_no_failure()`, `expect_no_success()` and `expect_snapshot_failure()` provide more options for testing expectations.
45
* `expect_error()` and friends no longer give an uninformative error if they fail inside a magrittr pipe (#1994).
56
* `expect_setequal()` correctly identifies what is missing where (#1962).

R/expect-condition.R

+18-6
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,6 @@ expect_condition_matching <- function(base_class,
253253
label = NULL,
254254
trace_env = caller_env(),
255255
error_call = caller_env()) {
256-
257-
check_dots_used(error = function(cnd) {
258-
warn(conditionMessage(cnd), call = error_call)
259-
})
260-
261256
matcher <- cnd_matcher(
262257
base_class,
263258
class,
@@ -299,6 +294,13 @@ cnd_matcher <- function(base_class,
299294
check_string(class, allow_null = TRUE, call = error_call)
300295
check_string(pattern, allow_null = TRUE, allow_na = TRUE, call = error_call)
301296

297+
if (is.null(pattern) && dots_n(...) > 0) {
298+
cli::cli_abort(
299+
"Can't specify {.arg ...} without {.arg pattern}.",
300+
call = error_call
301+
)
302+
}
303+
302304
function(cnd) {
303305
if (!inherit) {
304306
cnd$parent <- NULL
@@ -316,7 +318,17 @@ cnd_matcher <- function(base_class,
316318
return(FALSE)
317319
}
318320
if (!is.null(pattern) && !isNA(pattern)) {
319-
grepl(pattern, conditionMessage(x), ...)
321+
withCallingHandlers(
322+
grepl(pattern, conditionMessage(x), ...),
323+
error = function(e) {
324+
cli::cli_abort(
325+
"Failed to compare {base_class} to {.arg pattern}.",
326+
parent = e,
327+
call = error_call
328+
)
329+
}
330+
)
331+
320332
} else {
321333
TRUE
322334
}

tests/testthat/_snaps/expect-condition.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@
2929

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

32-
# unused arguments generate a warning
32+
# unused arguments generate an error
3333

3434
Code
3535
expect_condition(stop("Hi!"), foo = "bar")
3636
Condition
37-
Warning in `expect_condition()`:
38-
Arguments in `...` must be used.
39-
x Problematic argument:
40-
* foo = "bar"
41-
i Did you misspell an argument name?
37+
Error in `expect_condition()`:
38+
! Can't specify `...` without `pattern`.
39+
Code
40+
expect_condition(stop("Hi!"), "x", foo = "bar")
41+
Condition
42+
Error in `expect_condition()`:
43+
! Failed to compare condition to `pattern`.
44+
Caused by error in `grepl()`:
45+
! unused argument (foo = "bar")
4246

tests/testthat/test-expect-condition.R

+5-2
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,11 @@ test_that("can match parent conditions (#1493)", {
223223
expect_error(expect_error(f(), "Parent message.", inherit = FALSE))
224224
})
225225

226-
test_that("unused arguments generate a warning", {
227-
expect_snapshot(expect_condition(stop("Hi!"), foo = "bar"))
226+
test_that("unused arguments generate an error", {
227+
expect_snapshot(error = TRUE, {
228+
expect_condition(stop("Hi!"), foo = "bar")
229+
expect_condition(stop("Hi!"), "x", foo = "bar")
230+
})
228231
})
229232

230233

0 commit comments

Comments
 (0)