Annotate

spec/util_promise_spec.lua @ 13241:0419de4e4db1

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