New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change expect_silent to include object output #615

Closed
r2evans opened this Issue Aug 10, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@r2evans

r2evans commented Aug 10, 2017

(This is softly mentioned #614, and related to it since I'm trying to replace info functionality.)

I want to test for silence (or warning or error) from a function and still test its output. If a function is "expensive", I'd much rather not run it multiple times. However, the current expect_silent and family explicitly discard the object upon execution.

Right now I can do

expect_silent2 <- function(obj) { expect_silent(o <- obj) ; o ; }

but this does not work using the NSE hack for replacing info. That is:

expect_silent(myfunc(FALSE))
expect_silent(myfunc(TRUE))
# Error: myfunc(TRUE) produced warnings.
expect_silent2(myfunc(FALSE))
# [1] FALSE
expect_silent2(myfunc(TRUE))
# Error: o <- obj produced warnings.

That last error message isn't very useful, so I try the NSE trick:

expect_silent3 <- function(obj) { eval(bquote(expect_silent(.(o <- obj)))) ; o ; }
expect_silent3(myfunc(TRUE))
# Warning in myfunc(TRUE) : quux
# [1] TRUE

expect_silent4 <- function(obj) { eval(bquote(expect_silent(o <- .(obj)))) ; o ; }
expect_silent4(myfunc(TRUE))
# Warning in myfunc(TRUE) : quux
# [1] TRUE

expect_silent5 <- function(obj) eval(bquote(expect_silent2(.(obj))))
expect_silent5(myfunc(TRUE))
# Warning in myfunc(TRUE) : quux
# [1] TRUE

but none of them triggers the Error: myfunc(TRUE) produced warnings I would expect.

@r2evans r2evans changed the title from expect_silent omitting the output to change expect_silent to include object output Aug 10, 2017

@hadley

This comment has been minimized.

Member

hadley commented Oct 1, 2017

Just do the assignment inside the expectation:

expect_silent(x <- foo())
expect_equal(x, 10)

But you already know how to do that so I'm not sure what you want.I don't think it make sense for some expectation()

...

Ooooh, all the other expectations return the first object invisibly, so expect_silent() etc should be consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment