Skip to content

undoc'd string length limitation on NSE replacement for 'info' #614

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

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

undoc'd string length limitation on NSE replacement for 'info' #614

r2evans opened this issue Aug 10, 2017 · 1 comment

Comments

@r2evans
Copy link

r2evans commented Aug 10, 2017

I believe you are considering (or actively) deprecating the info argument to many expect functions. I understand the value of using NSE for label, but it has a length limitation I'm butting my head against.

Working example: a function that takes an input file, does something to it (silently), and returns a data.frame. Because the processing is non-trivial, I want to do it only once per file, so I capture the return value after testing for silence (ergo expect_silence2). I need to run it twice on each file (diff arguments).

Taking a cue from your tests chapter, I believe a way to do this is something like:

my_complex_function <- function(fn, add1 = FALSE) data.frame(x = seq_len(nchar(fn)) + add1)
expect_silent2 <- function(obj) { expect_silent(o <- obj) ; o ; }
expect_mytests <- function(fn) {
  out0 <- expect_silent2(my_complex_function(fn, add1 = FALSE))
  out1 <- expect_silent2(my_complex_function(fn, add1 = TRUE))
  nr <- function(a, b) nrow(b)
  eval(bquote(expect_lt(nr(.(fn), out0), 10)))
  eval(bquote(expect_lt(nr(.(fn), out1), 10)))
}

Ideally, I'd run it as for (fn in allfiles) expect_mytests(fn), but for the sake of this issue:

expect_mytests(strrep("b", 9))
expect_mytests(strrep("b", 53))
# Error: nr("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", out0) is not strictly less than 10. Difference: 43
expect_mytests(strrep("b", 54))
# Error: nr(...) is not strictly less than 10. Difference: 89

First: we've lost the NSE info on the last call (not dependent on options("width")). Filenames can be (and in my use, "are often") over 53 characters, and this complete loss is unexpected. I can strategically truncate the string, though this is prone to "works best for me" syndrome (e.g., first 50? last 50? first/last 25?).

Second: the only way I can imagine providing that file-specificity with the failure messages is to add it as an unused argument to my nr function. This works, but it seems a bit kludgy. Is this the only way to include the filename (in this example) within the failure message?

(Third: is this really the best/only way to get the info stuff into the failure message?)

As I'm writing this ... if I switch the order of the arguments within nr, this goes away:

expect_mytests <- function(fn) {
  out0 <- expect_silent2(my_complex_function(fn, add1 = FALSE))
  out1 <- expect_silent2(my_complex_function(fn, add1 = TRUE))
  nr <- function(a, ...) nrow(a) # arguably better-looking, anyway
  eval(bquote(expect_lt(nr(out0, .(fn)), 10)))
  eval(bquote(expect_lt(nr(out1, .(fn)), 10)))
}
expect_mytests(strrep("b", 99))
# Error: nr(out0, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") is not strictly less than 10. Difference: 89

Is this a limitation of eval or bquote, or is this something testthat is imposing? (I could not find "54" ...)

Related issue refs:

@hadley
Copy link
Member

hadley commented Oct 1, 2017

We're going to replace this again with quasiquotation, so please see #626

@hadley hadley closed this as completed Oct 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants