[R-pkg-devel] Testing for a Specific R Error

Duncan Murdoch murdoch.duncan at gmail.com
Sat Dec 2 21:42:26 CET 2017


On 02/12/2017 2:19 PM, Bill Denney wrote:
> 
>> 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.

That sounds like a really good idea.  It should work in your use case, 
though in cases where the error message includes information about the 
arguments that led to the error (e.g. vignette("foobar") says "vignette 
‘foobar’ not found" in English and "vignette ‘foobar’ introuvable" in 
French) it might be harder.

Duncan Murdoch

> 
> 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