Skip to content
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

r2evans opened this issue Aug 10, 2017 · 1 comment

change expect_silent to include object output #615

r2evans opened this issue Aug 10, 2017 · 1 comment


Copy link

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

# Error: myfunc(TRUE) produced warnings.
# [1] FALSE
# 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 ; }
# Warning in myfunc(TRUE) : quux
# [1] TRUE

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

expect_silent5 <- function(obj) eval(bquote(expect_silent2(.(obj))))
# 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 expect_silent omitting the output change expect_silent to include object output Aug 10, 2017
Copy link

@hadley 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
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants