Skip to content

Commit

Permalink
test #2344: call dep_auto() automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
atusy committed Oct 17, 2024
1 parent 88921d1 commit 8b06f7b
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions tests/testit/test-cache.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
library(testit)

dep_list$restore()
knit_code$restore()


assert('find_symbols() identifies all symbols', {
(find_symbols('x = x + 1; rnorm(1, std = z)') %==% c('x', 'rnorm', 'z'))
})

knit_lazy = function(lazy = TRUE) {
if (TRUE) return(TRUE)
in_dir(tempdir(), {
txt = c(sprintf('```{r test, cache=TRUE, cache.lazy=%s}', lazy),
'x1 = Sys.time()', '```')
Expand All @@ -30,3 +35,79 @@ assert('dep_prev() sets dependencies on previous chunks', {
})
dep_list$restore()
knit_code$restore()

assert('dep_auto() solves dependencies', {
# dependency is empty now
(dep_list$get() %==% list())

# base rmd text
rmd0 = c(
'```{r, autodep=TRUE, cache=TRUE}',
'x = %s',
'```',
'```{r, autodep=TRUE, cache=TRUE}',
'print(x)',
'```'
)

td = tempfile()
dir.create(td, showWarnings = FALSE, recursive = TRUE)

rmd1 = sprintf(rmd0, 'runif(1)')
rmd2 = sprintf(rmd0, '"a"')

# without child document
in_dir(td, {
# with cache, the result should reproduce
knit1 = knit(text = rmd1, quiet = TRUE)
(knit(text = rmd1, quiet = TRUE) %==% knit1)

# on updating `x`, the printed result should change
knit2 = knit(text = rmd2, quiet = TRUE)
print2 = gsub('\n.*', '', gsub('.*\n##', '##', knit2))
(print2 %==% '## [1] "a"')
})
})
dep_list$restore()
knit_code$restore()

assert('dep_auto() solves dependencies of child documents', {
# dependency is empty now
(dep_list$get() %==% list())

# base rmd text
rmd0 = c(
'```{r, autodep=TRUE, cache=TRUE}',
'x = %s',
'```',
'```{r, autodep=TRUE, cache=TRUE}',
'print(x)',
'```'
)
rmd1 = sprintf(rmd0, 'runif(1)')
rmd2 = sprintf(rmd0, '"a"')

td = tempfile()
dir.create(td, showWarnings = FALSE, recursive = TRUE)

# with child document
parent = c(
'```{r, child="child.Rmd"}',
'```'
)
in_dir(td, {
# with cache, the result should reproduce
writeLines(rmd1, 'child.Rmd')
knit1 = knit(text = parent, quiet = TRUE)
(knit(text = parent, quiet = TRUE) %==% knit1)

# on updating `x`, the printed result should change
writeLines(rmd2, 'child.Rmd')
knit2 = knit(text = parent, quiet = TRUE)
print2 = gsub('\n.*', '', gsub('.*\n##', '##', knit2))
(print2 %==% '## [1] "a"')
})
})
dep_list$restore()
knit_code$restore()

0 comments on commit 8b06f7b

Please sign in to comment.