Software /
code /
prosody
Comparison
spec/util_promise_spec.lua @ 9457:d01cff2f7a26
util.promise tests: Fix luacheck warnings
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 10 Oct 2018 18:02:36 +0100 |
parent | 9456:d54a0e129af8 |
child | 9510:8ef46d09386a |
comparison
equal
deleted
inserted
replaced
9456:d54a0e129af8 | 9457:d01cff2f7a26 |
---|---|
1 local promise = require "util.promise"; | 1 local promise = require "util.promise"; |
2 | 2 |
3 describe("util.promise", function () | 3 describe("util.promise", function () |
4 --luacheck: ignore 212/resolve 212/reject | |
4 describe("new()", function () | 5 describe("new()", function () |
5 it("returns a promise object", function () | 6 it("returns a promise object", function () |
6 assert(promise.new()); | 7 assert(promise.new()); |
7 end); | 8 end); |
8 end); | 9 end); |
70 assert.spy(cb1).was_called(0); | 71 assert.spy(cb1).was_called(0); |
71 assert.spy(cb2).was_called(0); | 72 assert.spy(cb2).was_called(0); |
72 r("foo"); | 73 r("foo"); |
73 assert.spy(cb1).was_called(1); | 74 assert.spy(cb1).was_called(1); |
74 assert.spy(cb2).was_called(1); | 75 assert.spy(cb2).was_called(1); |
75 assert.equal("foo", result); | 76 assert.equal("foo", result); |
76 end); | 77 end); |
77 it("automatically rejects on error", function () | 78 it("automatically rejects on error", function () |
78 local r; | 79 local r; |
79 local p = promise.new(function (resolve) | 80 local p = promise.new(function (resolve) |
80 r = resolve; | 81 r = resolve; |
105 assert.spy(cb).was_called(0); | 106 assert.spy(cb).was_called(0); |
106 assert.spy(err_cb).was_called(0); | 107 assert.spy(err_cb).was_called(0); |
107 r("oh doh"); | 108 r("oh doh"); |
108 assert.spy(cb).was_called(0); | 109 assert.spy(cb).was_called(0); |
109 assert.spy(err_cb).was_called(1); | 110 assert.spy(err_cb).was_called(1); |
110 assert.equal("oh doh", result); | 111 assert.equal("oh doh", result); |
111 end); | 112 end); |
112 it("supports chaining of rejected promises", function () | 113 it("supports chaining of rejected promises", function () |
113 local r, result; | 114 local r, result; |
114 local p = promise.new(function (resolve, reject) | 115 local p = promise.new(function (resolve, reject) |
115 r = reject; | 116 r = reject; |
121 return "ok" | 122 return "ok" |
122 end); | 123 end); |
123 local cb2 = spy.new(function (v) | 124 local cb2 = spy.new(function (v) |
124 result = v; | 125 result = v; |
125 end); | 126 end); |
126 local err_cb2 = spy.new(function (v) end); | 127 local err_cb2 = spy.new(function () end); |
127 p:next(cb, err_cb):next(cb2, err_cb2) | 128 p:next(cb, err_cb):next(cb2, err_cb2) |
128 assert.spy(cb).was_called(0); | 129 assert.spy(cb).was_called(0); |
129 assert.spy(err_cb).was_called(0); | 130 assert.spy(err_cb).was_called(0); |
130 assert.spy(cb2).was_called(0); | 131 assert.spy(cb2).was_called(0); |
131 assert.spy(err_cb2).was_called(0); | 132 assert.spy(err_cb2).was_called(0); |
132 r("oh doh"); | 133 r("oh doh"); |
133 assert.spy(cb).was_called(0); | 134 assert.spy(cb).was_called(0); |
134 assert.spy(err_cb).was_called(1); | 135 assert.spy(err_cb).was_called(1); |
135 assert.spy(cb2).was_called(1); | 136 assert.spy(cb2).was_called(1); |
136 assert.spy(err_cb2).was_called(0); | 137 assert.spy(err_cb2).was_called(0); |
137 assert.equal("ok", result); | 138 assert.equal("ok", result); |
138 end); | 139 end); |
139 | 140 |
140 describe("race()", function () | 141 describe("race()", function () |
141 it("works with fulfilled promises", function () | 142 it("works with fulfilled promises", function () |
142 local p1, p2 = promise.resolve("yep"), promise.resolve("nope"); | 143 local p1, p2 = promise.resolve("yep"), promise.resolve("nope"); |
249 assert.equal("yep", result); | 250 assert.equal("yep", result); |
250 end); | 251 end); |
251 describe("reject()", function () | 252 describe("reject()", function () |
252 it("returns a rejected promise", function () | 253 it("returns a rejected promise", function () |
253 local p = promise.reject("foo"); | 254 local p = promise.reject("foo"); |
254 local cb = spy.new(function (v) | 255 local cb = spy.new(function () end); |
255 result = v; | |
256 end); | |
257 p:next(cb); | 256 p:next(cb); |
258 assert.spy(cb).was_called(1); | 257 assert.spy(cb).was_called(1); |
259 assert.spy(cb).was_called_with("foo"); | 258 assert.spy(cb).was_called_with("foo"); |
260 end); | 259 end); |
261 end); | 260 end); |