Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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
44 changes: 32 additions & 12 deletions R/expect-condition.R
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ expect_condition_matching <- function(
trace_env = caller_env(),
error_call = caller_env()
) {
check_condition_dots(regexp, ..., error_call = error_call)
matcher <- cnd_matcher(
base_class,
class,
Expand Down Expand Up @@ -320,21 +321,14 @@ expect_condition_matching <- function(
cnd_matcher <- function(
base_class,
class = NULL,
pattern = NULL,
regexp = NULL,
...,
inherit = TRUE,
ignore_deprecation = FALSE,
error_call = caller_env()
) {
check_string(class, allow_null = TRUE, call = error_call)
check_string(pattern, allow_null = TRUE, allow_na = TRUE, call = error_call)

if (is.null(pattern) && dots_n(...) > 0) {
cli::cli_abort(
"Can't specify {.arg ...} without {.arg pattern}.",
call = error_call
)
}
check_string(regexp, allow_null = TRUE, allow_na = TRUE, call = error_call)

function(cnd) {
if (!inherit) {
Expand All @@ -352,12 +346,12 @@ cnd_matcher <- function(
if (!is.null(class) && !inherits(x, class)) {
return(FALSE)
}
if (!is.null(pattern) && !isNA(pattern)) {
if (!is.null(regexp) && !isNA(regexp)) {
withCallingHandlers(
grepl(pattern, conditionMessage(x), ...),
grepl(regexp, conditionMessage(x), ...),
error = function(e) {
cli::cli_abort(
"Failed to compare {base_class} to {.arg pattern}.",
"Failed to compare {base_class} to {.arg regexp}.",
parent = e,
call = error_call
)
Expand Down Expand Up @@ -582,3 +576,29 @@ cnd_message <- function(x) {
withr::local_options(rlang_backtrace_on_error = "none")
conditionMessage(x)
}

check_condition_dots <- function(
regexp = NULL,
...,
error_call = caller_env()
) {
if (!is.null(regexp) || missing(...)) {
return()
}

dot_names <- ...names()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, in case it matters, I just came across this admonition to continue avoiding ...names() until 4.1.3 😱

r-lib/lintr#2502

https://github.com/r-lib/lintr/blob/dc8f14996fc303d40f120eea6d56fb54187dbc9c/R/lint.R#L43

if (is.null(dot_names)) {
dot_names <- rep("", ...length())
}
unnamed <- dot_names == ""
dot_names[unnamed] <- paste0("..", seq_along(dot_names)[unnamed])

cli::cli_abort(
c(
"Can't supply {.arg ...} unless {.arg regexp} is set.",
"*" = "Unusued arguments: {.arg {dot_names}}.",
i = "{.arg ...} is passed to {.fn grepl} if and only if the {.arg regexp} argument is used."
),
call = error_call
)
}
2 changes: 1 addition & 1 deletion R/expect-no-condition.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ expect_no_ <- function(
matcher <- cnd_matcher(
base_class,
class,
pattern = regexp,
regexp = regexp,
ignore_deprecation = base_class == "warning" &&
is.null(regexp) &&
is.null(class)
Expand Down
6 changes: 5 additions & 1 deletion R/test-compiled-code.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ run_cpp_tests <- function(package) {
c(line, line, 1, 1)
)

exp <- new_expectation("error", exception_text, srcref = exception_srcref)
exp <- new_expectation(
"error",
exception_text,
srcref = exception_srcref
)
exp$test <- test_name

get_reporter()$add_result(
Expand Down
22 changes: 19 additions & 3 deletions tests/testthat/_snaps/expect-condition.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
expect_error(stop("!"), regexp = 1)
Condition
Error in `expect_error()`:
! `pattern` must be a single string, `NA`, or `NULL`, not the number 1.
! `regexp` must be a single string, `NA`, or `NULL`, not the number 1.
Code
expect_error(stop("!"), class = 1)
Condition
Expand All @@ -35,12 +35,28 @@
expect_condition(stop("Hi!"), foo = "bar")
Condition
Error in `expect_condition()`:
! Can't specify `...` without `pattern`.
! Can't supply `...` unless `regexp` is set.
* Unusued arguments: `foo`.
i `...` is passed to `grepl()` if and only if the `regexp` argument is used.
Code
expect_condition(stop("Hi!"), , , "bar")
Condition
Error in `expect_condition()`:
! Can't supply `...` unless `regexp` is set.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how actionable this is, OTOH, I think it's unlikely to arise organically anyway (this is very strange code). Not worth spending more time on it IMO.

* Unusued arguments: `..1`.
i `...` is passed to `grepl()` if and only if the `regexp` argument is used.
Code
expect_condition(stop("Hi!"), , , "bar", foo = "bar")
Condition
Error in `expect_condition()`:
! Can't supply `...` unless `regexp` is set.
* Unusued arguments: `..1` and `foo`.
i `...` is passed to `grepl()` if and only if the `regexp` argument is used.
Code
expect_condition(stop("Hi!"), "x", foo = "bar")
Condition
Error in `expect_condition()`:
! Failed to compare condition to `pattern`.
! Failed to compare condition to `regexp`.
Caused by error in `grepl()`:
! unused argument (foo = "bar")

1 change: 1 addition & 0 deletions tests/testthat/test-catch.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ test_that("get_routine() fails when no routine exists", {
expect_error(get_routine("utils", "no_such_routine"))
})

skip_if_not_installed("xml2")
run_cpp_tests("testthat")
10 changes: 8 additions & 2 deletions tests/testthat/test-compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,14 @@ test_that("base lengths must be identical", {
})

test_that("tzones must be identical", {
t1 <- ISOdatetime(2016, 2, 29, 12, 13, 14, "EST")
t2 <- ISOdatetime(2016, 2, 29, 12, 13, 14, "US/Eastern")
# skip on minimal setups
tryCatch(
{
t1 <- ISOdatetime(2016, 2, 29, 12, 13, 14, "EST")
t2 <- ISOdatetime(2016, 2, 29, 12, 13, 14, "US/Eastern")
},
warning = function(w) skip(conditionMessage(w))
)

expect_match(compare(t1, t2)$message, '"tzone": 1 string mismatch')
})
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-expect-condition.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ test_that("can match parent conditions (#1493)", {
test_that("unused arguments generate an error", {
expect_snapshot(error = TRUE, {
expect_condition(stop("Hi!"), foo = "bar")
expect_condition(stop("Hi!"), , , "bar")
expect_condition(stop("Hi!"), , , "bar", foo = "bar")
expect_condition(stop("Hi!"), "x", foo = "bar")
})
})
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-expect-inheritance.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ test_that("checks its inputs", {
})

test_that("can check with actual class", {
skip_if_not_installed("S7")
Foo <- S7::new_class("Foo", package = NULL)
Bar <- S7::new_class("Bar", package = NULL)
expect_success(expect_s7_class(Foo(), class = Foo))
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-reporter-junit.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("reporter doesn't change without warning", {
skip_if_not_installed("xml2")
expect_snapshot_reporter(JunitReporterMock$new())
})

Expand Down
Loading