markvanderloo / tinytest

A lightweight, no-dependency, but full-featured package for unit testing in R
GNU General Public License v3.0
223 stars 20 forks source link

Nuance of Testing Environment #13

Closed bryanhanson closed 5 years ago

bryanhanson commented 5 years ago

I'm converting a package from testthat to tinytest. I ran into this error, which admittedly comes from a function I adapted from StackOverflow, but does work with testthat and of course, in the package. Maybe the helper function needs to be modified?

# Simplified .chkArgs Function which checks classes of arguments in the calling function
# mode indicates acceptable classes of arguments

.chkArgs <- function(mode = 0L) {

  # The following is from stackoverflow.com/a/53137483/633251
  fargs <- function(n) { mget(names(formals(sys.function(n))), sys.frame(n), inherits = TRUE); }
  args <- fargs(-2);  # 2 because the helper function fargs is yet another level down

  # print(data.frame(cls = unlist(lapply(args, class)))) # save for debugging

  if (mode == 0L) {
    specOK <- FALSE
    specOK <-  any("Spectra" %in% class(args$spectra), "Spectra2D" %in% class(args$spectra))
    if (!specOK) stop("Argument 'spectra' was not found or did not have class Spectra or Spectra2D")
  }

  # other modes dropped for this simple demo
}

# Simple test function; ALWAYS call with ALL arguments
tf <- function(spectra, pca, mode) {
    .chkArgs(mode) # check the classes of 1st and maybe 2nd argument
}

library("tinytest")
# pass integers as 1st two arguments, which are not expected
expect_error(tf(12, 12, 0), "did not have class Spectra or Spectra2D") # error here

# For comparison, the testthat equivalent which works as expected

library("testthat")

test_that("chkArgs detects missing Spectra or Spectra2D object mode 0", {
  expect_error(tf(12, 12, 0))
})

The error I see is

Error in sys.frame(-2) : not that many frames on the stack

Any suggestions?

markvanderloo commented 5 years ago

Hi Bryan,

Thanks for this example. This is probably something that needs to be treated more carefully by tinytest.

markvanderloo commented 5 years ago

Resolved in https://github.com/markvanderloo/tinytest/commit/dda56cb68ce2141518c13a9e6a43a93e5bba6fcc