Skip to content

Commit 6e3c1e5

Browse files
authored
Check expect() and fail() inputs (#2254)
Fixes #2247
1 parent b3aaec1 commit 6e3c1e5

File tree

7 files changed

+41
-8
lines changed

7 files changed

+41
-8
lines changed

R/expect-that.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
#' Learn more about creating your own expectations in
99
#' `vignette("custom-expectation")`.
1010
#'
11-
#' @param message Failure message to send to the user. It's best practice to
12-
#' describe both what is expected and what was actually received.
11+
#' @param message A character vector describing the failure. The
12+
#' first element should describe the expected value, and the second (and
13+
#' optionally subsequence) elements should describe what was actually seen.
1314
#' @param info Character vector continuing additional information. Included
1415
#' for backward compatibility only and new expectations should not use it.
1516
#' @param srcref Location of the failure. Should only needed to be explicitly
@@ -41,6 +42,9 @@ fail <- function(
4142
trace_env = caller_env(),
4243
trace = NULL
4344
) {
45+
check_character(message)
46+
check_character(info, allow_null = TRUE)
47+
4448
trace <- trace %||% capture_trace(trace_env)
4549
message <- paste(c(message, info), collapse = "\n")
4650
expectation("failure", message, srcref = srcref, trace = trace)

R/expectation.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#' `vignette("custom-expectation")` for details.
66
#'
77
#' @param ok `TRUE` or `FALSE` indicating if the expectation was successful.
8-
#' @param failure_message Message to show if the expectation failed.
8+
#' @param failure_message A character vector describing the failure. The
9+
#' first element should describe the expected value, and the second (and
10+
#' optionally subsequence) elements should describe what was actually seen.
911
#' @inheritParams fail
1012
#' @return An expectation object from either `succeed()` or `fail()`.
1113
#' with a `continue_test` restart.
@@ -20,6 +22,9 @@ expect <- function(
2022
trace = NULL,
2123
trace_env = caller_env()
2224
) {
25+
check_bool(ok)
26+
check_character(failure_message)
27+
2328
if (!ok) {
2429
return(fail(
2530
failure_message,

man/expect.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fail.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/succeed.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# validates key inputs
2+
3+
Code
4+
expect(1)
5+
Condition
6+
Error in `expect()`:
7+
! `ok` must be `TRUE` or `FALSE`, not the number 1.
8+
Code
9+
expect(TRUE, 1)
10+
Condition
11+
Error in `expect()`:
12+
! `failure_message` must be a character vector, not the number 1.
13+

tests/testthat/test-expectation.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ test_that("info only evaluated on failure", {
77
expect_no_error(expect(TRUE, "fail", info = stop("!")))
88
})
99

10+
test_that("validates key inputs", {
11+
expect_snapshot(error = TRUE, {
12+
expect(1)
13+
expect(TRUE, 1)
14+
})
15+
})
16+
1017
test_that("can subclass expectation", {
1118
exp <- new_expectation(
1219
"failure",

0 commit comments

Comments
 (0)