Debian Codegraph @

tests/testthat/test-hybrid-then.R

raw
1 test_that("hybrid_then() handles synchronous values with on_success", {
2 result <- hybrid_then(
3 42,
4 on_success = \(x) x + 8
5 )
6 expect_equal(result, 50)
7 })
8
9 test_that("hybrid_then() handles synchronous values without callbacks", {
10 result <- hybrid_then(42)
11 expect_equal(result, 42)
12 })
13
14 test_that("hybrid_then() handles synchronous errors with on_failure", {
15 expect_assertions(n = 2, {
16 result <- hybrid_then(
17 stop("sync error"),
18 on_failure = \(err) {
19 expect_match(err$message, "sync error")
20 "recovered"
21 }
22 )
23 expect_equal(result, "recovered")
24 })
25 })
26
27 test_that("hybrid_then() re-throws synchronous errors without on_failure", {
28 expect_error(
29 hybrid_then(stop("sync error")),
30 "sync error"
31 )
32 })
33
34 test_that("hybrid_then() handles asynchronous values with on_success", {
35 expect_assertions(n = 2, {
36 result <- hybrid_then(
37 promise_resolve(42),
38 on_success = \(x) x + 8
39 )
40 expect_true(is.promise(result))
41 result |> extract() |> expect_equal(50)
42 })
43 })
44
45 test_that("hybrid_then() handles asynchronous values without callbacks", {
46 expect_assertions(n = 2, {
47 result <- hybrid_then(promise_resolve(42))
48 expect_true(is.promise(result))
49 result |> extract() |> expect_equal(42)
50 })
51 })
52
53 test_that("hybrid_then() handles asynchronous errors with on_failure", {
54 expect_assertions(n = 3, {
55 result <- hybrid_then(
56 promise_resolve(stop("async error")),
57 on_failure = \(err) {
58 expect_match(err$message, "async error")
59 "recovered"
60 }
61 )
62 expect_true(is.promise(result))
63 result |> extract() |> expect_equal("recovered")
64 })
65 })
66
67 test_that("hybrid_then() propagates asynchronous errors without on_failure", {
68 expect_assertions(n = 2, {
69 result <- hybrid_then(promise_resolve(stop("async error")))
70 expect_true(is.promise(result))
71 result |>
72 catch(\(err) {
73 expect_match(err$message, "async error")
74 }) |>
75 wait_for_it()
76 })
77 })
78
79 test_that("hybrid_then() with tee=TRUE preserves synchronous values", {
80 counter <- 0
81 result <- hybrid_then(
82 42,
83 on_success = \(x) {
84 counter <<- counter + 1
85 99
86 },
87 tee = TRUE
88 )
89 expect_equal(result, 42)
90 expect_equal(counter, 1)
91 })
92
93 test_that("hybrid_then() with tee=TRUE preserves synchronous errors", {
94 counter <- 0
95 expect_error(
96 hybrid_then(
97 stop("sync error"),
98 on_failure = \(err) {
99 counter <<- counter + 1
100 "recovered"
101 },
102 tee = TRUE
103 ),
104 "sync error"
105 )
106 expect_equal(counter, 1)
107 })
108
109
110 test_that("hybrid_then() with tee=TRUE preserves asynchronous values", {
111 expect_assertions(n = 3, {
112 counter <- 0
113 result <- hybrid_then(
114 promise_resolve(42),
115 on_success = \(x) {
116 counter <<- counter + 1
117 99
118 },
119 tee = TRUE
120 )
121 expect_true(is.promise(result))
122 result |> extract() |> expect_equal(42)
123 expect_equal(counter, 1)
124 })
125 })
126
127 test_that("hybrid_then() with tee=TRUE preserves asynchronous errors", {
128 expect_assertions(n = 3, {
129 counter <- 0
130 result <- hybrid_then(
131 promise_resolve(stop("async error")),
132 on_failure = \(err) {
133 counter <<- counter + 1
134 "recovered"
135 },
136 tee = TRUE
137 )
138 expect_true(is.promise(result))
139 result |>
140 catch(\(err) {
141 expect_match(err$message, "async error")
142 }) |>
143 wait_for_it()
144 expect_equal(counter, 1)
145 })
146 })
147
148 test_that("hybrid_then() handles both callbacks with synchronous success", {
149 success_called <- FALSE
150 failure_called <- FALSE
151
152 result <- hybrid_then(
153 42,
154 on_success = \(x) {
155 success_called <<- TRUE
156 x * 2
157 },
158 on_failure = \(err) {
159 failure_called <<- TRUE
160 "error"
161 }
162 )
163
164 expect_equal(result, 84)
165 expect_true(success_called)
166 expect_false(failure_called)
167 })
168
169 test_that("hybrid_then() handles both callbacks with synchronous failure", {
170 success_called <- FALSE
171 failure_called <- FALSE
172
173 result <- hybrid_then(
174 stop("error"),
175 on_success = \(x) {
176 success_called <<- TRUE
177 x * 2
178 },
179 on_failure = \(err) {
180 failure_called <<- TRUE
181 "recovered"
182 }
183 )
184
185 expect_equal(result, "recovered")
186 expect_false(success_called)
187 expect_true(failure_called)
188 })
189
190 test_that("hybrid_then() handles both callbacks with asynchronous success", {
191 success_called <- FALSE
192 failure_called <- FALSE
193
194 result <- hybrid_then(
195 promise_resolve(42),
196 on_success = \(x) {
197 success_called <<- TRUE
198 x * 2
199 },
200 on_failure = \(err) {
201 failure_called <<- TRUE
202 "error"
203 }
204 )
205
206 expect_true(is.promise(result))
207 result |> extract() |> expect_equal(84)
208 expect_true(success_called)
209 expect_false(failure_called)
210 })
211
212 test_that("hybrid_then() handles both callbacks with asynchronous failure", {
213 success_called <- FALSE
214 failure_called <- FALSE
215
216 result <- hybrid_then(
217 promise_resolve(stop("error")),
218 on_success = \(x) {
219 success_called <<- TRUE
220 x * 2
221 },
222 on_failure = \(err) {
223 failure_called <<- TRUE
224 "recovered"
225 }
226 )
227
228 expect_true(is.promise(result))
229 result |> extract() |> expect_equal("recovered")
230 expect_false(success_called)
231 expect_true(failure_called)
232 })
233
234 test_that("hybrid_then() can chain synchronous and asynchronous operations", {
235 # Start with sync, end with async
236 result1 <- hybrid_then(
237 42,
238 on_success = \(x) promise_resolve(x + 8)
239 )
240 expect_true(is.promise(result1))
241 result1 |> extract() |> expect_equal(50)
242
243 # Start with async, callback returns sync
244 result2 <- hybrid_then(
245 promise_resolve(42),
246 on_success = \(x) x + 8
247 )
248 expect_true(is.promise(result2))
249 result2 |> extract() |> expect_equal(50)
250 })
251
252 test_that("hybrid_then() validates tee parameter", {
253 expect_snapshot(
254 hybrid_then(42, tee = "invalid"),
255 error = TRUE
256 )
257
258 expect_snapshot(
259 hybrid_then(42, tee = 1),
260 error = TRUE
261 )
262 })
263
264 test_that("hybrid_then() validates `on_success` and `on_failure` parameters", {
265 expect_snapshot(
266 hybrid_then(42, on_success = "invalid"),
267 error = TRUE
268 )
269
270 expect_snapshot(
271 hybrid_then(42, on_failure = "invalid"),
272 error = TRUE
273 )
274 })
275
276 test_that("hybrid_then() example from documentation works", {
277 add_to <- function(x, k) {
278 hybrid_then(x, on_success = function(value) {
279 value + k
280 })
281 }
282
283 # Synchronous
284 result_sync <- 42 |> add_to(100)
285 expect_equal(result_sync, 142)
286
287 # Asynchronous
288 async_called <- FALSE
289 result_async <- promise_resolve(42) |> add_to(8)
290 expect_true(is.promise(result_async))
291 result_async |>
292 then(\(x) {
293 async_called <<- TRUE
294 expect_equal(x, 50)
295 }) |>
296 wait_for_it()
297 expect_true(async_called)
298 })
299
300 test_that("hybrid_then() on_success callback can throw errors synchronously", {
301 expect_error(
302 hybrid_then(
303 42,
304 on_success = \(x) stop("callback error")
305 ),
306 "callback error"
307 )
308 })
309
310 test_that("hybrid_then() on_success callback can throw errors asynchronously", {
311 expect_assertions(n = 2, {
312 result <- hybrid_then(
313 promise_resolve(42),
314 on_success = \(x) stop("callback error")
315 )
316 expect_true(is.promise(result))
317 result |>
318 catch(\(err) {
319 expect_match(err$message, "callback error")
320 }) |>
321 wait_for_it()
322 })
323 })
324
325 test_that("hybrid_then() on_failure callback can throw errors synchronously", {
326 expect_error(
327 hybrid_then(
328 stop("original error"),
329 on_failure = \(err) stop("callback error")
330 ),
331 "callback error"
332 )
333 })
334
335 test_that("hybrid_then() on_failure callback can throw errors asynchronously", {
336 expect_assertions(n = 2, {
337 result <- hybrid_then(
338 promise_resolve(stop("original error")),
339 on_failure = \(err) stop("callback error")
340 )
341 expect_true(is.promise(result))
342 result |>
343 catch(\(err) {
344 expect_match(err$message, "callback error")
345 }) |>
346 wait_for_it()
347 })
348 })
349
350 test_that("hybrid_then() with tee=TRUE and on_success that throws (sync)", {
351 counter <- 0
352 expect_error(
353 hybrid_then(
354 42,
355 on_success = \(x) {
356 counter <<- counter + 1
357 stop("callback error")
358 },
359 tee = TRUE
360 ),
361 "callback error"
362 )
363 expect_equal(counter, 1)
364 })
365
366 test_that("hybrid_then() with tee=TRUE and on_success that throws (async)", {
367 expect_assertions(n = 3, {
368 counter <- 0
369 result <- hybrid_then(
370 promise_resolve(42),
371 on_success = \(x) {
372 counter <<- counter + 1
373 stop("callback error")
374 },
375 tee = TRUE
376 )
377 expect_true(is.promise(result))
378 result |>
379 catch(\(err) {
380 expect_match(err$message, "callback error")
381 }) |>
382 wait_for_it()
383 expect_equal(counter, 1)
384 })
385 })
386
387 test_that("hybrid_then() can chain multiple hybrid_then calls synchronously", {
388 result <- 1 |>
389 hybrid_then(on_success = \(x) x + 1) |>
390 hybrid_then(on_success = \(x) x * 2) |>
391 hybrid_then(on_success = \(x) x - 1)
392 expect_equal(result, 3)
393 })
394
395 test_that("hybrid_then() can chain multiple hybrid_then calls asynchronously", {
396 expect_assertions(n = 2, {
397 result <- promise_resolve(1) |>
398 hybrid_then(on_success = \(x) x + 1) |>
399 hybrid_then(on_success = \(x) x * 2) |>
400 hybrid_then(on_success = \(x) x - 1)
401 expect_true(is.promise(result))
402 result |> extract() |> expect_equal(3)
403 })
404 })
405
406 test_that("hybrid_then() can chain with mix of sync and async results", {
407 expect_assertions(n = 2, {
408 result <- 1 |>
409 hybrid_then(on_success = \(x) promise_resolve(x + 1)) |>
410 hybrid_then(on_success = \(x) x * 2) |>
411 hybrid_then(on_success = \(x) promise_resolve(x - 1))
412 expect_true(is.promise(result))
413 result |> extract() |> expect_equal(3)
414 })
415 })
416
417 test_that("hybrid_then() handles invisible values synchronously", {
418 result <- hybrid_then(
419 invisible(42),
420 on_success = \(x) x + 8
421 )
422 expect_equal(result, 50)
423 })
424
425 test_that("hybrid_then() handles NULL return from on_success callback", {
426 result <- hybrid_then(
427 42,
428 on_success = \(x) NULL
429 )
430 expect_null(result)
431 })
432
433 test_that("hybrid_then() handles NULL return from on_failure callback", {
434 result <- hybrid_then(
435 stop("error"),
436 on_failure = \(err) NULL
437 )
438 expect_null(result)
439 })
440
441 test_that("hybrid_then() handles on_success returning a promise (sync to async)", {
442 expect_assertions(n = 2, {
443 result <- hybrid_then(
444 42,
445 on_success = \(x) promise_resolve(x * 2)
446 )
447 expect_true(is.promise(result))
448 result |> extract() |> expect_equal(84)
449 })
450 })
451
452 test_that("hybrid_then() handles on_failure returning a promise (sync to async)", {
453 expect_assertions(n = 2, {
454 result <- hybrid_then(
455 stop("error"),
456 on_failure = \(err) promise_resolve("recovered via promise")
457 )
458 expect_true(is.promise(result))
459 result |> extract() |> expect_equal("recovered via promise")
460 })
461 })
462
463 test_that("hybrid_then() with only on_failure and synchronous success", {
464 result <- hybrid_then(
465 42,
466 on_failure = \(err) "should not be called"
467 )
468 expect_equal(result, 42)
469 })
470
471 test_that("hybrid_then() with only on_failure and asynchronous success", {
472 expect_assertions(n = 2, {
473 result <- hybrid_then(
474 promise_resolve(42),
475 on_failure = \(err) "should not be called"
476 )
477 expect_true(is.promise(result))
478 result |> extract() |> expect_equal(42)
479 })
480 })
481
482 test_that("hybrid_then() preserves error class information", {
483 expect_assertions(n = 2, {
484 custom_error <- structure(
485 list(message = "custom error"),
486 class = c("custom_error", "error", "condition")
487 )
488
489 result <- hybrid_then(
490 stop(custom_error),
491 on_failure = \(err) {
492 expect_s3_class(err, "custom_error")
493 "recovered"
494 }
495 )
496 expect_equal(result, "recovered")
497 })
498 })
499
500 test_that("hybrid_then() works with complex expressions", {
501 result <- hybrid_then(
502 {
503 x <- 10
504 y <- 20
505 x + y
506 },
507 on_success = \(val) val * 2
508 )
509 expect_equal(result, 60)
510 })