Skip to content

Commit 13e102e

Browse files
authored
Add more functions to reparse() (#2147)
Fixes #1678. Fixes #2042.
1 parent 383e227 commit 13e102e

File tree

3 files changed

+16
-36
lines changed

3 files changed

+16
-36
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* `expect_snapshot_value()` can now handle expressions that generate `-` (#1678) or zero length atomic vectors (#2042).
34
* `expect_matches()` failures should be a little easier to read (#2135).
45
* New `local_on_cran(TRUE)` allows you to simulate how your tests will run on CRAN (#2112).
56
* `expect_no_*()` now executes the entire code block, rather than stopping at the first message or warning (#1991).

R/snapshot-value.R

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -77,43 +77,20 @@ expect_snapshot_value <- function(
7777
# Safe environment for evaluating deparsed objects, based on inspection of
7878
# https://github.com/wch/r-source/blob/5234fe7b40aad8d3929d240c83203fa97d8c79fc/src/main/deparse.c#L845
7979
reparse <- function(x) {
80-
env <- env(
81-
emptyenv(),
82-
`-` = `-`,
83-
c = c,
84-
list = list,
85-
quote = quote,
86-
structure = structure,
87-
expression = expression,
88-
`function` = `function`,
89-
new = methods::new,
90-
getClass = methods::getClass,
91-
pairlist = pairlist,
92-
alist = alist,
93-
as.pairlist = as.pairlist
94-
)
95-
96-
eval(parse(text = x), env)
97-
}
98-
99-
# Safe environment for evaluating deparsed objects, based on inspection of
100-
# https://github.com/wch/r-source/blob/5234fe7b40aad8d3929d240c83203fa97d8c79fc/src/main/deparse.c#L845
101-
reparse <- function(x) {
102-
env <- env(
103-
emptyenv(),
104-
`-` = `-`,
105-
c = c,
106-
list = list,
107-
quote = quote,
108-
structure = structure,
109-
expression = expression,
110-
`function` = `function`,
111-
new = methods::new,
112-
getClass = methods::getClass,
113-
pairlist = pairlist,
114-
alist = alist,
115-
as.pairlist = as.pairlist
80+
env <- env(emptyenv())
81+
env_bind(
82+
env,
83+
!!!env_get_list(
84+
base_env(),
85+
c(
86+
c("c", "structure", ":", "-"),
87+
c("list", "numeric", "integer", "logical", "character"),
88+
"function",
89+
c("quote", "alist", "pairlist", "as.pairlist", "expression")
90+
)
91+
)
11692
)
93+
env_bind(env, !!!env_get_list(ns_env("methods"), c("new", "getClass")))
11794

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

tests/testthat/test-snapshot-value.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ test_that("reparse handles common cases", {
2424
expect_equal(roundtrip(c(1, 2, 3)), c(1, 2, 3))
2525
expect_equal(roundtrip(list(1, 2, 3)), list(1, 2, 3))
2626
expect_equal(roundtrip(mtcars), mtcars)
27+
expect_equal(roundtrip(1:10), 1:10)
28+
expect_equal(roundtrip(numeric()), numeric())
2729

2830
f <- function(x) x + 1
2931
expect_equal(roundtrip(f), f, ignore_function_env = TRUE)

0 commit comments

Comments
 (0)