Closed lentinj closed 1 month ago
catch_warnings <- function(expr) {
ww <- NULL
wh <- function(w) {
ww <<- append(ww, w$message)
invokeRestart("muffleWarning")
}
got <- withCallingHandlers(expr, warning=wh)
list(got=got, warnings=ww)
}
No, relying on the ordering is bad, but the count still seems useful to check.
I've just put this together:
It expects a character vector of regexes, one per warning, and they should be in order.
Insisting that we provide one regex per-warning is probably reasonable, if they all match you ought still care about the number of warnings. You could do something like
rep(regex, count)
to say you're expecting count warnings matching regex.What's more awkward is if you want to check for your warnings, but ignore third-party warnings out of your control. We could instead make sure there is at least one warning matching each regex, but I'm more tempted to say "if you really want to ignore extra warnings, then you're on your own, this isn't something we encourage".
EDIT: Insisting they're in order is also debatable, but it's pretty unlikely you're testing multi-threaded code.