Skip to content
Merged
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
8 changes: 6 additions & 2 deletions R/expect-that.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#' Learn more about creating your own expectations in
#' `vignette("custom-expectation")`.
#'
#' @param message Failure message to send to the user. It's best practice to
#' describe both what is expected and what was actually received.
#' @param message A character vector describing the failure. The
#' first element should describe the expected value, and the second (and
#' optionally subsequence) elements should describe what was actually seen.
#' @param info Character vector continuing additional information. Included
#' for backward compatibility only and new expectations should not use it.
#' @param srcref Location of the failure. Should only needed to be explicitly
Expand Down Expand Up @@ -41,6 +42,9 @@ fail <- function(
trace_env = caller_env(),
trace = NULL
) {
check_character(message)
check_character(info, allow_null = TRUE)

trace <- trace %||% capture_trace(trace_env)
message <- paste(c(message, info), collapse = "\n")
expectation("failure", message, srcref = srcref, trace = trace)
Expand Down
7 changes: 6 additions & 1 deletion R/expectation.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#' `vignette("custom-expectation")` for details.
#'
#' @param ok `TRUE` or `FALSE` indicating if the expectation was successful.
#' @param failure_message Message to show if the expectation failed.
#' @param failure_message A character vector describing the failure. The
#' first element should describe the expected value, and the second (and
#' optionally subsequence) elements should describe what was actually seen.
#' @inheritParams fail
#' @return An expectation object from either `succeed()` or `fail()`.
#' with a `continue_test` restart.
Expand All @@ -20,6 +22,9 @@ expect <- function(
trace = NULL,
trace_env = caller_env()
) {
check_bool(ok)
check_character(failure_message)

if (!ok) {
return(fail(
failure_message,
Expand Down
4 changes: 3 additions & 1 deletion man/expect.Rd

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

5 changes: 3 additions & 2 deletions man/fail.Rd

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

5 changes: 3 additions & 2 deletions man/succeed.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/_snaps/expectation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# validates key inputs

Code
expect(1)
Condition
Error in `expect()`:
! `ok` must be `TRUE` or `FALSE`, not the number 1.
Code
expect(TRUE, 1)
Condition
Error in `expect()`:
! `failure_message` must be a character vector, not the number 1.

7 changes: 7 additions & 0 deletions tests/testthat/test-expectation.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ test_that("info only evaluated on failure", {
expect_no_error(expect(TRUE, "fail", info = stop("!")))
})

test_that("validates key inputs", {
expect_snapshot(error = TRUE, {
expect(1)
expect(TRUE, 1)
})
})

test_that("can subclass expectation", {
exp <- new_expectation(
"failure",
Expand Down