Annotate

spec/util_events_spec.lua @ 10067:598befab492e

mod_admin_telnet: Check for simple commands before executing in sandbox This makes fixing yield over pcall boundry issue easier since it would have jumped to the thread error handler instead of proceeding to checking for simple commands.
author Kim Alvefur <zash@zash.se>
date Fri, 10 May 2019 01:28:09 +0200
parent 8802:befffddf1b25
child 11058:c99afee1c548
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8760
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local events = require "util.events";
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 describe("util.events", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 it("should export a new() function", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 assert.is_function(events.new);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 describe("new()", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 it("should return return a new events object", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local e = events.new();
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 assert.is_function(e.add_handler);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 assert.is_function(e.remove_handler);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local e, h;
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 describe("API", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 before_each(function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 e = events.new();
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 h = spy.new(function () end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 it("should call handlers when an event is fired", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 e.add_handler("myevent", h);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 e.fire_event("myevent");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 assert.spy(h).was_called();
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 it("should not call handlers when a different event is fired", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 e.add_handler("myevent", h);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 e.fire_event("notmyevent");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 assert.spy(h).was_not_called();
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 it("should pass the data argument to handlers", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 e.add_handler("myevent", h);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 e.fire_event("myevent", "mydata");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 assert.spy(h).was_called_with("mydata");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 it("should support non-string events", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local myevent = {};
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 e.add_handler(myevent, h);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 e.fire_event(myevent, "mydata");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 assert.spy(h).was_called_with("mydata");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 it("should call handlers in priority order", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local data = {};
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 e.add_handler("myevent", function () table.insert(data, "h1"); end, 5);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 e.add_handler("myevent", function () table.insert(data, "h2"); end, 3);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 e.add_handler("myevent", function () table.insert(data, "h3"); end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 e.fire_event("myevent", "mydata");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 assert.same(data, { "h1", "h2", "h3" });
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 it("should support non-integer priority values", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local data = {};
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 e.add_handler("myevent", function () table.insert(data, "h1"); end, 1);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 e.add_handler("myevent", function () table.insert(data, "h2"); end, 0.5);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 e.add_handler("myevent", function () table.insert(data, "h3"); end, 0.25);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 e.fire_event("myevent", "mydata");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 assert.same(data, { "h1", "h2", "h3" });
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 it("should support negative priority values", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 local data = {};
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 e.add_handler("myevent", function () table.insert(data, "h1"); end, 1);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 e.add_handler("myevent", function () table.insert(data, "h2"); end, 0);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 e.add_handler("myevent", function () table.insert(data, "h3"); end, -1);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 e.fire_event("myevent", "mydata");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 assert.same(data, { "h1", "h2", "h3" });
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 it("should support removing handlers", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 e.add_handler("myevent", h);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 e.fire_event("myevent");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 e.remove_handler("myevent", h);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 e.fire_event("myevent");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 assert.spy(h).was_called(1);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 it("should support adding multiple handlers at the same time", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 local ht = {
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 myevent1 = spy.new(function () end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 myevent2 = spy.new(function () end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 myevent3 = spy.new(function () end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 };
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 e.add_handlers(ht);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 e.fire_event("myevent1");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 e.fire_event("myevent2");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 assert.spy(ht.myevent1).was_called();
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 assert.spy(ht.myevent2).was_called();
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 assert.spy(ht.myevent3).was_not_called();
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 it("should support removing multiple handlers at the same time", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 local ht = {
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 myevent1 = spy.new(function () end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 myevent2 = spy.new(function () end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 myevent3 = spy.new(function () end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 };
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 e.add_handlers(ht);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 e.remove_handlers(ht);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 e.fire_event("myevent1");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 e.fire_event("myevent2");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 assert.spy(ht.myevent1).was_not_called();
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 assert.spy(ht.myevent2).was_not_called();
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 assert.spy(ht.myevent3).was_not_called();
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 pending("should support adding handlers within an event handler")
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 pending("should support removing handlers within an event handler")
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
8761
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
116 it("should support getting the current handlers for an event", function ()
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
117 e.add_handler("myevent", h);
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
118 local handlers = e.get_handlers("myevent");
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
119 assert.equal(h, handlers[1]);
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
120 end);
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
121
8760
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 describe("wrappers", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 local w
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 before_each(function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 w = spy.new(function (handlers, event_name, event_data)
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 assert.is_function(handlers);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 assert.equal("myevent", event_name)
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 assert.equal("abc", event_data);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 return handlers(event_name, event_data);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 it("should get called", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 e.add_wrapper("myevent", w);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 e.add_handler("myevent", h);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 e.fire_event("myevent", "abc");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 assert.spy(w).was_called(1);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 assert.spy(h).was_called(1);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 it("should be removable", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 e.add_wrapper("myevent", w);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 e.add_handler("myevent", h);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 e.fire_event("myevent", "abc");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 e.remove_wrapper("myevent", w);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 e.fire_event("myevent", "abc");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 assert.spy(w).was_called(1);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 assert.spy(h).was_called(2);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 it("should allow multiple wrappers", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 local w2 = spy.new(function (handlers, event_name, event_data)
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 return handlers(event_name, event_data);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 e.add_wrapper("myevent", w);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 e.add_handler("myevent", h);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 e.add_wrapper("myevent", w2);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 e.fire_event("myevent", "abc");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 e.remove_wrapper("myevent", w);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 e.fire_event("myevent", "abc");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 assert.spy(w).was_called(1);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 assert.spy(w2).was_called(2);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 assert.spy(h).was_called(2);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 end);
8761
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
165
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
166 it("should support a mix of global and event wrappers", function ()
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
167 local w2 = spy.new(function (handlers, event_name, event_data)
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
168 return handlers(event_name, event_data);
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
169 end);
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
170 e.add_wrapper(false, w);
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
171 e.add_handler("myevent", h);
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
172 e.add_wrapper("myevent", w2);
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
173 e.fire_event("myevent", "abc");
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
174 e.remove_wrapper(false, w);
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
175 e.fire_event("myevent", "abc");
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
176 assert.spy(w).was_called(1);
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
177 assert.spy(w2).was_called(2);
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
178 assert.spy(h).was_called(2);
b6e193e33145 util.events: Add more tests (100% line coverage)
Matthew Wild <mwild1@gmail.com>
parents: 8760
diff changeset
179 end);
8760
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 describe("global wrappers", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 local w
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 before_each(function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 w = spy.new(function (handlers, event_name, event_data)
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 assert.is_function(handlers);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 assert.equal("myevent", event_name)
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 assert.equal("abc", event_data);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 return handlers(event_name, event_data);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 it("should get called", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 e.add_wrapper(false, w);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 e.add_handler("myevent", h);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 e.fire_event("myevent", "abc");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 assert.spy(w).was_called(1);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 assert.spy(h).was_called(1);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 it("should be removable", function ()
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 e.add_wrapper(false, w);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 e.add_handler("myevent", h);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 e.fire_event("myevent", "abc");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 e.remove_wrapper(false, w);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 e.fire_event("myevent", "abc");
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 assert.spy(w).was_called(1);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 assert.spy(h).was_called(2);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 end);
6f3c3bb768cb util.events: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 end);