aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_events_spec.lua
blob: 82b303110b388e0977d1cae09710d4b2e4748c3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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")

		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);
		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);