tests/testthat/test-eval.R

test_that("log_echo causes output to be immediately written to stderr()", {
  f <- function() {
    1
  }
  out <- capture.output(
    res <- evaluate("f()", log_echo = TRUE),
    type = "message"
  )
  expect_equal(out, "f()")

  # But still recorded in eval result
  expect_length(res, 2)
  expect_equal(res[[1]]$src, "f()")
})

test_that("log_warning causes warnings to be immediately written to stderr()", {
  f <- function() {
    warning("Hi!", immediate. = TRUE)
  }
  out <- capture.output(
    res <- evaluate("f()", log_warning = TRUE),
    type = "message"
  )
  expect_equal(out, "Warning in f(): Hi!")

  # But still recorded in eval result
  expect_length(res, 2)
  expect_equal(res[[1]]$src, "f()")
  expect_equal(res[[2]], simpleWarning("Hi!", quote(f())))
})

test_that("show_warning handles different types of warning", {

  expect_snapshot({
    w1 <- simpleWarning("This is a warning")
    cat(format_warning(w1))
    w2 <- simpleWarning("This is a warning", call = quote(f()))
    cat(format_warning(w2))
    w3 <- rlang::warning_cnd(message = "This is a warning")
    cat(format_warning(w3))
    w4 <- rlang::warning_cnd(message = "This is a warning")
    cat(format_warning(w4))
  })

})

Try the evaluate package in your browser

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

evaluate documentation built on Nov. 2, 2023, 5:18 p.m.