Debian Codegraph @

tests/testthat/test-visibility.R

raw
1 library(testthat)
2
3 describe("visibility", {
4 single_fn <- function(value) {
5 info <- withVisible(value)
6 if (info$visible) {
7 info$value
8 } else {
9 invisible(info$value)
10 }
11 }
12 double_fn <- function(value, .visible) {
13 if (.visible) value else invisible(value)
14 }
15
16 # display in block to avoid indent of doom
17 for (add_catch in c("false", "single", "double", "expr")) {
18 for (add_finally in c("false", "expr")) {
19 for (add_then in c("false", "single", "double", "expr")) {
20 it(
21 paste0(
22 "survives ",
23 paste0(
24 c(
25 if (add_then != "false") paste0("then-", add_then),
26 if (add_catch != "false") paste0("catch-", add_catch),
27 if (add_finally != "false") paste0("finally-", add_finally),
28 "then"
29 ),
30 collapse = ", "
31 )
32 ),
33 {
34 p <- promise_resolve(invisible(1))
35
36 p <-
37 switch(
38 add_then,
39 "false" = p,
40 "single" = p |> then(single_fn),
41 "double" = p |> then(double_fn),
42 "expr" = p |>
43 then(
44 \(value) {
45 info <- withVisible(value)
46 if (info$visible) {
47 info$value
48 } else {
49 invisible(info$value)
50 }
51 }
52 )
53 )
54 p <-
55 switch(
56 add_catch,
57 "false" = p,
58 "single" = p |> catch(single_fn),
59 "double" = p |> catch(double_fn),
60 "expr" = p |>
61 catch(
62 \(err) {}
63 )
64 )
65
66 finally_val <- NULL
67 p <-
68 switch(
69 add_finally,
70 "false" = p,
71 "expr" = p |>
72 finally(\() {
73 finally_val <<- TRUE
74 })
75 )
76
77 extended_val <-
78 p |>
79 then(function(value, .visible) {
80 list(value = value, visible = .visible)
81 }) |>
82 extract()
83
84 regular_val <-
85 p |>
86 then(function(value) {
87 withVisible(value)
88 }) |>
89 extract()
90
91 if (add_finally != "false") {
92 expect_true(finally_val)
93 }
94
95 expect_identical(extended_val$value, 1)
96 expect_identical(extended_val$visible, FALSE)
97
98 expect_identical(regular_val$value, 1)
99 expect_identical(regular_val$visible, FALSE)
100 }
101 )
102 }
103 }
104 }
105 })