Skip to content

Commit 58e3e87

Browse files
committed
Add tests and NEWS bullet
1 parent a42f99e commit 58e3e87

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# coro (development version)
22

3+
* Async functions and generators can now be R6 methods.
4+
5+
36
# coro 1.1.0
47

58
* Iterator functions are now allowed to have a `close` argument.

tests/testthat/test-async.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,24 @@ test_that("async functions do not cause CMD check notes (#40)", {
364364
))
365365
)
366366
})
367+
368+
test_that("async methods in R6 classes", {
369+
testthat::skip_if_not_installed("R6")
370+
371+
AsyncClass <- R6::R6Class(
372+
classname = "AsyncClass",
373+
public = list(
374+
async_resolved = async(function() "value"),
375+
async_pending = async(function() await("value"))
376+
)
377+
)
378+
class <- AsyncClass$new()
379+
380+
later::with_temp_loop({
381+
out <- class$async_resolved()
382+
expect_promise(out, "value", "fulfilled")
383+
384+
out <- class$async_pending()
385+
expect_promise(out, status = "pending")
386+
})
387+
})

tests/testthat/test-generator.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ test_that("short syntax and for loop support", {
3232
expect_identical(out, dbl(1, 9, 25))
3333
})
3434

35+
test_that("generator methods in R6 classes", {
36+
testthat::skip_if_not_installed("R6")
37+
38+
GenClass <- R6::R6Class(
39+
"GenClass",
40+
public = list(
41+
gen_method = generator(function() {
42+
yield(1)
43+
yield(2)
44+
})
45+
)
46+
)
47+
48+
obj <- GenClass$new()
49+
g <- obj$gen_method()
50+
51+
expect_equal(g(), 1)
52+
expect_equal(g(), 2)
53+
expect_exhausted(g())
54+
})
55+
3556
test_that("generator factories print nicely", {
3657
factory <- generator(function() yield(NULL))
3758
expect_snapshot(print(factory, reproducible = TRUE))

0 commit comments

Comments
 (0)