1
2context("event_emitter")
3
4test_that("can create event emitter", {
5  do <- function() {
6    x <- event_emitter$new(async = FALSE)
7  }
8  expect_silent(run_event_loop(do()))
9})
10
11test_that("can add a listener", {
12  do <- function() {
13    x <- event_emitter$new(async = FALSE)
14    x$listen_on("foo", function() {  })
15  }
16  expect_silent(run_event_loop(do()))
17})
18
19test_that("can add a one-shot listener", {
20  do <- function() {
21    x <- event_emitter$new()
22    x$listen_once("foo", function() {  })
23  }
24  expect_silent(run_event_loop(do()))
25})
26
27test_that("no listeners", {
28  do <- function() {
29    x <- event_emitter$new(async = FALSE)
30    x$emit("foo")
31  }
32  expect_silent(run_event_loop(do()))
33})
34
35test_that("listener is called on event", {
36  called <- FALSE
37  do <- function() {
38    x <- event_emitter$new(async = FALSE)
39    x$listen_on("foo", function() { called <<- TRUE })
40    x$emit("foo")
41  }
42  expect_silent(run_event_loop(do()))
43  expect_true(called)
44})
45
46test_that("listener called multiple times", {
47  called <- 0L
48  do <- function() {
49    x <- event_emitter$new(async = FALSE)
50    x$listen_on("foo", function() { called <<- called + 1L })
51    x$emit("foo")
52    x$emit("foo")
53    x$emit("foo")
54  }
55  expect_silent(run_event_loop(do()))
56  expect_equal(called, 3L)
57})
58
59test_that("listener gets arguments", {
60  arg1 <- arg2 <- NULL
61  do <- function() {
62    x <- event_emitter$new(async = FALSE)
63    x$listen_on("foo", function(a1, a2) {
64      arg1 <<- a1
65      arg2 <<- a2
66    })
67    x$emit("foo", 1, 2)
68  }
69  expect_silent(run_event_loop(do()))
70  expect_equal(arg1, 1)
71  expect_equal(arg2, 2)
72})
73
74test_that("named arguments to listeners", {
75  arg1 <- arg2 <- arg3 <- NULL
76  do <- function() {
77    x <- event_emitter$new(async = FALSE)
78    x$listen_on("foo", function(a1, a2, a3) {
79      arg1 <<- a1
80      arg2 <<- a2
81      arg3 <<- a3
82    })
83    x$emit("foo", a3 = 3, a2 = 2, 1)
84  }
85  expect_silent(run_event_loop(do()))
86  expect_equal(arg1, 1)
87  expect_equal(arg2, 2)
88  expect_equal(arg3, 3)
89})
90
91test_that("all listeners are called", {
92  arg1 <- arg2 <- arg3 <- NULL
93  arg11 <- arg21 <- arg31 <- NULL
94  do <- function() {
95    x <- event_emitter$new(async = FALSE)
96    x$listen_on("foo", function(a1, a2, a3) {
97      arg1 <<- a1
98      arg2 <<- a2
99      arg3 <<- a3
100    })
101    x$listen_on("foo", function(a1, a2, a3) {
102      arg11 <<- a1
103      arg12 <<- a2
104      arg13 <<- a3
105    })
106    x$emit("foo", a3 = 3, a2 = 2, 1)
107  }
108  expect_silent(run_event_loop(do()))
109  expect_equal(arg1, 1)
110  expect_equal(arg2, 2)
111  expect_equal(arg3, 3)
112  expect_equal(arg11, 1)
113  expect_equal(arg12, 2)
114  expect_equal(arg13, 3)
115})
116
117test_that("one shot listener is only called once", {
118  called <- called1 <- 0L
119  do <- function() {
120    x <- event_emitter$new(async = FALSE)
121    x$listen_on("foo", function() { called <<- called + 1L })
122    x$listen_once("foo", function() { called1 <<- called1 + 1L })
123    x$emit("foo")
124    x$emit("foo")
125    x$emit("foo")
126  }
127  expect_silent(run_event_loop(do()))
128  expect_equal(called, 3L)
129  expect_equal(called1, 1L)
130})
131
132test_that("can remove listener", {
133  called <- called2 <- 0L
134  cb1 <- function() { called <<- called + 1L }
135  cb2 <- function(x = 1) { called2 <<- called2 + 1L }
136  do <- function() {
137    x <- event_emitter$new(async = FALSE)
138    x$listen_on("foo", cb1)
139    x$listen_on("foo", cb2)
140    x$emit("foo")
141    x$listen_off("foo", cb2)
142    x$emit("foo")
143    x$emit("foo")
144    x$listen_off("foo", cb1)
145    x$emit("foo")
146  }
147
148  expect_silent(run_event_loop(do()))
149  expect_equal(called, 3L)
150  expect_equal(called2, 1L)
151})
152
153test_that("only removes one listener instance", {
154  called <- 0L
155  cb <- function() { called <<- called + 1L }
156  do <- function() {
157    x <- event_emitter$new(async = FALSE)
158    x$listen_on("foo", cb)
159    x$listen_on("foo", cb)
160    x$emit("foo")                       # + 2
161    x$listen_off("foo", cb)
162    x$emit("foo")                       # + 1
163    x$emit("foo")                       # + 1
164    x$listen_off("foo", cb)
165    x$emit("foo")                       # + 0
166  }
167
168  expect_silent(run_event_loop(do()))
169  expect_equal(called, 4L)
170})
171
172test_that("multiple events", {
173  foo <- bar <- FALSE
174  do <- function() {
175    x <- event_emitter$new(async = FALSE)
176    x$listen_on("foo", function() { foo <<- TRUE })
177    x$listen_on("bar", function() { bar <<- TRUE })
178    x$emit("foo")
179    x$emit("bar")
180  }
181  expect_silent(run_event_loop(do()))
182  expect_true(foo)
183  expect_true(bar)
184})
185
186test_that("list event names", {
187  n1 <- n2 <- n3 <- n4 <- n5 <- 0L
188  do <- function() {
189    x <- event_emitter$new(async = FALSE)
190    n1 <<- x$get_event_names()
191    cb1 <- function() { foo <<- TRUE }
192    cb2 <- function() { bar <<- TRUE }
193    x$listen_on("foo", cb1)
194    n2 <<- x$get_event_names()
195    x$listen_on("bar", cb2)
196    n3 <<- x$get_event_names()
197    x$listen_off("foo", cb1)
198    n4 <<- x$get_event_names()
199    x$listen_off("bar", cb2)
200    n5 <<- x$get_event_names()
201  }
202  expect_silent(run_event_loop(do()))
203  expect_equal(n1, character())
204  expect_equal(n2, "foo")
205  expect_equal(n3, c("foo", "bar"))
206  expect_equal(n4, "bar")
207  expect_equal(n5, character())
208})
209
210test_that("get listener count for event", {
211  n1 <- n2 <- n3 <- n4 <- n5 <- 0L
212  do <- function() {
213    x <- event_emitter$new(async = FALSE)
214    cb <- function() { foo <<- TRUE }
215    n1 <<- x$get_listener_count("foo")
216    x$listen_on("foo", cb)
217    n2 <<- x$get_listener_count("foo")
218    x$listen_on("foo", cb)
219    n3 <<- x$get_listener_count("foo")
220    x$listen_off("foo", cb)
221    n4 <<- x$get_listener_count("foo")
222    x$listen_off("foo", cb)
223    n5 <<- x$get_listener_count("foo")
224  }
225  expect_silent(run_event_loop(do()))
226  expect_equal(n1, 0L)
227  expect_equal(n2, 1L)
228  expect_equal(n3, 2L)
229  expect_equal(n4, 1L)
230  expect_equal(n5, 0L)
231})
232
233test_that("remove all listeners", {
234  called <- FALSE
235  do <- function() {
236    x <- event_emitter$new(async = FALSE)
237    x$listen_on("foo", function() called <<- TRUE)
238    x$listen_on("foo", function() called <<- TRUE)
239    x$listen_on("foo", function() called <<- TRUE)
240    x$listen_on("foo", function() called <<- TRUE)
241    x$remove_all_listeners("foo")
242    x$emit("foo")
243    x$emit("foo")
244    x$emit("foo")
245  }
246  expect_silent(run_event_loop(do()))
247  expect_false(called)
248})
249
250test_that("error callback is called on error", {
251  err <- NULL
252  do <- function() {
253    x <- event_emitter$new(async = FALSE)
254    x$listen_on("foo", function() stop("foobar"))
255    x$listen_on("error", function(e) err <<- e)
256    x$emit("foo")
257  }
258
259  expect_silent(run_event_loop(do()))
260  expect_false(is.null(err))
261  expect_s3_class(err, "error")
262  expect_equal(err$event, "foo")
263  expect_equal(conditionMessage(err), "foobar")
264})
265
266test_that("fail stage if no error callback", {
267  do <- function() {
268    x <- event_emitter$new(async = FALSE)
269    x$listen_on("foo", function() stop("foobar"))
270    x$emit("foo")
271  }
272
273  expect_error(run_event_loop(do()), "foobar")
274})
275
276test_that("all error callbacks are called", {
277  err1 <- err2 <- NULL
278  do <- function() {
279    x <- event_emitter$new(async = FALSE)
280    x$listen_on("foo", function() stop("foobar"))
281    x$listen_on("error", function(e) err1 <<- e)
282    x$listen_on("error", function(e) err2 <<- e)
283    x$emit("foo")
284  }
285
286  expect_silent(run_event_loop(do()))
287  expect_false(is.null(err1))
288  expect_false(is.null(err2))
289  expect_s3_class(err1, "error")
290  expect_equal(err1$event, "foo")
291  expect_equal(conditionMessage(err1), "foobar")
292  expect_s3_class(err2, "error")
293  expect_equal(err2$event, "foo")
294  expect_equal(conditionMessage(err2), "foobar")
295})
296
297test_that("error within error callback", {
298  err <- NULL
299  do <- function() {
300    x <- event_emitter$new(async = FALSE)
301    x$listen_on("foo", function() stop("foobar"))
302    x$listen_on("error", function(e) { err <<- e; stop("baz")  })
303    x$emit("foo")
304  }
305
306  expect_error(run_event_loop(do()), "baz")
307  expect_false(is.null(err))
308  expect_s3_class(err, "error")
309  expect_equal(conditionMessage(err), "foobar")
310})
311