Comparison

spec/util_promise_spec.lua @ 9517:b1c6ede17592

util.promise: Add promise.try()
author Matthew Wild <mwild1@gmail.com>
date Thu, 18 Oct 2018 18:03:31 +0100
parent 9514:9db707a86a25
child 9549:800c274928bf
comparison
equal deleted inserted replaced
9516:038446c50630 9517:b1c6ede17592
329 assert.spy(on_finally).was_not_called_with("foo"); 329 assert.spy(on_finally).was_not_called_with("foo");
330 assert.spy(on_finally).was_not_called_with(test_error); 330 assert.spy(on_finally).was_not_called_with(test_error);
331 assert.spy(on_finally_err).was_called_with(test_error); 331 assert.spy(on_finally_err).was_called_with(test_error);
332 end); 332 end);
333 end); 333 end);
334 describe("try()", function ()
335 it("works with functions that return a promise", function ()
336 local resolve;
337 local p = promise.try(function ()
338 return promise.new(function (_resolve)
339 resolve = _resolve;
340 end);
341 end);
342 assert.is_function(resolve);
343 local on_resolved = spy.new(function () end);
344 p:next(on_resolved);
345 assert.spy(on_resolved).was_not_called();
346 resolve("foo");
347 assert.spy(on_resolved).was_called_with("foo");
348 end);
349
350 it("works with functions that return a value", function ()
351 local p = promise.try(function ()
352 return "foo";
353 end);
354 local on_resolved = spy.new(function () end);
355 p:next(on_resolved);
356 assert.spy(on_resolved).was_called_with("foo");
357 end);
358
359 it("works with functions that return a promise that rejects", function ()
360 local reject;
361 local p = promise.try(function ()
362 return promise.new(function (_, _reject)
363 reject = _reject;
364 end);
365 end);
366 assert.is_function(reject);
367 local on_rejected = spy.new(function () end);
368 p:catch(on_rejected);
369 assert.spy(on_rejected).was_not_called();
370 reject("foo");
371 assert.spy(on_rejected).was_called_with("foo");
372 end);
373
374 it("works with functions that throw errors", function ()
375 local test_error = {};
376 local p = promise.try(function ()
377 error(test_error);
378 end);
379 local on_rejected = spy.new(function () end);
380 p:catch(on_rejected);
381 assert.spy(on_rejected).was_called(1);
382 assert.spy(on_rejected).was_called_with(test_error);
383 end);
384 end);
334 end); 385 end);