Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# testthat (development version)

* `expect_snapshot_value()` can now handle expressions that generate `-` (#1678) or zero length atomic vectors (#2042).
* `expect_matches()` failures should be a little easier to read (#2135).
* New `local_on_cran(TRUE)` allows you to simulate how your tests will run on CRAN (#2112).
* `expect_no_*()` now executes the entire code block, rather than stopping at the first message or warning (#1991).
Expand Down
49 changes: 13 additions & 36 deletions R/snapshot-value.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,43 +77,20 @@ expect_snapshot_value <- function(
# Safe environment for evaluating deparsed objects, based on inspection of
# https://github.com/wch/r-source/blob/5234fe7b40aad8d3929d240c83203fa97d8c79fc/src/main/deparse.c#L845
reparse <- function(x) {
env <- env(
emptyenv(),
`-` = `-`,
c = c,
list = list,
quote = quote,
structure = structure,
expression = expression,
`function` = `function`,
new = methods::new,
getClass = methods::getClass,
pairlist = pairlist,
alist = alist,
as.pairlist = as.pairlist
)

eval(parse(text = x), env)
}

# Safe environment for evaluating deparsed objects, based on inspection of
# https://github.com/wch/r-source/blob/5234fe7b40aad8d3929d240c83203fa97d8c79fc/src/main/deparse.c#L845
reparse <- function(x) {
env <- env(
emptyenv(),
`-` = `-`,
c = c,
list = list,
quote = quote,
structure = structure,
expression = expression,
`function` = `function`,
new = methods::new,
getClass = methods::getClass,
pairlist = pairlist,
alist = alist,
as.pairlist = as.pairlist
env <- env(emptyenv())
env_bind(
env,
!!!env_get_list(
base_env(),
c(
c("c", "structure", ":", "-"),
c("list", "numeric", "integer", "logical", "character"),
"function",
c("quote", "alist", "pairlist", "as.pairlist", "expression")
)
)
)
env_bind(env, !!!env_get_list(ns_env("methods"), c("new", "getClass")))

eval(parse(text = x), env)
}
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-snapshot-value.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ test_that("reparse handles common cases", {
expect_equal(roundtrip(c(1, 2, 3)), c(1, 2, 3))
expect_equal(roundtrip(list(1, 2, 3)), list(1, 2, 3))
expect_equal(roundtrip(mtcars), mtcars)
expect_equal(roundtrip(1:10), 1:10)
expect_equal(roundtrip(numeric()), numeric())

f <- function(x) x + 1
expect_equal(roundtrip(f), f, ignore_function_env = TRUE)
Expand Down
Loading