-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
expect_error_forwarded()
, expect_warning_forwarded()
, and `ex…
…pect_message_forwarded()`
- Loading branch information
1 parent
f060cce
commit 156feee
Showing
6 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#' 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 | ||
#' @return The value of the first argument | ||
expect_error_forwarded <- function(actual, expected, label = NULL) { | ||
message <- error_message(expected) | ||
expect_error({{ actual }}, regexp = message, fixed = TRUE, label = label) | ||
} | ||
|
||
#' @export | ||
#' @rdname expect_error_forwarded | ||
expect_warning_forwarded <- function(actual, expected, label = NULL) { | ||
message <- warning_message(expected) | ||
expect_warning({{ actual }}, regexp = message, fixed = TRUE, label = label) | ||
} | ||
|
||
#' @export | ||
#' @rdname expect_error_forwarded | ||
expect_message_forwarded <- function(actual, expected, label = NULL) { | ||
message <- message_message(expected) | ||
expect_message({{ actual }}, 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
test_that("expect_error_forwarded general use", { | ||
expect_success( | ||
expect_error_forwarded( | ||
stop(), | ||
stop() | ||
) | ||
) | ||
|
||
# Matches | ||
expect_success( | ||
expect_error_forwarded( | ||
stop("foo"), | ||
stop("foo") | ||
) | ||
) | ||
# Does not match | ||
expect_error( | ||
expect_error_forwarded( | ||
stop("foo"), | ||
stop("bar") | ||
) | ||
) | ||
|
||
# Matches | ||
expect_success( | ||
expect_warning_forwarded( | ||
warning("foo"), | ||
warning("foo") | ||
) | ||
) | ||
# Does not match | ||
expect_warning( | ||
expect_error( | ||
expect_warning_forwarded( | ||
warning("foo"), | ||
warning("bar") | ||
), | ||
regexp = '`warning("foo")` did not throw the expected warning.', | ||
fixed = TRUE | ||
), | ||
regexp = "foo" | ||
) | ||
|
||
# Matches | ||
expect_success( | ||
expect_message_forwarded( | ||
message("foo"), | ||
message("foo") | ||
) | ||
) | ||
# Does not match | ||
expect_message( | ||
expect_error( | ||
expect_message_forwarded( | ||
message("foo"), | ||
message("bar") | ||
), | ||
regexp = '`message("foo")` did not throw the expected message.', | ||
fixed = TRUE | ||
), | ||
regexp = "foo" | ||
) | ||
}) | ||
|
||
test_that("expect_*_forwarded with no condition generated ", { | ||
expect_error( | ||
expect_error_forwarded(1, 1), | ||
regexp = "No error thrown", | ||
fixed = TRUE | ||
) | ||
expect_error( | ||
expect_warning_forwarded(1, 1), | ||
regexp = "No warning thrown", | ||
fixed = TRUE | ||
) | ||
expect_error( | ||
expect_message_forwarded(1, 1), | ||
regexp = "No message thrown", | ||
fixed = TRUE | ||
) | ||
}) |