Skip to content
Closed
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ Config/testthat/parallel: true
Config/testthat/start-first: watcher, parallel*
Encoding: UTF-8
Roxygen: list(markdown = TRUE, r6 = FALSE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export(expect_equal)
export(expect_equal_to_reference)
export(expect_equivalent)
export(expect_error)
export(expect_error_forwarded)
export(expect_failure)
export(expect_false)
export(expect_gt)
Expand All @@ -102,6 +103,7 @@ export(expect_lte)
export(expect_mapequal)
export(expect_match)
export(expect_message)
export(expect_message_forwarded)
export(expect_more_than)
export(expect_named)
export(expect_no_condition)
Expand Down Expand Up @@ -130,6 +132,7 @@ export(expect_type)
export(expect_vector)
export(expect_visible)
export(expect_warning)
export(expect_warning_forwarded)
export(expectation)
export(fail)
export(find_test_scripts)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# testthat (development version)

* Add new error, warning, and message capturing expectations for conditions from
other packages. `expect_error_forwarded()`, `expect_warning_forwarded()`, and
`expect_message_forwarded()` capture errors, warnings, and messages that come
from other code to match the message that is not in the control of the package
being tested (@billdenney, #1927).

# testthat 3.2.1

* Fix incorrect format string detected by latest R-devel. Fix thanks to
Expand Down
90 changes: 90 additions & 0 deletions R/expect-condition-forwarded.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#' Does code throw an error, warning, or message that is the same as another?
#'
#' @description
#' `expect_error_forwarded()`, `expect_warning_forwarded()`, and
#' `expect_message_forwarded()`, check that code throws an error, warning,
#' message, or condition with a message that matches an `expected` value from an
#' example. These functions are useful to test that a condition matches one
#' from another package that you do not control.
#'
#' @export
#' @family expectations
#' @inheritParams expect_that
#' @param expected A call that causes the expected error, warning, or message.
#' It is usually a call to another package than the one being tested.
#' @return The value of the first argument
#' @examples
#' myfun <- function(x) {
#' stopifnot(length(x) == 1)
#' }
#'
#' expect_error_forwarded(
#' myfun(x = 1:2),
#' {
#' x <- 1:2
#' stopifnot(length(x) == 1)
#' }
#' )
expect_error_forwarded <- function(object, expected, label = NULL) {
message <- error_message(expected)
expect_error({{ object }}, regexp = message, fixed = TRUE, label = label)
}

#' @export
#' @rdname expect_error_forwarded
expect_warning_forwarded <- function(object, expected, label = NULL) {
message <- warning_message(expected)
expect_warning({{ object }}, regexp = message, fixed = TRUE, label = label)
}

#' @export
#' @rdname expect_error_forwarded
expect_message_forwarded <- function(object, expected, label = NULL) {
message <- message_message(expected)
expect_message({{ object }}, regexp = message, fixed = TRUE, label = label)
}

error_message <- function(code) {
out <- tryCatch(
{
code
NULL
},
error = function(e) conditionMessage(e)
)
if (is.null(out)) {
stop("No error thrown")
} else {
out
}
}

warning_message <- function(code) {
out <- tryCatch(
{
code
NULL
},
warning = function(e) conditionMessage(e)
)
if (is.null(out)) {
stop("No warning thrown")
} else {
out
}
}

message_message <- function(code) {
out <- tryCatch(
{
code
NULL
},
message = function(e) conditionMessage(e)
)
if (is.null(out)) {
stop("No message thrown")
} else {
out
}
}
4 changes: 4 additions & 0 deletions R/expect-condition.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#' or condition with a message that matches `regexp`, or a class that inherits
#' from `class`. See below for more details.
#'
#' If you need to test that an error, warning, or message is the same as one
#' given by another package, then see the [expect_error_forwarded()],
#' [expect_warning_forwarded()], and [expect_message_forwarded()] functions.
#'
#' In the 3rd edition, these functions match (at most) a single condition. All
#' additional and non-matching (if `regexp` or `class` are used) conditions
#' will bubble up outside the expectation. If these additional conditions
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ reference:
- expect_invisible
- expect_output
- expect_silent
- expect_error_forwarded
- local_reproducible_output

- title: Snapshot testing
Expand Down
1 change: 1 addition & 0 deletions man/comparison-expectations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/equality-expectations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/expect_error.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions man/expect_error_forwarded.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/expect_length.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/expect_match.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/expect_named.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/expect_null.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/expect_output.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/expect_reference.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/expect_silent.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/inheritance-expectations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/logical-expectations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading