tests/helper/helper.R

library(backports)

expect_identical = function(x, y) {
  stopifnot(identical(x, y))
}

expect_true = function(x) {
  stopifnot(isTRUE(x))
}

expect_false = function(x) {
  stopifnot(isFALSE(x))
}

expect_error = function(x, pattern = NULL) {
  ok = try(eval.parent(substitute(x)), silent = TRUE)
  if (!inherits(ok, "try-error"))
    stop(deparse(substitute(x)), " did not throw an error")
  if (!is.null(pattern) && !grepl(pattern, as.character(ok)))
    stop(sprintf("Expected error message matching '%s', got '%s'", pattern, backports:::trimws(as.character(ok))))
}

makeCompareFun = function(f1, f2, ...) {
  f1 = match.fun(f1)
  f2 = match.fun(f2)

  function(...) {
    r1 = try(f1(...), silent = TRUE)
    r2 = try(f2(...), silent = TRUE)
    if (inherits(r1, "try-error")) {
      stopifnot(inherits(r2, "try-error"))
    } else {
      expect_identical(r1, r2)
    }
  }
}

Try the backports package in your browser

Any scripts or data that you put into this service are public.

backports documentation built on June 24, 2024, 5:24 p.m.