tests/testthat/test-handlers.R

context("handlers")

test_that("Verify options are set.", {

  enable_errorist()

  # Retrieve errorist helper
  test_call = getOption("error")

  # Retrieve the default call-type
  default_call = getOption("errorist.warning",
                           searcher::search_google)

  expect_true(is.call(test_call),
              "Verify that extracted error handler is of type call")

  # needle -> haystack
  expect_true("ErroristWarningHandler" %in% getTaskCallbackNames(),
               "Check to see the Warning Handler is set")


  disable_errorist()

  # Retrieve error handler after errorist's bindings are removed.
  restored_error_call = getOption("error")

  # Determine if in RStudio
  is_rstudio = function() Sys.getenv("RSTUDIO") == "1"

  # This check attempts to figure out the state of the handlers by:
  # 1. checking if an error option wasn't set.
  # 2. checking if we are in rstudio and a call is still present.
  # The latter is important as RStudio setups custom handler for traceback()
  # et cetera routines.
  expect_true(is.null(restored_error_call) || (is_rstudio() && is.call(restored_error_call)),
              "Check to see error handler is null or a call (RStudio may inject)")

  # !needle -> haystack
  expect_false("ErroristWarningHandler" %in% getTaskCallbackNames(),
                "Check to see the Warning Handler was removed from callback")

})


test_that("Check Output from Functions", {

  enable_errorist()

  # Retrieve errorist helper
  test_error_call = getOption("error")
  options_default_function = getOption("errorist.warning", searcher::search_google)

  # Retrieve the function
  test_error_function = test_error_call[[1]]

  expect_true(is.function(options_default_function),
              "Verify the default error handler is a function.")

  expect_true(identical(environment(test_error_function),
                        environment(options_default_function)),
              "Verify that test function call matches the default function's environment")

  expect_equal(test_error_function("toad"), options_default_function("toad"),
               "Check that the search query term is the same.")

  ## TO DO: Make more robust
  # For right now, this should satisfy the sanity check of default functions.
  # However, as Hadley pointed out, "the first element of a call is not always
  # a symbol." As an example, he gave: `quote(f()()())`, which gives another call.

  # Read: https://adv-r.hadley.nz/expressions.html#abstract-syntax-trees

  ## Cannot really interface with the addTaskCallback()... This should
  ## always return a "TRUE" with a warning check enabled.

  disable_errorist()

})
coatless/errorist documentation built on Nov. 16, 2023, 9:45 p.m.