Diff

spec/util_promise_spec.lua @ 12749:eb9814372c54

util.promise: Remove some redundant checks, add tests confirming redundancy This lines don't appear to do anything useful, and all tests pass when they are removed. Discovered via mutation testing. I added extra tests to exercise this code, because I wasn't certain that there were no side-effects caused by removal. Everything appears to be fine, thanks to the "pending" check at the start of promise_settle().
author Matthew Wild <mwild1@gmail.com>
date Fri, 07 Oct 2022 17:43:26 +0100
parent 11951:6d9e3f541830
child 12751:2639e0e1c378
line wrap: on
line diff
--- a/spec/util_promise_spec.lua	Fri Oct 07 17:01:35 2022 +0100
+++ b/spec/util_promise_spec.lua	Fri Oct 07 17:43:26 2022 +0100
@@ -30,6 +30,27 @@
 		r("foo");
 		assert.spy(cb).was_called(1);
 	end);
+	it("ignores resolve/reject of settled promises", function ()
+		local res, rej;
+		local p = promise.new(function (resolve, reject)
+			res, rej = resolve, reject;
+		end);
+		local cb = spy.new(function (v)
+			assert.equal("foo", v);
+		end);
+		p:next(cb, cb);
+		assert.spy(cb).was_called(0);
+		res("foo");
+		assert.spy(cb).was_called(1);
+		rej("bar");
+		assert.spy(cb).was_called(1);
+		rej(promise.resolve("bar"));
+		assert.spy(cb).was_called(1);
+		res(promise.reject("bar"));
+		assert.spy(cb).was_called(1);
+		res(promise.resolve("bar"));
+		assert.spy(cb).was_called(1);
+	end);
 	it("allows chaining :next() calls", function ()
 		local r;
 		local result;