Reputation: 10152
I am trying to extract the SYMBOLs from a string that represents code using the getParseData()
function. The code works when I run it in my normal session, but when I run it in a testthat::test_that
it fails and returns a NULL.
Furthermore, when I execute the examples of getParseData()
, I also get a NULL and not the expected parsed data.
E.g.
# from ?getParseData
fn <- function(x) {
x + 1 # A comment, kept as part of the source
}
getParseData(fn)
#> NULL
# I would expect a data.frame here!
Note that when I click "Run Examples" in the RStudio helper tab, the expected result is shown...
Or with code as string (my main use case)
# this is what I expect; works when I execute it directly
getParseData(parse(text = code))
#> line1 col1 line2 col2 id parent token terminal text
#> 7 1 1 1 8 7 0 expr FALSE
#> 1 1 1 1 3 1 3 SYMBOL TRUE mpg
#> 3 1 1 1 3 3 7 expr FALSE
#> 2 1 5 1 5 2 7 GT TRUE >
#> 4 1 7 1 8 4 5 NUM_CONST TRUE 10
#> 5 1 7 1 8 5 7 expr FALSE
Similarly, when I use this in a testthat case
test_that("getPaseData", {
code <- "mpg > 10"
print("getParseData(parse(text = code))")
print(getParseData(parse(text = code))) # for debugging
expect_equal(class(getParseData(parse(text = code))), "data.frame")
})
The tests pass when I call the code directly, but when I click on "Run Tests" in RStudio it fails with NULL.
(eg in the picture below the purple arrows show what happens when I click RUN TESTS and it fails; the green arrow shows that the tests pass when I run it from the console.
Upvotes: 0
Views: 31