Software /
code /
prosody
Annotate
spec/util_promise_spec.lua @ 12749:eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
This lines don't appear to do anything useful, and all tests pass when they
are removed. Discovered via mutation testing.
I added extra tests to exercise this code, because I wasn't certain that there
were no side-effects caused by removal. Everything appears to be fine, thanks
to the "pending" check at the start of promise_settle().
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 07 Oct 2022 17:43:26 +0100 |
parent | 11951:6d9e3f541830 |
child | 12751:2639e0e1c378 |
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); |
12749
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
33 it("ignores resolve/reject of settled promises", function () |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
34 local res, rej; |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
35 local p = promise.new(function (resolve, reject) |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
36 res, rej = resolve, reject; |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
37 end); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
38 local cb = spy.new(function (v) |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
39 assert.equal("foo", v); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
40 end); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
41 p:next(cb, cb); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
42 assert.spy(cb).was_called(0); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
43 res("foo"); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
44 assert.spy(cb).was_called(1); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
45 rej("bar"); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
46 assert.spy(cb).was_called(1); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
47 rej(promise.resolve("bar")); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
48 assert.spy(cb).was_called(1); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
49 res(promise.reject("bar")); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
50 assert.spy(cb).was_called(1); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
51 res(promise.resolve("bar")); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
52 assert.spy(cb).was_called(1); |
eb9814372c54
util.promise: Remove some redundant checks, add tests confirming redundancy
Matthew Wild <mwild1@gmail.com>
parents:
11951
diff
changeset
|
53 end); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 it("allows chaining :next() calls", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 local r; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 local p = promise.new(function (resolve) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 r = resolve; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 local cb1 = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 assert.equal("foo", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 return "bar"; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 local cb2 = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 assert.equal("bar", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 p:next(cb1):next(cb2); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 assert.spy(cb1).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 assert.spy(cb2).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 r("foo"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 assert.spy(cb1).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 assert.spy(cb2).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 assert.equal("bar", result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 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
|
77 local r; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 local p = promise.new(function (resolve) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 r = resolve; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 local cb1 = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 assert.equal("foo", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 local cb2 = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 assert.equal("foo", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 p:next(cb1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 p:next(cb2); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 assert.spy(cb1).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 assert.spy(cb2).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 r("foo"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 assert.spy(cb1).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 assert.spy(cb2).was_called(1); |
9457
d01cff2f7a26
util.promise tests: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
9456
diff
changeset
|
97 assert.equal("foo", result); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 it("automatically rejects on error", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 local r; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 local p = promise.new(function (resolve) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 r = resolve; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 error("oh no"); |
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 local cb = spy.new(function () end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 local err_cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 assert.equal("oh no", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 p:next(cb, err_cb); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 assert.spy(err_cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 r("foo"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 assert.spy(err_cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 it("supports reject()", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 local r, result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 local p = promise.new(function (resolve, reject) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 r = reject; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 local cb = spy.new(function () end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 local err_cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 assert.equal("oh doh", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 p:next(cb, err_cb); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 assert.spy(err_cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 r("oh doh"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 assert.spy(err_cb).was_called(1); |
9457
d01cff2f7a26
util.promise tests: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
9456
diff
changeset
|
132 assert.equal("oh doh", result); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 it("supports chaining of rejected promises", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 local r, result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 local p = promise.new(function (resolve, reject) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 r = reject; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 local cb = spy.new(function () end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 local err_cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 assert.equal("oh doh", v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 return "ok" |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 local cb2 = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 end); |
9457
d01cff2f7a26
util.promise tests: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
9456
diff
changeset
|
148 local err_cb2 = spy.new(function () end); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 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
|
150 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 assert.spy(err_cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 assert.spy(cb2).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 assert.spy(err_cb2).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 r("oh doh"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 assert.spy(err_cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 assert.spy(cb2).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 assert.spy(err_cb2).was_called(0); |
9457
d01cff2f7a26
util.promise tests: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
9456
diff
changeset
|
159 assert.equal("ok", result); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 |
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
|
162 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
|
163 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
|
164 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
|
165 local p = promise.new(function (resolve, reject) |
9550
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
166 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
|
167 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 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 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
|
177 assert.spy(err_cb).was_called(1); |
9550
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
178 assert.spy(err_cb).was_called_with("oh doh"); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
179 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
|
180 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
|
181 |
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
|
182 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
|
183 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
|
184 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
|
185 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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 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
|
199 |
9550
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
200 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
|
201 local p = promise.resolve("foo"); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
202 local cb = spy.new(function () end); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
203 local p2 = p:catch(cb); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
204 assert.spy(cb).was_called(0); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
205 |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
206 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
|
207 p2:catch(cb2); |
9550
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
208 assert.spy(cb2).was_called(0); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
209 end); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
210 |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
211 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
|
212 local p = promise.reject("foo"); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
213 local cb = spy.new(function () end); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
214 local p2 = p:next(cb); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
215 assert.spy(cb).was_called(0); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
216 |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
217 local cb2 = spy.new(function () end); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
218 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
|
219 p2:next(cb2, cb2_err); |
9550
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
220 assert.spy(cb2).was_called(0); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
221 assert.spy(cb2_err).was_called(1); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
222 assert.spy(cb2_err).was_called_with("foo"); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
223 end); |
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
224 |
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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 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
|
230 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
|
231 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
|
232 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
|
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 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
|
235 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
|
236 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
|
237 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
|
238 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
|
239 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
|
240 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
|
241 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
|
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 |
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 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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 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
|
252 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
|
253 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
|
254 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
|
255 |
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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 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
|
265 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
|
266 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
|
267 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
|
268 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
|
269 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
|
270 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
|
271 end); |
10297
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
272 |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
273 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
|
274 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
|
275 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
|
276 return "hello" |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
277 end); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
278 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
|
279 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
|
280 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
|
281 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
|
282 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
|
283 end); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
284 |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
285 it("nil", function () |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
286 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
|
287 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
|
288 return |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
289 end); |
da9f21a70e52
util.promise: Add some additional tests to cover callback return values
Matthew Wild <mwild1@gmail.com>
parents:
9557
diff
changeset
|
290 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
|
291 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
|
292 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
|
293 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
|
294 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
|
295 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
|
296 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
|
297 |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
298 describe("race()", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
299 it("works with fulfilled promises", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
300 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
|
301 local p = promise.race({ p1, p2 }); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
302 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
303 p:next(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
304 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
305 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
306 assert.equal("yep", result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
307 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
308 it("works with pending promises", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
309 local r1, r2; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
310 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
|
311 local p = promise.race({ p1, p2 }); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
312 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
313 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
314 local cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
315 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
316 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
317 p:next(cb); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
318 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
319 r2("yep"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
320 r1("nope"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
321 assert.spy(cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
322 assert.equal("yep", result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
323 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
324 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
325 describe("all()", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
326 it("works with fulfilled promises", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
327 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
|
328 local p = promise.all({ p1, p2 }); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
329 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
330 p:next(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
331 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
332 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
333 assert.same({ "yep", "nope" }, result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
334 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
335 it("works with pending promises", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
336 local r1, r2; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
337 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
|
338 local p = promise.all({ p1, p2 }); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
339 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
340 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
341 local cb = 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); |
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 r2("yep"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
347 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
348 r1("nope"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
349 assert.spy(cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
350 assert.same({ "nope", "yep" }, result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
351 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
352 it("rejects if any promise rejects", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
353 local r1, r2; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
354 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
|
355 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
|
356 local p = promise.all({ p1, p2 }); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
357 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
358 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
359 local cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
360 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
361 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
362 local cb_err = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
363 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
364 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
365 p:next(cb, cb_err); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
366 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
367 assert.spy(cb_err).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
368 r2("fail"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
369 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
370 assert.spy(cb_err).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
371 r1("nope"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
372 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
373 assert.spy(cb_err).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
374 assert.equal("fail", result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
375 end); |
11483
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
376 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
|
377 local r1, r2; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
378 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
|
379 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
|
380 |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
381 local result; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
382 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
|
383 result = v; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
384 end); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
385 p:next(cb); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
386 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
|
387 r2("yep"); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
388 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
|
389 r1("nope"); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
390 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
|
391 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
|
392 end); |
11485
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
393 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
|
394 local r1; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
395 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
|
396 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
|
397 |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
398 local result; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
399 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
|
400 result = v; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
401 end); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
402 p:next(cb); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
403 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
|
404 r1("nope"); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
405 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
|
406 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
|
407 end); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
408 end); |
10922
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
409 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
|
410 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
|
411 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
|
412 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
|
413 local result; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
414 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
|
415 result = v; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
416 end); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
417 assert.same({ |
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 { 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
|
420 }, result); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
421 end); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
422 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
|
423 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
|
424 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
|
425 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
|
426 |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
427 local result; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
428 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
|
429 result = v; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
430 end); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
431 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
|
432 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
|
433 r2("yep"); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
434 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
|
435 r1("nope"); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
436 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
|
437 assert.same({ |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
438 { 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
|
439 { 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
|
440 }, result); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
441 end); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
442 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
|
443 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
|
444 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
|
445 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
|
446 |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
447 local result; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
448 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
|
449 result = v; |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
450 end); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
451 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
|
452 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
|
453 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
|
454 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
|
455 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
|
456 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
|
457 assert.same({ |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
458 { 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
|
459 { 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
|
460 }, result); |
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
461 end); |
11483
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
462 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
|
463 local r1, r2; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
464 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
|
465 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
|
466 |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
467 local result; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
468 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
|
469 result = v; |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
470 end); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
471 p:next(cb); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
472 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
|
473 r2("yep"); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
474 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
|
475 r1("nope"); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
476 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
|
477 assert.same({ |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
478 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
|
479 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
|
480 }, result); |
24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
Matthew Wild <mwild1@gmail.com>
parents:
10922
diff
changeset
|
481 end); |
11485
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
482 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
|
483 local r1; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
484 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
|
485 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
|
486 |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
487 local result; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
488 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
|
489 result = v; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
490 end); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
491 p:next(cb); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
492 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
|
493 r1("nope"); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
494 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
|
495 assert.same({ |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
496 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
|
497 bar = "yep"; |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
498 }, result); |
7d42ed3a8a40
util.promise: all()/all_settled() pass through non-promise values
Matthew Wild <mwild1@gmail.com>
parents:
11484
diff
changeset
|
499 end); |
10922
7d3dbb9eb3eb
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Matthew Wild <mwild1@gmail.com>
parents:
10297
diff
changeset
|
500 end); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
501 describe("catch()", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
502 it("works", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
503 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
504 local p = promise.new(function (resolve) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
505 error({ foo = true }); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
506 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
507 local cb1 = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
508 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
509 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
510 assert.spy(cb1).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
511 p:catch(cb1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
512 assert.spy(cb1).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
513 assert.same({ foo = true }, result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
514 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
515 end); |
11484
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
516 describe("join()", function () |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
517 it("works", function () |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
518 local r1, r2; |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
519 local res1, res2; |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
520 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
|
521 |
11486
78d843faaffc
util.promise: Switch order of parameters to join()
Matthew Wild <mwild1@gmail.com>
parents:
11485
diff
changeset
|
522 local p = promise.join(function (_res1, _res2) |
11484
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
523 res1, res2 = _res1, _res2; |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
524 return promise.resolve("works"); |
11486
78d843faaffc
util.promise: Switch order of parameters to join()
Matthew Wild <mwild1@gmail.com>
parents:
11485
diff
changeset
|
525 end, p1, p2); |
11484
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
526 |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
527 local result; |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
528 local cb = spy.new(function (v) |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
529 result = v; |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
530 end); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
531 p:next(cb); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
532 assert.spy(cb).was_called(0); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
533 r2("yep"); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
534 assert.spy(cb).was_called(0); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
535 r1("nope"); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
536 assert.spy(cb).was_called(1); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
537 assert.same("works", result); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
538 assert.equals("nope", res1); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
539 assert.equals("yep", res2); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
540 end); |
a0120e935442
util.promise: Add join() convenience method
Matthew Wild <mwild1@gmail.com>
parents:
11483
diff
changeset
|
541 end); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
542 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
|
543 local r1, r2; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
544 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
|
545 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
546 local result; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
547 local cb = spy.new(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
548 result = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
549 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
550 p1:next(cb); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
551 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
552 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
553 r1(p2); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
554 assert.spy(cb).was_called(0); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
555 r2("yep"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
556 assert.spy(cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
557 assert.equal("yep", result); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
558 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
559 describe("reject()", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
560 it("returns a rejected promise", function () |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
561 local p = promise.reject("foo"); |
9457
d01cff2f7a26
util.promise tests: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
9456
diff
changeset
|
562 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
|
563 p:catch(cb); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
564 assert.spy(cb).was_called(1); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
565 assert.spy(cb).was_called_with("foo"); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
566 end); |
9511
cb88d729e98d
util.promise: Add additional test for promise.reject()
Matthew Wild <mwild1@gmail.com>
parents:
9510
diff
changeset
|
567 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
|
568 local p = promise.reject("foo"); |
cb88d729e98d
util.promise: Add additional test for promise.reject()
Matthew Wild <mwild1@gmail.com>
parents:
9510
diff
changeset
|
569 local cb = spy.new(function () end); |
cb88d729e98d
util.promise: Add additional test for promise.reject()
Matthew Wild <mwild1@gmail.com>
parents:
9510
diff
changeset
|
570 p:next(cb); |
cb88d729e98d
util.promise: Add additional test for promise.reject()
Matthew Wild <mwild1@gmail.com>
parents:
9510
diff
changeset
|
571 assert.spy(cb).was_called(0); |
cb88d729e98d
util.promise: Add additional test for promise.reject()
Matthew Wild <mwild1@gmail.com>
parents:
9510
diff
changeset
|
572 end); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
573 end); |
9514
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
574 describe("finally()", function () |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
575 local p, p2, resolve, reject, on_finally; |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
576 before_each(function () |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
577 p = promise.new(function (_resolve, _reject) |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
578 resolve, reject = _resolve, _reject; |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
579 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
580 on_finally = spy.new(function () end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
581 p2 = p:finally(on_finally); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
582 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
583 it("runs when a promise is resolved", function () |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
584 assert.spy(on_finally).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
585 resolve("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
586 assert.spy(on_finally).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
587 assert.spy(on_finally).was_not_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
588 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
589 it("runs when a promise is rejected", function () |
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 reject("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
592 assert.spy(on_finally).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
593 assert.spy(on_finally).was_not_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
594 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
595 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
|
596 local cb2 = spy.new(function () end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
597 p2:next(cb2); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
598 assert.spy(on_finally).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
599 assert.spy(cb2).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
600 resolve("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
601 assert.spy(on_finally).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
602 assert.spy(cb2).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
603 assert.spy(on_finally).was_not_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
604 assert.spy(cb2).was_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
605 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
606 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
|
607 local on_finally_err = spy.new(function () end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
608 local on_finally_ok = 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 p2:next(on_finally_ok); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
611 assert.spy(on_finally).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
612 assert.spy(on_finally_err).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
613 reject("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
614 assert.spy(on_finally).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
615 -- 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
|
616 assert.spy(on_finally_ok).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
617 assert.spy(on_finally_err).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
618 assert.spy(on_finally).was_not_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
619 assert.spy(on_finally_err).was_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
620 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
621 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
|
622 p = promise.new(function (_resolve, _reject) |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
623 resolve, reject = _resolve, _reject; |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
624 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
625 local test_error = {}; |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
626 on_finally = spy.new(function () error(test_error) end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
627 p2 = p:finally(on_finally); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
628 |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
629 local on_finally_err = spy.new(function () end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
630 p2:catch(on_finally_err); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
631 assert.spy(on_finally).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
632 assert.spy(on_finally_err).was_called(0); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
633 reject("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
634 assert.spy(on_finally).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
635 assert.spy(on_finally_err).was_called(1); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
636 assert.spy(on_finally).was_not_called_with("foo"); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
637 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
|
638 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
|
639 end); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9511
diff
changeset
|
640 end); |
9517
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
641 describe("try()", function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
642 it("works with functions that return a promise", function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
643 local resolve; |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
644 local p = promise.try(function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
645 return promise.new(function (_resolve) |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
646 resolve = _resolve; |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
647 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
648 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
649 assert.is_function(resolve); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
650 local on_resolved = spy.new(function () end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
651 p:next(on_resolved); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
652 assert.spy(on_resolved).was_not_called(); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
653 resolve("foo"); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
654 assert.spy(on_resolved).was_called_with("foo"); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
655 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
656 |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
657 it("works with functions that return a value", function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
658 local p = promise.try(function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
659 return "foo"; |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
660 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
661 local on_resolved = spy.new(function () end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
662 p:next(on_resolved); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
663 assert.spy(on_resolved).was_called_with("foo"); |
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 |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
666 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
|
667 local reject; |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
668 local p = promise.try(function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
669 return promise.new(function (_, _reject) |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
670 reject = _reject; |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
671 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
672 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
673 assert.is_function(reject); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
674 local on_rejected = spy.new(function () end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
675 p:catch(on_rejected); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
676 assert.spy(on_rejected).was_not_called(); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
677 reject("foo"); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
678 assert.spy(on_rejected).was_called_with("foo"); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
679 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
680 |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
681 it("works with functions that throw errors", function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
682 local test_error = {}; |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
683 local p = promise.try(function () |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
684 error(test_error); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
685 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
686 local on_rejected = spy.new(function () end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
687 p:catch(on_rejected); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
688 assert.spy(on_rejected).was_called(1); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
689 assert.spy(on_rejected).was_called_with(test_error); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
690 end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9514
diff
changeset
|
691 end); |
11947
073e53b72792
util.promise: Support delayed promise execution
Kim Alvefur <zash@zash.se>
parents:
11727
diff
changeset
|
692 describe("set_nexttick()", function () |
073e53b72792
util.promise: Support delayed promise execution
Kim Alvefur <zash@zash.se>
parents:
11727
diff
changeset
|
693 it("works", function () |
073e53b72792
util.promise: Support delayed promise execution
Kim Alvefur <zash@zash.se>
parents:
11727
diff
changeset
|
694 local next_tick = spy.new(function (f) |
073e53b72792
util.promise: Support delayed promise execution
Kim Alvefur <zash@zash.se>
parents:
11727
diff
changeset
|
695 f(); |
073e53b72792
util.promise: Support delayed promise execution
Kim Alvefur <zash@zash.se>
parents:
11727
diff
changeset
|
696 end) |
11951 | 697 local cb = spy.new(function () end); |
11947
073e53b72792
util.promise: Support delayed promise execution
Kim Alvefur <zash@zash.se>
parents:
11727
diff
changeset
|
698 promise.set_nexttick(next_tick); |
073e53b72792
util.promise: Support delayed promise execution
Kim Alvefur <zash@zash.se>
parents:
11727
diff
changeset
|
699 promise.new(function (y, _) |
073e53b72792
util.promise: Support delayed promise execution
Kim Alvefur <zash@zash.se>
parents:
11727
diff
changeset
|
700 y("okay"); |
073e53b72792
util.promise: Support delayed promise execution
Kim Alvefur <zash@zash.se>
parents:
11727
diff
changeset
|
701 end):next(cb); |
073e53b72792
util.promise: Support delayed promise execution
Kim Alvefur <zash@zash.se>
parents:
11727
diff
changeset
|
702 assert.spy(next_tick).was.called(); |
073e53b72792
util.promise: Support delayed promise execution
Kim Alvefur <zash@zash.se>
parents:
11727
diff
changeset
|
703 assert.spy(cb).was.called_with("okay"); |
073e53b72792
util.promise: Support delayed promise execution
Kim Alvefur <zash@zash.se>
parents:
11727
diff
changeset
|
704 end); |
073e53b72792
util.promise: Support delayed promise execution
Kim Alvefur <zash@zash.se>
parents:
11727
diff
changeset
|
705 end) |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
706 end); |