Debian Codegraph @

tests/testthat/test-methods.R

raw
1 library(testthat)
2
3 describe("then()", {
4 it("honors .visible argument", {
5 result <- NULL
6 p <- promise(\(resolve, reject) resolve(invisible(1))) |>
7 then(function(value, .visible) {
8 result <<- list(value = value, visible = .visible)
9 })
10 p |> wait_for_it()
11 expect_identical(result$value, 1)
12 expect_identical(result$visible, FALSE)
13
14 p <- promise(\(resolve, reject) resolve(1)) |>
15 then(function(value, .visible) {
16 result <<- list(value = value, visible = .visible)
17 })
18 p |> wait_for_it()
19 expect_identical(result$value, 1)
20 expect_identical(result$visible, TRUE)
21
22 # .visible is preserved even with an intermediate then() or catch()
23 p <- promise(\(resolve, reject) resolve(invisible(1))) |>
24 then() |>
25 catch(\(err) "what error?") |>
26 then(function(value, .visible) {
27 result <<- list(value = value, visible = .visible)
28 })
29 p |> wait_for_it()
30 expect_identical(result$value, 1)
31 expect_identical(result$visible, FALSE)
32 })
33 it("method ignores non-functions or NULL...", {
34 p1 <- promise(\(resolve, reject) resolve(1))
35 expect_warning(
36 {
37 p1 <- p1$then(10)
38 },
39 "`onFulfilled` must be a function or `NULL`",
40 fixed = TRUE
41 )
42 expect_warning(
43 {
44 p1 <- p1$then(NULL)
45 },
46 NA
47 )
48 expect_identical(extract(p1), 1)
49 })
50 it("...but function only ignores NULL, not non-functions", {
51 expect_error(promise(\(resolve, reject) resolve(1)) |> then(10))
52 expect_error(promise(\(resolve, reject) resolve(1)) |> then(NULL), NA)
53 })
54
55 it("honors visibility with no .visible argument", {
56 result <- NULL
57 p <- promise_resolve(invisible(1))$then(function(value) {
58 result <<- withVisible(value)
59 })
60 p |> wait_for_it()
61 expect_identical(result$value, 1)
62 expect_identical(result$visible, FALSE)
63
64 result <- NULL
65 p <- promise_resolve(2)$then(function(value) {
66 result <<- withVisible(value)
67 })
68 p |> wait_for_it()
69 expect_identical(result$value, 2)
70 expect_identical(result$visible, TRUE)
71 })
72 })
73
74 describe("catch()", {
75 it("catches", {
76 p <- ext_promise()
77 p2 <- p$promise |> catch(\(err) TRUE)
78 p$reject("boom")
79 expect_identical(extract(p2), TRUE)
80 })
81 it("can throw", {
82 p <- promise(\(resolve, reject) stop("foo")) |> catch(\(err) stop("bar"))
83 expect_error(extract(p), "^bar$")
84 })
85 it("method ignores non-functions or NULL...", {
86 p1 <- promise(\(resolve, reject) resolve(1))
87 expect_warning(
88 {
89 p1 <- p1$catch(10)
90 },
91 "`onRejected` must be a function or `NULL`",
92 fixed = TRUE
93 )
94 expect_warning(
95 {
96 p1 <- p1$catch(NULL)
97 },
98 NA
99 )
100 expect_identical(extract(p1), 1)
101 })
102 it("...but function only ignores NULL, not non-functions", {
103 expect_error(promise(\(resolve, reject) resolve(1)) |> catch(10))
104 expect_error(promise(\(resolve, reject) resolve(1)) |> catch(NULL), NA)
105 })
106 })
107
108 describe("finally()", {
109 it("calls back when a promise is resolved", {
110 called <- FALSE
111 p <- promise(\(resolve, reject) resolve(10)) |>
112 finally(\() {
113 called <<- TRUE
114 })
115 p |> wait_for_it()
116 expect_identical(called, TRUE)
117 expect_identical(extract(p), 10)
118 })
119 it("calls back when a promise is rejected", {
120 called <- FALSE
121 p <- promise(\(resolve, reject) reject("foobar"))
122 p |>
123 finally(\() {
124 called <<- TRUE
125 }) |>
126 squelch_unhandled_promise_error() |>
127 wait_for_it()
128 expect_identical(called, TRUE)
129 expect_error(extract(p), "^foobar$")
130 })
131 it("does not affect the return value of the promise", {
132 p1 <- promise(\(resolve, reject) resolve(1)) |>
133 finally(\() {
134 20
135 })
136 expect_identical(extract(p1), 1)
137
138 p2 <- promise(\(resolve, reject) reject("err")) |>
139 finally(\() {
140 20
141 })
142 expect_error(extract(p2), "^err$")
143 })
144 it("errors replace the result of the promise", {
145 p1 <- promise(\(resolve, reject) resolve(1)) |>
146 finally(\() {
147 stop("boom")
148 })
149 expect_error(extract(p1), "^boom$")
150
151 p2 <- promise(\(resolve, reject) reject("foo")) |>
152 finally(\() {
153 stop("bar")
154 })
155 expect_error(extract(p2), "^bar$")
156 })
157 it("method ignores non-functions or NULL...", {
158 p1 <- promise(\(resolve, reject) resolve(1))$finally(10)$finally(NULL)
159 expect_identical(extract(p1), 1)
160 })
161 it("...but function only ignores NULL, not non-functions", {
162 expect_error(promise(\(resolve, reject) resolve(1)) |> finally(10))
163 expect_error(promise(\(resolve, reject) resolve(1)) |> finally(NULL), NA)
164 })
165 })
166
167 describe("future", {
168 it("is treated as promise when used as resolution", {
169 p <- promise_resolve(future::future(1))
170 expect_identical(extract(p), 1)
171
172 p2 <- promise_resolve(future::future(stop("boom")))
173 expect_error(extract(p2))
174 })
175
176 it("is treated as promise when used as resolution", {
177 p <- promise_reject(future::future(1))
178 expect_identical(extract(p), 1)
179
180 p2 <- promise_reject(future::future(stop("boom")))
181 expect_error(extract(p2))
182 })
183 })