Skip to content

Nuance of Testing Environment #13

@bryanhanson

Description

@bryanhanson

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions