Software /
code /
prosody
Comparison
spec/util_promise_spec.lua @ 9550:98de4c2e2627
util.promise: Fix missing parameters
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 25 Oct 2018 15:24:52 +0100 |
parent | 9549:800c274928bf |
child | 9551:7421379ba464 |
comparison
equal
deleted
inserted
replaced
9549:800c274928bf | 9550:98de4c2e2627 |
---|---|
140 | 140 |
141 it("propagates errors down the chain, even when some handlers are not provided", function () | 141 it("propagates errors down the chain, even when some handlers are not provided", function () |
142 local r, result; | 142 local r, result; |
143 local test_error = {}; | 143 local test_error = {}; |
144 local p = promise.new(function (resolve, reject) | 144 local p = promise.new(function (resolve, reject) |
145 r = resolve; | 145 r = reject; |
146 end); | 146 end); |
147 local cb = spy.new(function () end); | 147 local cb = spy.new(function () end); |
148 local err_cb = spy.new(function (e) result = e end); | 148 local err_cb = spy.new(function (e) result = e end); |
149 local p2 = p:next(function () error(test_error) end); | 149 local p2 = p:next(function () error(test_error) end); |
150 local p3 = p2:next(cb) | 150 local p3 = p2:next(cb) |
152 assert.spy(cb).was_called(0); | 152 assert.spy(cb).was_called(0); |
153 assert.spy(err_cb).was_called(0); | 153 assert.spy(err_cb).was_called(0); |
154 r("oh doh"); | 154 r("oh doh"); |
155 assert.spy(cb).was_called(0); | 155 assert.spy(cb).was_called(0); |
156 assert.spy(err_cb).was_called(1); | 156 assert.spy(err_cb).was_called(1); |
157 assert.equal(test_error, result); | 157 assert.spy(err_cb).was_called_with("oh doh"); |
158 assert.equal("oh doh", result); | |
158 end); | 159 end); |
159 | 160 |
160 it("propagates values down the chain, even when some handlers are not provided", function () | 161 it("propagates values down the chain, even when some handlers are not provided", function () |
161 local r; | 162 local r; |
162 local p = promise.new(function (resolve, reject) | 163 local p = promise.new(function (resolve, reject) |
171 assert.spy(err_cb).was_called(0); | 172 assert.spy(err_cb).was_called(0); |
172 r(1337); | 173 r(1337); |
173 assert.spy(cb).was_called(1); | 174 assert.spy(cb).was_called(1); |
174 assert.spy(cb).was_called_with(1337); | 175 assert.spy(cb).was_called_with(1337); |
175 assert.spy(err_cb).was_called(0); | 176 assert.spy(err_cb).was_called(0); |
177 end); | |
178 | |
179 it("fulfilled promises do not call error handlers and do propagate value", function () | |
180 local p = promise.resolve("foo"); | |
181 local cb = spy.new(function () end); | |
182 local p2 = p:catch(cb); | |
183 assert.spy(cb).was_called(0); | |
184 | |
185 local cb2 = spy.new(function () end); | |
186 local p3 = p2:catch(cb2); | |
187 assert.spy(cb2).was_called(0); | |
188 end); | |
189 | |
190 it("rejected promises do not call fulfilled handlers and do propagate reason", function () | |
191 local p = promise.reject("foo"); | |
192 local cb = spy.new(function () end); | |
193 local p2 = p:next(cb); | |
194 assert.spy(cb).was_called(0); | |
195 | |
196 local cb2 = spy.new(function () end); | |
197 local cb2_err = spy.new(function () end); | |
198 local p3 = p2:next(cb2, cb2_err); | |
199 assert.spy(cb2).was_called(0); | |
200 assert.spy(cb2_err).was_called(1); | |
201 assert.spy(cb2_err).was_called_with("foo"); | |
176 end); | 202 end); |
177 | 203 |
178 describe("race()", function () | 204 describe("race()", function () |
179 it("works with fulfilled promises", function () | 205 it("works with fulfilled promises", function () |
180 local p1, p2 = promise.resolve("yep"), promise.resolve("nope"); | 206 local p1, p2 = promise.resolve("yep"), promise.resolve("nope"); |