Debian Codegraph @

tests/testthat/test-hybrid-then-visibility.R

raw
1 test_that("hybrid_then() preserves visible values from expr (no callback)", {
2 result <- hybrid_then(42)
3 expect_equal(result, 42)
4 # Check visibility by capturing with withVisible
5 vis <- withVisible(hybrid_then(42))
6 expect_true(vis$visible)
7 })
8
9 test_that("hybrid_then() with invisible expr (no callback)", {
10 result <- hybrid_then(invisible(42))
11 expect_equal(result, 42)
12 vis <- withVisible(hybrid_then(invisible(42)))
13 expect_false(vis$visible)
14 })
15
16 test_that("hybrid_then() with visible expr and on_success returning visible", {
17 result <- hybrid_then(
18 42,
19 on_success = \(x) x + 8
20 )
21 expect_equal(result, 50)
22 vis <- withVisible(hybrid_then(42, on_success = \(x) x + 8))
23 expect_true(vis$visible)
24 })
25
26 test_that("hybrid_then() with visible expr and on_success returning invisible", {
27 result <- hybrid_then(
28 42,
29 on_success = \(x) invisible(x + 8)
30 )
31 expect_equal(result, 50)
32 vis <- withVisible(hybrid_then(42, on_success = \(x) invisible(x + 8)))
33 expect_false(vis$visible)
34 })
35
36 test_that("hybrid_then() with invisible expr and on_success returning visible", {
37 result <- hybrid_then(
38 invisible(42),
39 on_success = \(x) x + 8
40 )
41 expect_equal(result, 50)
42 vis <- withVisible(hybrid_then(invisible(42), on_success = \(x) x + 8))
43 expect_true(vis$visible)
44 })
45
46 test_that("hybrid_then() with invisible expr and on_success returning invisible", {
47 result <- hybrid_then(
48 invisible(42),
49 on_success = \(x) invisible(x + 8)
50 )
51 expect_equal(result, 50)
52 vis <- withVisible(hybrid_then(invisible(42), on_success = \(x) {
53 invisible(x + 8)
54 }))
55 expect_false(vis$visible)
56 })
57
58 test_that("hybrid_then() with tee=TRUE preserves expr visibility (visible expr)", {
59 result <- hybrid_then(
60 42,
61 on_success = \(x) invisible(999),
62 tee = TRUE
63 )
64 expect_equal(result, 42)
65 vis <- withVisible(hybrid_then(
66 42,
67 on_success = \(x) invisible(999),
68 tee = TRUE
69 ))
70 expect_true(vis$visible)
71 })
72
73 test_that("hybrid_then() with tee=TRUE preserves expr visibility (invisible expr)", {
74 result <- hybrid_then(
75 invisible(42),
76 on_success = \(x) 999, # callback returns visible
77 tee = TRUE
78 )
79 expect_equal(result, 42)
80 vis <- withVisible(hybrid_then(
81 invisible(42),
82 on_success = \(x) 999,
83 tee = TRUE
84 ))
85 expect_false(vis$visible)
86 })
87
88 test_that("hybrid_then() on_success receives .visible parameter correctly (visible)", {
89 visible_captured <- NULL
90 result <- hybrid_then(
91 42,
92 on_success = \(x, .visible) {
93 visible_captured <<- .visible
94 x * 2
95 }
96 )
97 expect_equal(result, 84)
98 expect_true(visible_captured)
99 })
100
101 test_that("hybrid_then() on_success receives .visible parameter (invisible)", {
102 visible_captured <- NULL
103 result <- hybrid_then(
104 invisible(42),
105 on_success = \(x, .visible) {
106 visible_captured <<- .visible
107 x * 2
108 }
109 )
110 expect_equal(result, 84)
111 expect_false(visible_captured)
112 })
113
114 test_that("hybrid_then() on_success with .visible conditionally returning", {
115 make_conditional <- function(val) {
116 hybrid_then(
117 val,
118 on_success = \(x, .visible) {
119 if (.visible) {
120 x * 2
121 } else {
122 invisible(x * 2)
123 }
124 }
125 )
126 }
127
128 vis1 <- withVisible(make_conditional(42))
129 expect_equal(vis1$value, 84)
130 expect_true(vis1$visible)
131
132 vis2 <- withVisible(make_conditional(invisible(42)))
133 expect_equal(vis2$value, 84)
134 expect_false(vis2$visible)
135 })
136
137 test_that("hybrid_then() async with visible promise and on_success returning visible", {
138 expect_assertions(n = 2, {
139 result <- hybrid_then(
140 promise_resolve(42),
141 on_success = \(x) x + 8
142 )
143 expect_true(is.promise(result))
144 result |> extract() |> expect_equal(50)
145 })
146 })
147
148 test_that("hybrid_then() async with visible promise and on_success returning invisible", {
149 expect_assertions(n = 2, {
150 result <- hybrid_then(
151 promise_resolve(42),
152 on_success = \(x) invisible(x + 8)
153 )
154 expect_true(is.promise(result))
155 result |> extract() |> expect_equal(50)
156 })
157 })
158
159 test_that("hybrid_then() async visibility is handled by underlying then()", {
160 expect_assertions(n = 3, {
161 # The visibility semantics for async are handled by then()
162 # Just verify the values work correctly
163 visible_captured <- NULL
164 result <- hybrid_then(
165 promise_resolve(42),
166 on_success = \(x, .visible) {
167 visible_captured <<- .visible
168 x * 2
169 }
170 )
171 expect_true(is.promise(result))
172 result |> extract() |> expect_equal(84)
173 expect_true(visible_captured)
174 })
175 })
176
177 test_that("hybrid_then() with tee=TRUE and visible expr", {
178 # With tee=TRUE, expr visibility should be preserved regardless of callback
179 vis_test <- function(expr_vis, callback_vis) {
180 withVisible(
181 hybrid_then(
182 if (expr_vis) 42 else invisible(43),
183 on_success = if (callback_vis) {
184 \(x) x * 2
185 } else {
186 \(x) invisible(x * 3)
187 },
188 tee = TRUE
189 )
190 )
191 }
192
193 # Visible expr preserves visibility
194 expect_equal(vis_test(TRUE, TRUE)$value, 42)
195 expect_true(vis_test(TRUE, TRUE)$visible)
196
197 expect_equal(vis_test(TRUE, FALSE)$value, 42)
198 expect_true(vis_test(TRUE, FALSE)$visible)
199
200 expect_equal(vis_test(FALSE, TRUE)$value, 43)
201 expect_false(vis_test(FALSE, TRUE)$visible)
202
203 expect_equal(vis_test(FALSE, FALSE)$value, 43)
204 expect_false(vis_test(FALSE, FALSE)$visible)
205 })
206
207 test_that("hybrid_then() no callback with visible expr preserves visibility", {
208 # Visible expr
209 vis1 <- withVisible(hybrid_then(42))
210 expect_equal(vis1$value, 42)
211 expect_true(vis1$visible)
212 })
213
214 test_that("hybrid_then() no callback with invisible expr", {
215 # Invisible expr via invisible()
216 vis2 <- withVisible(hybrid_then(invisible(42)))
217 expect_equal(vis2$value, 42)
218 expect_false(vis2$visible)
219
220 # Expression that returns invisible
221 vis3 <- withVisible(hybrid_then({
222 x <- 42
223 invisible(x)
224 }))
225 expect_equal(vis3$value, 42)
226 expect_false(vis3$visible)
227 })
228
229 test_that("hybrid_then() on_failure with invisible error recovery", {
230 result <- hybrid_then(
231 stop("error"),
232 on_failure = \(err) invisible(0)
233 )
234 expect_equal(result, 0)
235 vis <- withVisible(hybrid_then(stop("error"), on_failure = \(err) {
236 invisible(0)
237 }))
238 expect_false(vis$visible)
239 })
240
241 test_that("hybrid_then() on_failure with visible error recovery", {
242 result <- hybrid_then(
243 stop("error"),
244 on_failure = \(err) 0
245 )
246 expect_equal(result, 0)
247 vis <- withVisible(hybrid_then(stop("error"), on_failure = \(err) 0))
248 expect_true(vis$visible)
249 })