Debian Codegraph @

tests/config/lua/LuaEventHandler.cpp

raw symbols SCIP
1 #include <config/lua/LuaEventHandler.hpp>
2
3 #include <event/EventBus.hpp>
4
5 #include <gtest/gtest.h>
6
7 extern "C" {
8 #include <lualib.h>
9 #include <lauxlib.h>
10 }
11
12 using namespace Config::Lua;
13
14 namespace {
15 class CLuaState {
16 public:
17 CLuaState() : m_lua(luaL_newstate()) {
18 luaL_openlibs(m_lua);
19 }
20
21 ~CLuaState() {
22 if (m_lua)
23 lua_close(m_lua);
24 }
25
26 lua_State* get() const {
27 return m_lua;
28 }
29
30 private:
31 lua_State* m_lua = nullptr;
32 };
33
34 int refGlobalFunction(lua_State* L, const char* name) {
35 lua_getglobal(L, name);
36 EXPECT_TRUE(lua_isfunction(L, -1));
37 return luaL_ref(L, LUA_REGISTRYINDEX);
38 }
39
40 int getGlobalInt(lua_State* L, const char* name) {
41 lua_getglobal(L, name);
42 const auto val = lua_tointeger(L, -1);
43 lua_pop(L, 1);
44 return val;
45 }
46
47 const char* getGlobalString(lua_State* L, const char* name) {
48 lua_getglobal(L, name);
49 const auto val = lua_tostring(L, -1);
50 lua_pop(L, 1);
51 return val;
52 }
53 }
54
55 TEST(ConfigLuaEventHandler, knownEventsContainsExpectedEntries) {
56 const auto& known = CLuaEventHandler::knownEvents();
57 EXPECT_TRUE(known.contains("config.reloaded"));
58 EXPECT_TRUE(known.contains("keybinds.submap"));
59 EXPECT_TRUE(known.contains("window.open"));
60 }
61
62 TEST(ConfigLuaEventHandler, registerDispatchAndUnregister) {
63 CLuaState S;
64 const auto L = S.get();
65 CLuaEventHandler handler(L);
66
67 ASSERT_EQ(luaL_dostring(L, R"(
68 count = 0
69 lastSubmap = ""
70 function onSubmap(name)
71 count = count + 1
72 lastSubmap = name
73 end
74 )"),
75 LUA_OK);
76
77 const int ref = refGlobalFunction(L, "onSubmap");
78 const auto handle = handler.registerEvent("keybinds.submap", ref);
79 ASSERT_TRUE(handle.has_value());
80
81 Event::bus()->m_events.keybinds.submap.emit("main");
82 EXPECT_EQ(getGlobalInt(L, "count"), 1);
83 EXPECT_STREQ(getGlobalString(L, "lastSubmap"), "main");
84
85 EXPECT_TRUE(handler.unregisterEvent(*handle));
86
87 Event::bus()->m_events.keybinds.submap.emit("other");
88 EXPECT_EQ(getGlobalInt(L, "count"), 1);
89 }
90
91 TEST(ConfigLuaEventHandler, registerUnknownEventReturnsNullopt) {
92 CLuaState S;
93 const auto L = S.get();
94 CLuaEventHandler handler(L);
95
96 ASSERT_EQ(luaL_dostring(L, "function cb() end"), LUA_OK);
97
98 const int ref = refGlobalFunction(L, "cb");
99 const auto handle = handler.registerEvent("totally.unknown", ref);
100
101 EXPECT_FALSE(handle.has_value());
102 luaL_unref(L, LUA_REGISTRYINDEX, ref);
103 }
104
105 TEST(ConfigLuaEventHandler, callbackLuaErrorDoesNotCrashDispatch) {
106 CLuaState S;
107 const auto L = S.get();
108 CLuaEventHandler handler(L);
109
110 ASSERT_EQ(luaL_dostring(L, "function broken() error('boom') end"), LUA_OK);
111
112 const int ref = refGlobalFunction(L, "broken");
113 const auto h = handler.registerEvent("config.reloaded", ref);
114 ASSERT_TRUE(h.has_value());
115
116 Event::bus()->m_events.config.reloaded.emit();
117 SUCCEED();
118 }
119
120 TEST(ConfigLuaEventHandler, addCustomEventAndDispatchToLua) {
121 CLuaState S;
122 const auto L = S.get();
123 CLuaEventHandler handler(L);
124
125 ASSERT_EQ(luaL_dostring(L, R"(
126 count = 0
127 str = ""
128 function onMyEvent(a, b)
129 count = a - 25
130 str = b
131 end
132 )"),
133 LUA_OK);
134
135 const auto e =
136 makeShared<Event::CEventBus::CCustomEvent>("plugin.my_event", std::vector{Event::CEventBus::CCustomEvent::TYPE_INT, Event::CEventBus::CCustomEvent::TYPE_STRING});
137 EXPECT_TRUE(Event::bus()->addPluginEvent(e));
138 EXPECT_TRUE(CLuaEventHandler::knownEvents().contains("plugin.my_event"));
139
140 const int ref = refGlobalFunction(L, "onMyEvent");
141 const auto handle = handler.registerEvent("plugin.my_event", ref);
142 ASSERT_TRUE(handle.has_value());
143
144 EXPECT_TRUE(e->emit({67, std::string("hello from plugin")}));
145 EXPECT_EQ(getGlobalInt(L, "count"), 42);
146 EXPECT_STREQ(getGlobalString(L, "str"), "hello from plugin");
147 }
148
149 TEST(ConfigLuaEventHandler, removingPluginEventUnregistersLuaEvent) {
150 CLuaState S;
151 const auto L = S.get();
152 CLuaEventHandler handler(L);
153
154 const auto e = makeShared<Event::CEventBus::CCustomEvent>("plugin.to_be_removed", std::vector<Event::CEventBus::CCustomEvent::eType>{});
155 EXPECT_TRUE(Event::bus()->addPluginEvent(e));
156 EXPECT_TRUE(CLuaEventHandler::knownEvents().contains("plugin.to_be_removed"));
157
158 EXPECT_TRUE(Event::bus()->removePluginEvent(e->m_name));
159 EXPECT_FALSE(CLuaEventHandler::knownEvents().contains("plugin.to_be_removed"));
160 }
161
162 TEST(ConfigLuaEventHandler, addCustomEventDuplicateFails) {
163 CLuaState S;
164 const auto L = S.get();
165 CLuaEventHandler handler(L);
166
167 const auto ev = makeShared<Event::CEventBus::CCustomEvent>("plugin.dup", std::vector<Event::CEventBus::CCustomEvent::eType>{});
168
169 EXPECT_TRUE(handler.addCustomEvent(ev));
170 EXPECT_FALSE(handler.addCustomEvent(ev));
171 }
172
173 TEST(ConfigLuaEventHandler, removeNonexistentCustomEventFails) {
174 CLuaState S;
175 const auto L = S.get();
176 CLuaEventHandler handler(L);
177
178 EXPECT_FALSE(handler.removeCustomEvent("plugin.nonexistent"));
179 }