File: on-failure.r

package info (click to toggle)
r-cran-assertthat 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 220 kB
  • sloc: sh: 9; makefile: 2
file content (29 lines) | stat: -rw-r--r-- 803 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#' Custom failure messages for assertions.
#'
#' @param x a assertion function that returns \code{TRUE} if the assertion
#'   is met, \code{FALSE} otherwise.
#' @param value a function with parameters \code{call} and \code{env}
#'   that returns a custom error message as a string.
#' @export
#' @examples
#' is_odd <- function(x) {
#'   assert_that(is.numeric(x), length(x) == 1)
#'   x %% 2 == 1
#' }
#' see_if(is_odd(2))
#'
#' on_failure(is_odd) <- function(call, env) {
#'   paste0(deparse(call$x), " is even")
#' }
#' see_if(is_odd(2))
on_failure <- function(x) attr(x, "fail")


#' @export
#' @rdname on_failure
#' @usage on_failure(x) <- value
"on_failure<-" <- function(x, value) {
  stopifnot(is.function(x), identical(names(formals(value)), c("call", "env")))
  attr(x, "fail") <- value
  x
}