Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testthat: handling both warning and value

Tags:

r

testthat

What's the best way to handle calls that generate a warning but then also return a value?

e.g.

> require(testthat)
> expect_warning(log(-1))
> expect_equal(log(-1), NaN)
Warning message:
In log(-1) : NaNs produced

I want to write the test such that the call to log(-1) should both (a) generate a warning and (b) return the value NaN. The way above works, but seeing the "Warning message:" at the bottom might confuse people. Should I suppress warnings temporarily?

like image 608
Stephen Eglen Avatar asked Jan 02 '14 15:01

Stephen Eglen


1 Answers

require(testthat)
expect_warning(val <- log(-1))
expect_true(is.nan(val))
like image 91
BrodieG Avatar answered Oct 15 '22 07:10

BrodieG