add pattern matching to checkException
Brought to you by:
burgerm
Right now, checkException returns TRUE iff 'expr' generates an error. Would it be possible to check the error message against a regex pattern so the user can tell what kind of error he is expecting, not just any error. This could be done with an extra option to checkException.
Here is an example:
my.sqrt <- function(x) {
if (!inherits(x, c("numeric","integer"))) stop("x is non-numeric")
if (any(x<0)) stop("x has non-positive values")
return(sqrt(x))
}
test.my.sqrt <- function() {
checkException(msg = "check non-numeric exception",
expr = my.sqrt("hi")
pattern = "non-numeric")
checkException(msg = "check non-positive",
expr = my.sqrt(-1:1)
pattern = "non-positive")
}
Thank you for your consideration,
Florent.