Software /
code /
prosody
Annotate
spec/util_promise_spec.lua @ 11942:b4f77a7bf8ab
mod_http_openmetrics: Serve HTTP from global context
Makes GET /metrics work regardless of the value of the Host header.
This was not possible before 220468f7a103 which added the ability to
have global HTTP modules, which was partly why the per-host part was
kept while the module lived among the community modules.
Another reason was to provide some semblance of security by obscurity by
only loading the module on e.g. a 'localhost' VirtualHost or Component.
The new IP limits obsoletes this.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 25 Nov 2021 16:57:01 +0100 |
parent | 11727:f3aee8a825cc |
child | 11947:073e53b72792 |
rev | line source |
---|---|
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local promise = require "util.promise"; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 describe("util.promise", function () |
9457
d01cff2f7a26
util.promise tests: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
9456
diff
changeset
|
4 --luacheck: ignore 212/resolve 212/reject |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 describe("new()", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 it("returns a promise object", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 assert(promise.new()); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 it("notifies immediately for fulfilled promises", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local p = promise.new(function (resolve) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 resolve("foo"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 assert.equal("foo", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 p:next(cb); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 assert.spy(cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 end); |
11727
f3aee8a825cc
Fix various spelling errors (thanks codespell)
Kim Alvefur <zash@zash.se>
parents:
11486
diff
changeset
|
20 it("notifies on fulfillment of pending promises", function () |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local r; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 local p = promise.new(function (resolve) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 r = resolve; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 local cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 assert.equal("foo", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 p:next(cb); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 r("foo"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 assert.spy(cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 it("allows chaining :next() calls", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local r; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 local p = promise.new(function (resolve) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 r = resolve; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 local cb1 = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 assert.equal("foo", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 return "bar"; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 local cb2 = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 assert.equal("bar", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 p:next(cb1):next(cb2); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 assert.spy(cb1).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 assert.spy(cb2).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 r("foo"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 assert.spy(cb1).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 assert.spy(cb2).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 assert.equal("bar", result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 it("supports multiple :next() calls on the same promise", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local r; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 local p = promise.new(function (resolve) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 r = resolve; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 local cb1 = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 assert.equal("foo", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 local cb2 = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 assert.equal("foo", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 p:next(cb1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 p:next(cb2); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 assert.spy(cb1).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 assert.spy(cb2).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 r("foo"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 assert.spy(cb1).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 assert.spy(cb2).was_called(1); |
9457
d01cff2f7a26
util.promise tests: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
9456
diff
changeset
|
76 assert.equal("foo", result); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 it("automatically rejects on error", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 local r; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 local p = promise.new(function (resolve) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 r = resolve; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 error("oh no"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 local cb = spy.new(function () end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 local err_cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 assert.equal("oh no", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 p:next(cb, err_cb); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 assert.spy(err_cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 r("foo"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 assert.spy(err_cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 it("supports reject()", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 local r, result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 local p = promise.new(function (resolve, reject) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 r = reject; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 local cb = spy.new(function () end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 local err_cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 assert.equal("oh doh", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 p:next(cb, err_cb); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 assert.spy(err_cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 r("oh doh"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 assert.spy(err_cb).was_called(1); |
9457
d01cff2f7a26
util.promise tests: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
9456
diff
changeset
|
111 assert.equal("oh doh", result); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 it("supports chaining of rejected promises", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 local r, result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 local p = promise.new(function (resolve, reject) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 r = reject; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 local cb = spy.new(function () end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 local err_cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 assert.equal("oh doh", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 return "ok" |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 local cb2 = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 end); |
9457
d01cff2f7a26
util.promise tests: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
9456
diff
changeset
|
127 local err_cb2 = spy.new(function () end); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 p:next(cb, err_cb):next(cb2, err_cb2) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 assert.spy(err_cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 assert.spy(cb2).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 assert.spy(err_cb2).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 r("oh doh"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 assert.spy(err_cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 assert.spy(cb2).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 assert.spy(err_cb2).was_called(0); |
9457
d01cff2f7a26
util.promise tests: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
9456
diff
changeset
|
138 assert.equal("ok", result); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 |
9549
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
141 it("propagates errors down the chain, even when some handlers are not provided", function () |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
142 local r, result; |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
143 local test_error = {}; |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
144 local p = promise.new(function (resolve, reject) |
9550
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
145 r = reject; |
9549
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
146 end); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
147 local cb = spy.new(function () end); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
148 local err_cb = spy.new(function (e) result = e end); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
149 local p2 = p:next(function () error(test_error) end); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
150 local p3 = p2:next(cb) |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
151 p3:catch(err_cb); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
152 assert.spy(cb).was_called(0); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
153 assert.spy(err_cb).was_called(0); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
154 r("oh doh"); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
155 assert.spy(cb).was_called(0); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
156 assert.spy(err_cb).was_called(1); |
9550
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
157 assert.spy(err_cb).was_called_with("oh doh"); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
158 assert.equal("oh doh", result); |
9549
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
159 end); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
160 |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
161 it("propagates values down the chain, even when some handlers are not provided", function () |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
162 local r; |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
163 local p = promise.new(function (resolve, reject) |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
164 r = resolve; |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
165 end); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
166 local cb = spy.new(function () end); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
167 local err_cb = spy.new(function () end); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
168 local p2 = p:next(function (v) return v; end); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
169 local p3 = p2:catch(err_cb) |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
170 p3:next(cb); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
171 assert.spy(cb).was_called(0); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
172 assert.spy(err_cb).was_called(0); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
173 r(1337); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
174 assert.spy(cb).was_called(1); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
175 assert.spy(cb).was_called_with(1337); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
176 assert.spy(err_cb).was_called(0); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
177 end); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9517
diff
changeset
|
178 |
9550
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
179 it("fulfilled promises do not call error handlers and do propagate value", function () |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
180 local p = promise.resolve("foo"); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
181 local cb = spy.new(function () end); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
182 local p2 = p:catch(cb); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
183 assert.spy(cb).was_called(0); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
184 |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
185 local cb2 = spy.new(function () end); |
9551
7421379ba464
util.promise tests: Fix declared but unused variables
Matthew Wild <mwild1@gmail.com>
parents:
9550
diff
changeset
|
186 p2:catch(cb2); |
9550
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
187 assert.spy(cb2).was_called(0); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
188 end); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
189 |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
190 it("rejected promises do not call fulfilled handlers and do propagate reason", function () |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
191 local p = promise.reject("foo"); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
192 local cb = spy.new(function () end); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
193 local p2 = p:next(cb); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
194 assert.spy(cb).was_called(0); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
195 |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
196 local cb2 = spy.new(function () end); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
197 local cb2_err = spy.new(function () end); |
9551
7421379ba464
util.promise tests: Fix declared but unused variables
Matthew Wild <mwild1@gmail.com>
parents:
9550
diff
changeset
|
198 p2:next(cb2, cb2_err); |
9550
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
199 assert.spy(cb2).was_called(0); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
200 assert.spy(cb2_err).was_called(1); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
201 assert.spy(cb2_err).was_called_with("foo"); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
202 end); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
203 |
9557
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
204 describe("allows callbacks to return", function () |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
205 it("pending promises", function () |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
206 local r; |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
207 local p = promise.resolve() |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
208 local cb = spy.new(function () |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
209 return promise.new(function (resolve) |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
210 r = resolve; |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
211 end); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
212 end); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
213 local cb2 = spy.new(function () end); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
214 p:next(cb):next(cb2); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
215 assert.spy(cb).was_called(1); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
216 assert.spy(cb2).was_called(0); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
217 r("hello"); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
218 assert.spy(cb).was_called(1); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
219 assert.spy(cb2).was_called(1); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
220 assert.spy(cb2).was_called_with("hello"); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
221 end); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
222 |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
223 it("resolved promises", function () |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
224 local p = promise.resolve() |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
225 local cb = spy.new(function () |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
226 return promise.resolve("hello"); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
227 end); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
228 local cb2 = spy.new(function () end); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
229 p:next(cb):next(cb2); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
230 assert.spy(cb).was_called(1); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
231 assert.spy(cb2).was_called(1); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
232 assert.spy(cb2).was_called_with("hello"); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
233 end); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
234 |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
235 it("rejected promises", function () |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
236 local p = promise.resolve() |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
237 local cb = spy.new(function () |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
238 return promise.reject("hello"); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
239 end); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
240 local cb2 = spy.new(function () |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
241 return promise.reject("goodbye"); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
242 end); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
243 local cb3 = spy.new(function () end); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
244 p:next(cb):catch(cb2):catch(cb3); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
245 assert.spy(cb).was_called(1); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
246 assert.spy(cb2).was_called(1); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
247 assert.spy(cb2).was_called_with("hello"); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
248 assert.spy(cb3).was_called(1); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
249 assert.spy(cb3).was_called_with("goodbye"); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
250 end); |
10297
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
251 |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
252 it("ordinary values", function () |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
253 local p = promise.resolve() |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
254 local cb = spy.new(function () |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
255 return "hello" |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
256 end); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
257 local cb2 = spy.new(function () end); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
258 p:next(cb):next(cb2); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
259 assert.spy(cb).was_called(1); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
260 assert.spy(cb2).was_called(1); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
261 assert.spy(cb2).was_called_with("hello"); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
262 end); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
263 |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
264 it("nil", function () |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
265 local p = promise.resolve() |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
266 local cb = spy.new(function () |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
267 return |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
268 end); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
269 local cb2 = spy.new(function () end); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
270 p:next(cb):next(cb2); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
271 assert.spy(cb).was_called(1); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
272 assert.spy(cb2).was_called(1); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
273 assert.spy(cb2).was_called_with(nil); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
274 end); |
9557
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
275 end); |
d7fdd418adf8
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Matthew Wild <mwild1@gmail.com>
parents:
9551
diff
changeset
|
276 |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
277 describe("race()", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 it("works with fulfilled promises", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
279 local p1, p2 = promise.resolve("yep"), promise.resolve("nope"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
280 local p = promise.race({ p1, p2 }); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
282 p:next(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
283 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
284 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
285 assert.equal("yep", result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
286 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
287 it("works with pending promises", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
288 local r1, r2; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
289 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
290 local p = promise.race({ p1, p2 }); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
291 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
292 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
293 local cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
294 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
295 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
296 p:next(cb); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
297 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
298 r2("yep"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
299 r1("nope"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
300 assert.spy(cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
301 assert.equal("yep", result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
302 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
303 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
304 describe("all()", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
305 it("works with fulfilled promises", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
306 local p1, p2 = promise.resolve("yep"), promise.resolve("nope"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
307 local p = promise.all({ p1, p2 }); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
308 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
309 p:next(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
310 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
311 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
312 assert.same({ "yep", "nope" }, result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
313 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
314 it("works with pending promises", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
315 local r1, r2; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
316 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
317 local p = promise.all({ p1, p2 }); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
318 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
319 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
320 local cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
321 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
322 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
323 p:next(cb); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
324 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
325 r2("yep"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
326 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
327 r1("nope"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
328 assert.spy(cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
329 assert.same({ "nope", "yep" }, result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
330 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
331 it("rejects if any promise rejects", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
332 local r1, r2; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
333 local p1 = promise.new(function (resolve, reject) r1 = reject end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
334 local p2 = promise.new(function (resolve, reject) r2 = reject end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
335 local p = promise.all({ p1, p2 }); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
336 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
337 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
338 local cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
339 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
340 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
341 local cb_err = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
342 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
343 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
344 p:next(cb, cb_err); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
345 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
346 assert.spy(cb_err).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
347 r2("fail"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
348 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
349 assert.spy(cb_err).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
350 r1("nope"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
351 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
352 assert.spy(cb_err).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
353 assert.equal("fail", result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
354 end); |
11483
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
355 it("works with non-numeric keys", function () |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
356 local r1, r2; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
357 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
358 local p = promise.all({ [true] = p1, [false] = p2 }); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
359 |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
360 local result; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
361 local cb = spy.new(function (v) |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
362 result = v; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
363 end); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
364 p:next(cb); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
365 assert.spy(cb).was_called(0); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
366 r2("yep"); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
367 assert.spy(cb).was_called(0); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
368 r1("nope"); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
369 assert.spy(cb).was_called(1); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
370 assert.same({ [true] = "nope", [false] = "yep" }, result); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
371 end); |
11485
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
372 it("passes through non-promise values", function () |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
373 local r1; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
374 local p1 = promise.new(function (resolve) r1 = resolve end); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
375 local p = promise.all({ [true] = p1, [false] = "yep" }); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
376 |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
377 local result; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
378 local cb = spy.new(function (v) |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
379 result = v; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
380 end); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
381 p:next(cb); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
382 assert.spy(cb).was_called(0); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
383 r1("nope"); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
384 assert.spy(cb).was_called(1); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
385 assert.same({ [true] = "nope", [false] = "yep" }, result); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
386 end); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
387 end); |
10922
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
388 describe("all_settled()", function () |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
389 it("works with fulfilled promises", function () |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
390 local p1, p2 = promise.resolve("yep"), promise.resolve("nope"); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
391 local p = promise.all_settled({ p1, p2 }); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
392 local result; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
393 p:next(function (v) |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
394 result = v; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
395 end); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
396 assert.same({ |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
397 { status = "fulfilled", value = "yep" }; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
398 { status = "fulfilled", value = "nope" }; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
399 }, result); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
400 end); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
401 it("works with pending promises", function () |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
402 local r1, r2; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
403 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
404 local p = promise.all_settled({ p1, p2 }); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
405 |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
406 local result; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
407 local cb = spy.new(function (v) |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
408 result = v; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
409 end); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
410 p:next(cb); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
411 assert.spy(cb).was_called(0); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
412 r2("yep"); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
413 assert.spy(cb).was_called(0); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
414 r1("nope"); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
415 assert.spy(cb).was_called(1); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
416 assert.same({ |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
417 { status = "fulfilled", value = "nope" }; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
418 { status = "fulfilled", value = "yep" }; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
419 }, result); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
420 end); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
421 it("works when some promises reject", function () |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
422 local r1, r2; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
423 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (_, reject) r2 = reject end); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
424 local p = promise.all_settled({ p1, p2 }); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
425 |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
426 local result; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
427 local cb = spy.new(function (v) |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
428 result = v; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
429 end); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
430 p:next(cb); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
431 assert.spy(cb).was_called(0); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
432 r2("this fails"); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
433 assert.spy(cb).was_called(0); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
434 r1("this succeeds"); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
435 assert.spy(cb).was_called(1); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
436 assert.same({ |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
437 { status = "fulfilled", value = "this succeeds" }; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
438 { status = "rejected", reason = "this fails" }; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
439 }, result); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
440 end); |
11483
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
441 it("works with non-numeric keys", function () |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
442 local r1, r2; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
443 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
444 local p = promise.all_settled({ foo = p1, bar = p2 }); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
445 |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
446 local result; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
447 local cb = spy.new(function (v) |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
448 result = v; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
449 end); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
450 p:next(cb); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
451 assert.spy(cb).was_called(0); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
452 r2("yep"); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
453 assert.spy(cb).was_called(0); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
454 r1("nope"); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
455 assert.spy(cb).was_called(1); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
456 assert.same({ |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
457 foo = { status = "fulfilled", value = "nope" }; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
458 bar = { status = "fulfilled", value = "yep" }; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
459 }, result); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
460 end); |
11485
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
461 it("passes through non-promise values", function () |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
462 local r1; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
463 local p1 = promise.new(function (resolve) r1 = resolve end); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
464 local p = promise.all_settled({ foo = p1, bar = "yep" }); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
465 |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
466 local result; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
467 local cb = spy.new(function (v) |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
468 result = v; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
469 end); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
470 p:next(cb); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
471 assert.spy(cb).was_called(0); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
472 r1("nope"); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
473 assert.spy(cb).was_called(1); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
474 assert.same({ |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
475 foo = { status = "fulfilled", value = "nope" }; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
476 bar = "yep"; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
477 }, result); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
478 end); |
10922
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
479 end); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
480 describe("catch()", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
481 it("works", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
482 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
483 local p = promise.new(function (resolve) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
484 error({ foo = true }); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
485 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
486 local cb1 = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
487 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
488 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
489 assert.spy(cb1).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
490 p:catch(cb1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
491 assert.spy(cb1).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
492 assert.same({ foo = true }, result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
493 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
494 end); |
11484
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
495 describe("join()", function () |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
496 it("works", function () |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
497 local r1, r2; |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
498 local res1, res2; |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
499 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
500 |
11486
78d843faaffc
util.promise: Switch order of parameters to join()
Matthew Wild <mwild1@gmail.com>
parents:
11485
diff
changeset
|
501 local p = promise.join(function (_res1, _res2) |
11484
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
502 res1, res2 = _res1, _res2; |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
503 return promise.resolve("works"); |
11486
78d843faaffc
util.promise: Switch order of parameters to join()
Matthew Wild <mwild1@gmail.com>
parents:
11485
diff
changeset
|
504 end, p1, p2); |
11484
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
505 |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
506 local result; |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
507 local cb = spy.new(function (v) |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
508 result = v; |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
509 end); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
510 p:next(cb); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
511 assert.spy(cb).was_called(0); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
512 r2("yep"); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
513 assert.spy(cb).was_called(0); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
514 r1("nope"); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
515 assert.spy(cb).was_called(1); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
516 assert.same("works", result); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
517 assert.equals("nope", res1); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
518 assert.equals("yep", res2); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
519 end); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
520 end); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
521 it("promises may be resolved by other promises", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
522 local r1, r2; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
523 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
524 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
525 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
526 local cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
527 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
528 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
529 p1:next(cb); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
530 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
531 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
532 r1(p2); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
533 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
534 r2("yep"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
535 assert.spy(cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
536 assert.equal("yep", result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
537 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
538 describe("reject()", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
539 it("returns a rejected promise", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
540 local p = promise.reject("foo"); |
9457
d01cff2f7a26
util.promise tests: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
9456
diff
changeset
|
541 local cb = spy.new(function () end); |
9510
8ef46d09386a
util.promise: Fix promise.reject() to return a rejected promise, and fix buggy test for it
Matthew Wild <mwild1@gmail.com>
parents:
9457
diff
changeset
|
542 p:catch(cb); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
543 assert.spy(cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
544 assert.spy(cb).was_called_with("foo"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
545 end); |
9511
cb88d729e98d
util.promise: Add additional test for promise.reject()
Matthew Wild <mwild1@gmail.com>
parents:
9510
diff
changeset
|
546 it("returns a rejected promise and does not call on_fulfilled", function () |
cb88d729e98d
util.promise: Add additional test for promise.reject()
Matthew Wild <mwild1@gmail.com>
parents:
9510
diff
changeset
|
547 local p = promise.reject("foo"); |
cb88d729e98d
util.promise: Add additional test for promise.reject()
Matthew Wild <mwild1@gmail.com>
parents:
9510
diff
changeset
|
548 local cb = spy.new(function () end); |
cb88d729e98d
util.promise: Add additional test for promise.reject()
Matthew Wild <mwild1@gmail.com>
parents:
9510
diff
changeset
|
549 p:next(cb); |
cb88d729e98d
util.promise: Add additional test for promise.reject()
Matthew Wild <mwild1@gmail.com>
parents:
9510
diff
changeset
|
550 assert.spy(cb).was_called(0); |
cb88d729e98d
util.promise: Add additional test for promise.reject()
Matthew Wild <mwild1@gmail.com>
parents:
9510
diff
changeset
|
551 end); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
552 end); |
9514
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
553 describe("finally()", function () |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
554 local p, p2, resolve, reject, on_finally; |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
555 before_each(function () |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
556 p = promise.new(function (_resolve, _reject) |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
557 resolve, reject = _resolve, _reject; |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
558 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
559 on_finally = spy.new(function () end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
560 p2 = p:finally(on_finally); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
561 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
562 it("runs when a promise is resolved", function () |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
563 assert.spy(on_finally).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
564 resolve("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
565 assert.spy(on_finally).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
566 assert.spy(on_finally).was_not_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
567 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
568 it("runs when a promise is rejected", function () |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
569 assert.spy(on_finally).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
570 reject("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
571 assert.spy(on_finally).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
572 assert.spy(on_finally).was_not_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
573 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
574 it("returns a promise that fulfills with the original value", function () |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
575 local cb2 = spy.new(function () end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
576 p2:next(cb2); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
577 assert.spy(on_finally).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
578 assert.spy(cb2).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
579 resolve("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
580 assert.spy(on_finally).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
581 assert.spy(cb2).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
582 assert.spy(on_finally).was_not_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
583 assert.spy(cb2).was_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
584 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
585 it("returns a promise that rejects with the original error", function () |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
586 local on_finally_err = spy.new(function () end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
587 local on_finally_ok = spy.new(function () end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
588 p2:catch(on_finally_err); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
589 p2:next(on_finally_ok); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
590 assert.spy(on_finally).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
591 assert.spy(on_finally_err).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
592 reject("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
593 assert.spy(on_finally).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
594 -- Since the original promise was rejected, the finally promise should also be |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
595 assert.spy(on_finally_ok).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
596 assert.spy(on_finally_err).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
597 assert.spy(on_finally).was_not_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
598 assert.spy(on_finally_err).was_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
599 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
600 it("returns a promise that rejects with an uncaught error inside on_finally", function () |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
601 p = promise.new(function (_resolve, _reject) |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
602 resolve, reject = _resolve, _reject; |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
603 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
604 local test_error = {}; |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
605 on_finally = spy.new(function () error(test_error) end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
606 p2 = p:finally(on_finally); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
607 |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
608 local on_finally_err = spy.new(function () end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
609 p2:catch(on_finally_err); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
610 assert.spy(on_finally).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
611 assert.spy(on_finally_err).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
612 reject("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
613 assert.spy(on_finally).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
614 assert.spy(on_finally_err).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
615 assert.spy(on_finally).was_not_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
616 assert.spy(on_finally).was_not_called_with(test_error); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
617 assert.spy(on_finally_err).was_called_with(test_error); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
618 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
619 end); |
9517
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
620 describe("try()", function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
621 it("works with functions that return a promise", function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
622 local resolve; |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
623 local p = promise.try(function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
624 return promise.new(function (_resolve) |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
625 resolve = _resolve; |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
626 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
627 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
628 assert.is_function(resolve); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
629 local on_resolved = spy.new(function () end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
630 p:next(on_resolved); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
631 assert.spy(on_resolved).was_not_called(); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
632 resolve("foo"); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
633 assert.spy(on_resolved).was_called_with("foo"); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
634 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
635 |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
636 it("works with functions that return a value", function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
637 local p = promise.try(function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
638 return "foo"; |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
639 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
640 local on_resolved = spy.new(function () end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
641 p:next(on_resolved); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
642 assert.spy(on_resolved).was_called_with("foo"); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
643 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
644 |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
645 it("works with functions that return a promise that rejects", function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
646 local reject; |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
647 local p = promise.try(function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
648 return promise.new(function (_, _reject) |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
649 reject = _reject; |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
650 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
651 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
652 assert.is_function(reject); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
653 local on_rejected = spy.new(function () end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
654 p:catch(on_rejected); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
655 assert.spy(on_rejected).was_not_called(); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
656 reject("foo"); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
657 assert.spy(on_rejected).was_called_with("foo"); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
658 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
659 |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
660 it("works with functions that throw errors", function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
661 local test_error = {}; |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
662 local p = promise.try(function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
663 error(test_error); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
664 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
665 local on_rejected = spy.new(function () end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
666 p:catch(on_rejected); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
667 assert.spy(on_rejected).was_called(1); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
668 assert.spy(on_rejected).was_called_with(test_error); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
669 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
670 end); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
671 end); |