8760
|
1 local events = require "util.events";
|
|
2
|
|
3 describe("util.events", function ()
|
|
4 it("should export a new() function", function ()
|
|
5 assert.is_function(events.new);
|
|
6 end);
|
|
7 describe("new()", function ()
|
|
8 it("should return return a new events object", function ()
|
|
9 local e = events.new();
|
|
10 assert.is_function(e.add_handler);
|
|
11 assert.is_function(e.remove_handler);
|
|
12 end);
|
|
13 end);
|
|
14
|
|
15 local e, h;
|
|
16
|
|
17
|
|
18 describe("API", function ()
|
|
19 before_each(function ()
|
|
20 e = events.new();
|
|
21 h = spy.new(function () end);
|
|
22 end);
|
|
23
|
|
24 it("should call handlers when an event is fired", function ()
|
|
25 e.add_handler("myevent", h);
|
|
26 e.fire_event("myevent");
|
|
27 assert.spy(h).was_called();
|
|
28 end);
|
|
29
|
|
30 it("should not call handlers when a different event is fired", function ()
|
|
31 e.add_handler("myevent", h);
|
|
32 e.fire_event("notmyevent");
|
|
33 assert.spy(h).was_not_called();
|
|
34 end);
|
|
35
|
|
36 it("should pass the data argument to handlers", function ()
|
|
37 e.add_handler("myevent", h);
|
|
38 e.fire_event("myevent", "mydata");
|
|
39 assert.spy(h).was_called_with("mydata");
|
|
40 end);
|
|
41
|
|
42 it("should support non-string events", function ()
|
|
43 local myevent = {};
|
|
44 e.add_handler(myevent, h);
|
|
45 e.fire_event(myevent, "mydata");
|
|
46 assert.spy(h).was_called_with("mydata");
|
|
47 end);
|
|
48
|
|
49 it("should call handlers in priority order", function ()
|
|
50 local data = {};
|
|
51 e.add_handler("myevent", function () table.insert(data, "h1"); end, 5);
|
|
52 e.add_handler("myevent", function () table.insert(data, "h2"); end, 3);
|
|
53 e.add_handler("myevent", function () table.insert(data, "h3"); end);
|
|
54 e.fire_event("myevent", "mydata");
|
|
55 assert.same(data, { "h1", "h2", "h3" });
|
|
56 end);
|
|
57
|
|
58 it("should support non-integer priority values", function ()
|
|
59 local data = {};
|
|
60 e.add_handler("myevent", function () table.insert(data, "h1"); end, 1);
|
|
61 e.add_handler("myevent", function () table.insert(data, "h2"); end, 0.5);
|
|
62 e.add_handler("myevent", function () table.insert(data, "h3"); end, 0.25);
|
|
63 e.fire_event("myevent", "mydata");
|
|
64 assert.same(data, { "h1", "h2", "h3" });
|
|
65 end);
|
|
66
|
|
67 it("should support negative priority values", function ()
|
|
68 local data = {};
|
|
69 e.add_handler("myevent", function () table.insert(data, "h1"); end, 1);
|
|
70 e.add_handler("myevent", function () table.insert(data, "h2"); end, 0);
|
|
71 e.add_handler("myevent", function () table.insert(data, "h3"); end, -1);
|
|
72 e.fire_event("myevent", "mydata");
|
|
73 assert.same(data, { "h1", "h2", "h3" });
|
|
74 end);
|
|
75
|
|
76 it("should support removing handlers", function ()
|
|
77 e.add_handler("myevent", h);
|
|
78 e.fire_event("myevent");
|
|
79 e.remove_handler("myevent", h);
|
|
80 e.fire_event("myevent");
|
|
81 assert.spy(h).was_called(1);
|
|
82 end);
|
|
83
|
|
84 it("should support adding multiple handlers at the same time", function ()
|
|
85 local ht = {
|
|
86 myevent1 = spy.new(function () end);
|
|
87 myevent2 = spy.new(function () end);
|
|
88 myevent3 = spy.new(function () end);
|
|
89 };
|
|
90 e.add_handlers(ht);
|
|
91 e.fire_event("myevent1");
|
|
92 e.fire_event("myevent2");
|
|
93 assert.spy(ht.myevent1).was_called();
|
|
94 assert.spy(ht.myevent2).was_called();
|
|
95 assert.spy(ht.myevent3).was_not_called();
|
|
96 end);
|
|
97
|
|
98 it("should support removing multiple handlers at the same time", function ()
|
|
99 local ht = {
|
|
100 myevent1 = spy.new(function () end);
|
|
101 myevent2 = spy.new(function () end);
|
|
102 myevent3 = spy.new(function () end);
|
|
103 };
|
|
104 e.add_handlers(ht);
|
|
105 e.remove_handlers(ht);
|
|
106 e.fire_event("myevent1");
|
|
107 e.fire_event("myevent2");
|
|
108 assert.spy(ht.myevent1).was_not_called();
|
|
109 assert.spy(ht.myevent2).was_not_called();
|
|
110 assert.spy(ht.myevent3).was_not_called();
|
|
111 end);
|
|
112
|
|
113 pending("should support adding handlers within an event handler")
|
|
114 pending("should support removing handlers within an event handler")
|
|
115
|
|
116 describe("wrappers", function ()
|
|
117 local w
|
|
118 before_each(function ()
|
|
119 w = spy.new(function (handlers, event_name, event_data)
|
|
120 assert.is_function(handlers);
|
|
121 assert.equal("myevent", event_name)
|
|
122 assert.equal("abc", event_data);
|
|
123 return handlers(event_name, event_data);
|
|
124 end);
|
|
125 end);
|
|
126
|
|
127 it("should get called", function ()
|
|
128 e.add_wrapper("myevent", w);
|
|
129 e.add_handler("myevent", h);
|
|
130 e.fire_event("myevent", "abc");
|
|
131 assert.spy(w).was_called(1);
|
|
132 assert.spy(h).was_called(1);
|
|
133 end);
|
|
134
|
|
135 it("should be removable", function ()
|
|
136 e.add_wrapper("myevent", w);
|
|
137 e.add_handler("myevent", h);
|
|
138 e.fire_event("myevent", "abc");
|
|
139 e.remove_wrapper("myevent", w);
|
|
140 e.fire_event("myevent", "abc");
|
|
141 assert.spy(w).was_called(1);
|
|
142 assert.spy(h).was_called(2);
|
|
143 end);
|
|
144
|
|
145 it("should allow multiple wrappers", function ()
|
|
146 local w2 = spy.new(function (handlers, event_name, event_data)
|
|
147 return handlers(event_name, event_data);
|
|
148 end);
|
|
149 e.add_wrapper("myevent", w);
|
|
150 e.add_handler("myevent", h);
|
|
151 e.add_wrapper("myevent", w2);
|
|
152 e.fire_event("myevent", "abc");
|
|
153 e.remove_wrapper("myevent", w);
|
|
154 e.fire_event("myevent", "abc");
|
|
155 assert.spy(w).was_called(1);
|
|
156 assert.spy(w2).was_called(2);
|
|
157 assert.spy(h).was_called(2);
|
|
158 end);
|
|
159 end);
|
|
160
|
|
161 describe("global wrappers", function ()
|
|
162 local w
|
|
163 before_each(function ()
|
|
164 w = spy.new(function (handlers, event_name, event_data)
|
|
165 assert.is_function(handlers);
|
|
166 assert.equal("myevent", event_name)
|
|
167 assert.equal("abc", event_data);
|
|
168 return handlers(event_name, event_data);
|
|
169 end);
|
|
170 end);
|
|
171
|
|
172 it("should get called", function ()
|
|
173 e.add_wrapper(false, w);
|
|
174 e.add_handler("myevent", h);
|
|
175 e.fire_event("myevent", "abc");
|
|
176 assert.spy(w).was_called(1);
|
|
177 assert.spy(h).was_called(1);
|
|
178 end);
|
|
179
|
|
180 it("should be removable", function ()
|
|
181 e.add_wrapper(false, w);
|
|
182 e.add_handler("myevent", h);
|
|
183 e.fire_event("myevent", "abc");
|
|
184 e.remove_wrapper(false, w);
|
|
185 e.fire_event("myevent", "abc");
|
|
186 assert.spy(w).was_called(1);
|
|
187 assert.spy(h).was_called(2);
|
|
188 end);
|
|
189 end);
|
|
190 end);
|
|
191 end);
|