Diff

spec/util_async_spec.lua @ 8630:deade38ffbbd

util.async: tests: slight modifications to allow more code reuse in tests
author Matthew Wild <mwild1@gmail.com>
date Mon, 19 Mar 2018 16:24:42 +0000
parent 8628:e88744fa0985
child 8631:1daabc077393
line wrap: on
line diff
--- a/spec/util_async_spec.lua	Mon Mar 19 11:51:40 2018 +0000
+++ b/spec/util_async_spec.lua	Mon Mar 19 16:24:42 2018 +0000
@@ -9,11 +9,16 @@
 		print = function () end
 	end
 
-	local function mock_watchers()
+	local function mock_watchers(event_log)
+		local function generic_logging_watcher(name)
+			return function (...)
+				table.insert(event_log, { name = name, n = select("#", ...)-1, select(2, ...) });
+			end;
+		end;
 		return setmetatable(mock{
-			ready = function () end;
-			waiting = function () end;
-			error = function () end;
+			ready = generic_logging_watcher("ready");
+			waiting = generic_logging_watcher("waiting");
+			error = generic_logging_watcher("error");
 		}, {
 			__index = function (_, event)
 				-- Unexpected watcher called
@@ -23,8 +28,9 @@
 	end
 
 	local function new(func, name)
-		local log = {};
-		return async.runner(func, mock_watchers()), log;
+		local event_log = {};
+		local spy_func = spy.new(func);
+		return async.runner(spy_func, mock_watchers(event_log)), event_log, spy_func;
 	end
 	describe("#runner", function()
 		it("should work", function()
@@ -537,12 +543,11 @@
 		it("should support multiple done() calls", function ()
 			local processed_item;
 			local wait, done;
-			local rf = spy.new(function (item)
+			local r, _, rf = new(function (item)
 				wait, done = async.waiter(4);
 				wait();
 				processed_item = item;
 			end);
-			local r = async.runner(rf, mock_watchers());
 			r:run("test");
 			for i = 1, 3 do
 				done();
@@ -558,12 +563,11 @@
 		it("should not allow done() to be called more than specified", function ()
 			local processed_item;
 			local wait, done;
-			local rf = spy.new(function (item)
+			local r, _, rf = new(function (item)
 				wait, done = async.waiter(4);
 				wait();
 				processed_item = item;
 			end);
-			local r = async.runner(rf, mock_watchers());
 			r:run("test");
 			for i = 1, 4 do
 				done();
@@ -576,13 +580,12 @@
 
 		it("should allow done() to be called before wait()", function ()
 			local processed_item;
-			local rf = spy.new(function (item)
+			local r, _, rf = new(function (item)
 				local wait, done = async.waiter();
 				done();
 				wait();
 				processed_item = item;
 			end);
-			local r = async.runner(rf, mock_watchers());
 			r:run("test");
 			assert.equal(processed_item, "test");
 			assert.equal(r.state, "ready");