Debian Codegraph @

tests/testthat/test-defer.R

raw
1 test_that("`frame_exits()` and `frame_clear_exits()`", {
2 on.exit("foo")
3 expect_equal(frame_exits(), list("foo"))
4
5 on.exit("bar", add = TRUE)
6 expect_equal(frame_exits(), list("foo", "bar"))
7
8 on.exit("baz", add = TRUE, after = FALSE)
9 expect_equal(frame_exits(), list("baz", "foo", "bar"))
10
11 frame_clear_exits()
12 expect_equal(frame_exits(), list())
13 })
14
15 test_that("deferred_run() displays how many expressions were run", {
16 expect_snapshot(deferred_run())
17
18 expect_snapshot(local({
19 local_tempfile()
20 local_tempfile()
21 deferred_run()
22 }))
23
24 expect_snapshot(expect_error(local({
25 local_tempfile()
26 defer(stop("foo"))
27 local_tempfile()
28 deferred_run()
29 })))
30 })
31
32 test_that("can't run `deferred_run()` in knitr", {
33 skip_if_cannot_knit()
34
35 rmd <- local_tempfile(fileext = ".Rmd")
36 out <- local_tempfile(fileext = ".md")
37 writeLines(rmd, text = "
38 ```{r}
39 withr::deferred_run()
40 ```
41 ```{r}
42 defer(writeLines('1'))
43 writeLines('2')
44 defer(writeLines('3'))
45 ```
46 ```{r}
47 writeLines('4')
48 withr::deferred_run()
49 ```
50 ")
51
52 knitr::knit(rmd, out, quiet = TRUE)
53
54 expect_snapshot({
55 writeLines(readLines(out))
56 })
57 })