[R-pkg-devel] Testing for a Specific R Error
Bill Denney
bill at denney.ws
Sat Dec 2 20:19:47 CET 2017
> On Dec 2, 2017, at 09:43, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
>
> I don't think there's anything better than Bill's solution, though I imagine it is possible to ask for translation of the message. For example, sqrt(-1) currently gives a warning with English message "NaNs produced".
Another idea just occurred to me. Get the current error text as I'm expecting it from R by generating the error in a tryCatch and then test for equality to that string. With that, changes in R (or other packages) would be automatically updated, and it would still be a precise test for the error I'm wanting to confirm in my test:
tryCatch(as.data.frame(structure(1, class="foo")), error=function(e) e$message)
Capture the output of that tryCatch and test for equality.
Then, it will be robust to language changes and to changes in R or other packages.
I'm going to implement that unless someone indicates something that I'm missing.
Thanks,
Bill
>
> I can ask to translate that into the current session language using
>
> gettext("NaNs produced", domain = "R")
>
> Figuring out the right thing for "domain" is likely a little painful: it depends on which package produced the message, and how.
>
> I don't know if CRAN tests would complain if you tested for equality between a warning message and the result of gettext(): it's still true that if the English warning changed, the test would fail.
>
> Duncan Murdoch
More information about the R-package-devel
mailing list