Debian Codegraph @

tests/testthat/test-legacy-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(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(~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
62 p <- promise(~ resolve(TRUE)) |>
63 then(function(value) {
64 x <<- value
65 })
66 expect_identical(x, NULL)
67 p |> wait_for_it()
68 expect_identical(x, TRUE)
69 })
70 })
71 describe("2.2.5. onFulfilled and onRejected must be called as functions (i.e. with no this value). [3.2]", {
72 # Not relevant for R.
73 })
74 describe("2.2.6. `then` may be called multiple times on the same promise.", {
75 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.", {
76 p <- ext_promise()
77 callbacks_called <- 0L
78 results <- new.env(parent = emptyenv())
79
80 all_promises <- lapply(1:10, function(i) {
81 results[[as.character(i)]] <- p$promise |>
82 then(function(value) {
83 callbacks_called <<- callbacks_called + 1L
84 expect_identical(callbacks_called, i)
85 value
86 })
87 })
88
89 p$resolve(cars)
90 promise_all(.list = all_promises) |> wait_for_it()
91
92 lapply(as.list(results), function(x) {
93 expect_identical(extract(x), cars)
94 })
95 })
96 })
97
98 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.", {
99 p <- ext_promise()
100 callbacks_called <- 0L
101 results <- new.env(parent = emptyenv())
102
103 all_promises <- lapply(1:10, function(i) {
104 results[[as.character(i)]] <- p$promise |>
105 catch(function(err) {
106 callbacks_called <<- callbacks_called + 1L
107 expect_identical(callbacks_called, i)
108 err
109 })
110 })
111
112 p$reject(simpleError("an error"))
113 promise_all(.list = all_promises) |> wait_for_it()
114
115 lapply(as.list(results), function(x) {
116 expect_identical(extract(x), simpleError("an error"))
117 })
118 })
119
120 describe("2.2.7. `then` must return a promise [3.3].", {
121 it(" ", {
122 promise(
123 ~ {}
124 ) |>
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(~ stop("foo"))
140 expect_error(extract(p1), "^foo$")
141
142 p2 <- promise(~ reject("boom")) |> catch(~ 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 })