Debian Codegraph @

tests/testthat/test-aplus-2-2.R

raw
1 library(testthat)
2
3 describe("2.2. The `then` Method", {
4 # A promise must provide a `then` method to access its current or eventual
5 # value or reason.
6 #
7 # A promise’s then method accepts two arguments:
8 # promise.then(onFulfilled, onRejected)
9
10 describe("2.2.1. Both onFulfilled and onRejected are optional arguments:", {
11 it("2.2.1.1. If onFulfilled is not a function, it must be ignored.", {
12 # NOTE: This behavior varies; we throw an error
13 # p <- promise(\(resolve, reject) resolve(10)) |> then(20)
14 # expect_identical(extract(p), 10)
15 })
16 it("2.2.1.1. If onRejected is not a function, it must be ignored.", {
17 # NOTE: This behavior varies; we throw an error
18 # p <- promise(\(resolve, reject) reject("foo")) |> then(onRejected = "bar")
19 # expect_error(extract(p), "foo")
20 })
21 })
22
23 describe("2.2.2. If onFulfilled is a function:", {
24 it("2.2.2.1. it must be called after promise is fulfilled, with promise’s value as its first argument.", {
25 x <- NULL
26 p <- ext_promise()
27
28 p$promise |>
29 then(function(value) {
30 x <<- value
31 }) |>
32 wait_for_it()
33 expect_identical(x, NULL)
34
35 p$resolve(10) |> wait_for_it()
36 expect_identical(x, 10)
37 })
38 it("2.2.2.2. it must not be called before promise is fulfilled.", {})
39 it("2.2.2.3. it must not be called more than once.", {})
40 })
41 describe("2.2.3. If onRejected is a function,", {
42 it("2.2.3.1. it must be called after promise is rejected, with promise’s reason as its first argument.", {
43 x <- NULL
44 p <- ext_promise()
45
46 p$promise |>
47 then(onRejected = function(reason) {
48 x <<- reason
49 }) |>
50 wait_for_it()
51 expect_identical(x, NULL)
52
53 p$reject(simpleError("boom"))
54 wait_for_it()
55 expect_identical(x, simpleError("boom"))
56 })
57 })
58 describe("2.2.4. onFulfilled or onRejected must not be called until the execution context stack contains only platform code. [3.1].", {
59 it(" ", {
60 x <- NULL
61 p <- promise_resolve(TRUE) |>
62 then(function(value) {
63 x <<- value
64 })
65 expect_identical(x, NULL)
66 p |> wait_for_it()
67 expect_identical(x, TRUE)
68 })
69 })
70 describe("2.2.5. onFulfilled and onRejected must be called as functions (i.e. with no this value). [3.2]", {
71 # Not relevant for R.
72 })
73 describe("2.2.6. `then` may be called multiple times on the same promise.", {
74 it("2.2.6.1. If/when promise is fulfilled, all respective onFulfilled callbacks must execute in the order of their originating calls to then.", {
75 p <- ext_promise()
76 callbacks_called <- 0L
77 results <- new.env(parent = emptyenv())
78
79 all_promises <- lapply(1:10, function(i) {
80 ret <- p$promise |>
81 then(function(value) {
82 callbacks_called <<- callbacks_called + 1L
83 expect_identical(callbacks_called, i)
84 value
85 })
86 results[[as.character(i)]] <- ret
87 ret
88 })
89
90 # str(all_promises)
91 p$resolve(cars)
92 promise_all(.list = all_promises) |> wait_for_it()
93
94 lapply(as.list(results), function(x) {
95 expect_identical(extract(x), cars)
96 })
97 })
98 })
99
100 describe("2.2.6.2. If/when promise is rejected, all respective onRejected callbacks must execute in the order of their originating calls to then.", {
101 p <- ext_promise()
102 callbacks_called <- 0L
103 results <- new.env(parent = emptyenv())
104
105 all_promises <- lapply(1:10, function(i) {
106 results[[as.character(i)]] <- p$promise |>
107 catch(function(err) {
108 callbacks_called <<- callbacks_called + 1L
109 expect_identical(callbacks_called, i)
110 err
111 })
112 })
113
114 p$reject(simpleError("an error"))
115 promise_all(.list = all_promises) |> wait_for_it()
116
117 lapply(as.list(results), function(x) {
118 expect_identical(extract(x), simpleError("an error"))
119 })
120 })
121
122 describe("2.2.7. `then` must return a promise [3.3].", {
123 it(" ", {
124 promise(\(resolve, reject) {}) |>
125 then() |>
126 is.promise() |>
127 expect_true()
128 })
129
130 it("2.2.7.1. If either onFulfilled or onRejected returns a value x, run the Promise Resolution Procedure [[Resolve]](promise2, x).", {
131 p1 <- promise_resolve(TRUE) |> then(\() "foo")
132 expect_identical(extract(p1), "foo")
133
134 p2 <- promise_reject("boom") |> catch(\() "bar")
135 expect_identical(extract(p2), "bar")
136 })
137
138 it("2.2.7.2. If either onFulfilled or onRejected throws an exception e, promise2 must be rejected with e as the reason.", {
139 p1 <- promise_resolve(TRUE) |> then(\(e) stop("foo"))
140 expect_error(extract(p1), "^foo$")
141
142 p2 <- promise_reject("boom") |> catch(\(e) stop("bar"))
143 expect_error(extract(p2), "^bar$")
144 })
145
146 it("2.2.7.3. If onFulfilled is not a function and promise1 is fulfilled, promise2 must be fulfilled with the same value as promise1.", {
147 p <- promise_resolve("baz") |> then()
148 expect_identical(extract(p), "baz")
149 })
150
151 it("2.2.7.4. If onRejected is not a function and promise1 is rejected, promise2 must be rejected with the same reason as promise1.", {
152 p <- promise_reject("qux") |> then()
153 expect_error(extract(p), "^qux$")
154 })
155 })
156 })