File

spec/util_events_spec.lua @ 11748:88ba05494d17 0.11

makefile: fix prosody.version target POSIX is quite explicit regarding the precedence of AND-OR lists [0]: > The operators "&&" and "||" shall have equal precedence and shall be > evaluated with left associativity. For example, both of the following > commands write solely `bar` to standard output: > false && echo foo || echo bar > true || echo foo && echo bar Given that, `prosody.version` target behaves as ((((((test -f prosody.release && cp ...) || test -f ...) && sed ...) || test -f ...) && hexdump ...) || echo unknown > $@) In the case of release tarballs, `prosody.release` does exist, so the first AND pair is executed. Given that it's successful, then the first `test -f` in the OR pair is ignored, and instead the `sed` in the AND pair is executed. `sed` success, as `.hg_archival.txt` exists, making the second `test -f` in the OR pair ignored, and `hexdump` in the AND pair is executed. Now, given that `.hg` doesn't exist, it fails, so the last `echo` is run, overwriting `prosody.version` with `unknown`. This can be worked around placing `()` around the AND pairs. Decided to use conditionals instead, as I think they better communicate the intention of the block. [0]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_03
author Lucas <lucas@sexy.is>
date Sun, 15 Aug 2021 04:10:36 +0000
parent 8802:befffddf1b25
child 11058:c99afee1c548
line wrap: on
line source

local events = require "util.events";

describe("util.events", function ()
	it("should export a new() function", function ()
		assert.is_function(events.new);
	end);
	describe("new()", function ()
		it("should return return a new events object", function ()
			local e = events.new();
			assert.is_function(e.add_handler);
			assert.is_function(e.remove_handler);
		end);
	end);

	local e, h;


	describe("API", function ()
		before_each(function ()
			e = events.new();
			h = spy.new(function () end);
		end);

		it("should call handlers when an event is fired", function ()
			e.add_handler("myevent", h);
			e.fire_event("myevent");
			assert.spy(h).was_called();
		end);

		it("should not call handlers when a different event is fired", function ()
			e.add_handler("myevent", h);
			e.fire_event("notmyevent");
			assert.spy(h).was_not_called();
		end);

		it("should pass the data argument to handlers", function ()
			e.add_handler("myevent", h);
			e.fire_event("myevent", "mydata");
			assert.spy(h).was_called_with("mydata");
		end);

		it("should support non-string events", function ()
			local myevent = {};
			e.add_handler(myevent, h);
			e.fire_event(myevent, "mydata");
			assert.spy(h).was_called_with("mydata");
		end);

		it("should call handlers in priority order", function ()
			local data = {};
			e.add_handler("myevent", function () table.insert(data, "h1"); end, 5);
			e.add_handler("myevent", function () table.insert(data, "h2"); end, 3);
			e.add_handler("myevent", function () table.insert(data, "h3"); end);
			e.fire_event("myevent", "mydata");
			assert.same(data, { "h1", "h2", "h3" });
		end);

		it("should support non-integer priority values", function ()
			local data = {};
			e.add_handler("myevent", function () table.insert(data, "h1"); end, 1);
			e.add_handler("myevent", function () table.insert(data, "h2"); end, 0.5);
			e.add_handler("myevent", function () table.insert(data, "h3"); end, 0.25);
			e.fire_event("myevent", "mydata");
			assert.same(data, { "h1", "h2", "h3" });
		end);

		it("should support negative priority values", function ()
			local data = {};
			e.add_handler("myevent", function () table.insert(data, "h1"); end, 1);
			e.add_handler("myevent", function () table.insert(data, "h2"); end, 0);
			e.add_handler("myevent", function () table.insert(data, "h3"); end, -1);
			e.fire_event("myevent", "mydata");
			assert.same(data, { "h1", "h2", "h3" });
		end);

		it("should support removing handlers", function ()
			e.add_handler("myevent", h);
			e.fire_event("myevent");
			e.remove_handler("myevent", h);
			e.fire_event("myevent");
			assert.spy(h).was_called(1);
		end);

		it("should support adding multiple handlers at the same time", function ()
			local ht = {
				myevent1 = spy.new(function () end);
				myevent2 = spy.new(function () end);
				myevent3 = spy.new(function () end);
			};
			e.add_handlers(ht);
			e.fire_event("myevent1");
			e.fire_event("myevent2");
			assert.spy(ht.myevent1).was_called();
			assert.spy(ht.myevent2).was_called();
			assert.spy(ht.myevent3).was_not_called();
		end);

		it("should support removing multiple handlers at the same time", function ()
			local ht = {
				myevent1 = spy.new(function () end);
				myevent2 = spy.new(function () end);
				myevent3 = spy.new(function () end);
			};
			e.add_handlers(ht);
			e.remove_handlers(ht);
			e.fire_event("myevent1");
			e.fire_event("myevent2");
			assert.spy(ht.myevent1).was_not_called();
			assert.spy(ht.myevent2).was_not_called();
			assert.spy(ht.myevent3).was_not_called();
		end);

		pending("should support adding handlers within an event handler")
		pending("should support removing handlers within an event handler")

		it("should support getting the current handlers for an event", function ()
			e.add_handler("myevent", h);
			local handlers = e.get_handlers("myevent");
			assert.equal(h, handlers[1]);
		end);

		describe("wrappers", function ()
			local w
			before_each(function ()
				w = spy.new(function (handlers, event_name, event_data)
					assert.is_function(handlers);
					assert.equal("myevent", event_name)
					assert.equal("abc", event_data);
					return handlers(event_name, event_data);
				end);
			end);

			it("should get called", function ()
				e.add_wrapper("myevent", w);
				e.add_handler("myevent", h);
				e.fire_event("myevent", "abc");
				assert.spy(w).was_called(1);
				assert.spy(h).was_called(1);
			end);

			it("should be removable", function ()
				e.add_wrapper("myevent", w);
				e.add_handler("myevent", h);
				e.fire_event("myevent", "abc");
				e.remove_wrapper("myevent", w);
				e.fire_event("myevent", "abc");
				assert.spy(w).was_called(1);
				assert.spy(h).was_called(2);
			end);

			it("should allow multiple wrappers", function ()
				local w2 = spy.new(function (handlers, event_name, event_data)
					return handlers(event_name, event_data);
				end);
				e.add_wrapper("myevent", w);
				e.add_handler("myevent", h);
				e.add_wrapper("myevent", w2);
				e.fire_event("myevent", "abc");
				e.remove_wrapper("myevent", w);
				e.fire_event("myevent", "abc");
				assert.spy(w).was_called(1);
				assert.spy(w2).was_called(2);
				assert.spy(h).was_called(2);
			end);

			it("should support a mix of global and event wrappers", function ()
				local w2 = spy.new(function (handlers, event_name, event_data)
					return handlers(event_name, event_data);
				end);
				e.add_wrapper(false, w);
				e.add_handler("myevent", h);
				e.add_wrapper("myevent", w2);
				e.fire_event("myevent", "abc");
				e.remove_wrapper(false, w);
				e.fire_event("myevent", "abc");
				assert.spy(w).was_called(1);
				assert.spy(w2).was_called(2);
				assert.spy(h).was_called(2);
			end);
		end);

		describe("global wrappers", function ()
			local w
			before_each(function ()
				w = spy.new(function (handlers, event_name, event_data)
					assert.is_function(handlers);
					assert.equal("myevent", event_name)
					assert.equal("abc", event_data);
					return handlers(event_name, event_data);
				end);
			end);

			it("should get called", function ()
				e.add_wrapper(false, w);
				e.add_handler("myevent", h);
				e.fire_event("myevent", "abc");
				assert.spy(w).was_called(1);
				assert.spy(h).was_called(1);
			end);

			it("should be removable", function ()
				e.add_wrapper(false, w);
				e.add_handler("myevent", h);
				e.fire_event("myevent", "abc");
				e.remove_wrapper(false, w);
				e.fire_event("myevent", "abc");
				assert.spy(w).was_called(1);
				assert.spy(h).was_called(2);
			end);
		end);
	end);
end);