Debian Codegraph @

tests/config/lua/LuaObjectsBasic.cpp

raw symbols SCIP
1 #include <config/lua/LuaEventHandler.hpp>
2
3 #include <config/lua/objects/LuaEventSubscription.hpp>
4 #include <config/lua/objects/LuaKeybind.hpp>
5 #include <config/lua/objects/LuaWorkspaceRule.hpp>
6
7 #include <managers/KeybindManager.hpp>
8 #include <event/EventBus.hpp>
9
10 #include <gtest/gtest.h>
11
12 extern "C" {
13 #include <lualib.h>
14 #include <lauxlib.h>
15 }
16
17 using namespace Config::Lua;
18
19 namespace {
20 class CLuaState {
21 public:
22 CLuaState() : m_lua(luaL_newstate()) {
23 luaL_openlibs(m_lua);
24 }
25
26 ~CLuaState() {
27 if (m_lua)
28 lua_close(m_lua);
29 }
30
31 lua_State* get() const {
32 return m_lua;
33 }
34
35 private:
36 lua_State* m_lua = nullptr;
37 };
38
39 int getGlobalInt(lua_State* L, const char* name) {
40 lua_getglobal(L, name);
41 const int v = sc<int>(lua_tointeger(L, -1));
42 lua_pop(L, 1);
43 return v;
44 }
45
46 bool getGlobalBool(lua_State* L, const char* name) {
47 lua_getglobal(L, name);
48 const bool v = lua_toboolean(L, -1) != 0;
49 lua_pop(L, 1);
50 return v;
51 }
52
53 bool isGlobalNil(lua_State* L, const char* name) {
54 lua_getglobal(L, name);
55 const bool isnil = lua_isnil(L, -1);
56 lua_pop(L, 1);
57 return isnil;
58 }
59 }
60
61 TEST(ConfigLuaObjects, keybindCanToggleEnabledFromLua) {
62 CLuaState S;
63 const auto L = S.get();
64
65 Objects::CLuaKeybind{}.setup(L);
66
67 auto keybind = makeShared<SKeybind>();
68 keybind->enabled = true;
69
70 Objects::CLuaKeybind::push(L, keybind);
71 lua_setglobal(L, "kb");
72
73 ASSERT_EQ(luaL_dostring(L, R"(
74 assert(kb:is_enabled() == true)
75 kb:set_enabled(false)
76 assert(kb:is_enabled() == false)
77 )"),
78 LUA_OK);
79
80 EXPECT_FALSE(keybind->enabled);
81 }
82
83 TEST(ConfigLuaObjects, workspaceRuleCanToggleEnabledFromLua) {
84 CLuaState S;
85 const auto L = S.get();
86
87 Objects::CLuaWorkspaceRule{}.setup(L);
88
89 auto rule = makeShared<Config::CWorkspaceRule>();
90 rule->setEnabled(true);
91
92 Objects::CLuaWorkspaceRule::push(L, rule);
93 lua_setglobal(L, "rule");
94
95 ASSERT_EQ(luaL_dostring(L, R"(
96 assert(rule:is_enabled() == true)
97 rule:set_enabled(false)
98 assert(rule:is_enabled() == false)
99 )"),
100 LUA_OK);
101
102 EXPECT_FALSE(rule->isEnabled());
103 }
104
105 TEST(ConfigLuaObjects, keybindExposesMetadataAndRemoveMethods) {
106 CLuaState S;
107 const auto L = S.get();
108
109 Objects::CLuaKeybind{}.setup(L);
110
111 auto keybind = makeShared<SKeybind>();
112 keybind->enabled = true;
113 keybind->description = "Close active window";
114 keybind->hasDescription = true;
115 keybind->displayKey = "SUPER + Q";
116 keybind->submap.name = "default";
117 keybind->handler = "exec";
118 keybind->arg = "kitty";
119 keybind->modmask = HL_MODIFIER_META;
120 keybind->key = "Q";
121 keybind->keycode = 24;
122 keybind->repeat = true;
123 keybind->locked = true;
124 keybind->release = false;
125 keybind->nonConsuming = true;
126 keybind->transparent = false;
127 keybind->ignoreMods = false;
128 keybind->longPress = false;
129 keybind->dontInhibit = true;
130 keybind->click = false;
131 keybind->drag = false;
132 keybind->submapUniversal = false;
133 keybind->deviceInclusive = true;
134 keybind->devices = {"kbd-a", "kbd-b"};
135 keybind->allowInputCapture = true;
136
137 Objects::CLuaKeybind::push(L, keybind);
138 lua_setglobal(L, "kb");
139
140 ASSERT_EQ(luaL_dostring(L, R"(
141 assert(kb.enabled == true)
142 assert(kb.description == "Close active window")
143 assert(kb.display_key == "SUPER + Q")
144 assert(kb.submap == "default")
145 assert(kb.handler == "exec")
146 assert(kb.arg == "kitty")
147 assert(kb.modmask ~= nil)
148 assert(kb.key == "Q")
149 assert(kb.keycode == 24)
150 assert(kb.repeating == true)
151 assert(kb.locked == true)
152 assert(kb.non_consuming == true)
153 assert(kb.dont_inhibit == true)
154 assert(type(kb.devices) == "table")
155 assert(kb.allow_input_capture == true)
156
157 kb:remove()
158 kb:unbind()
159 )"),
160 LUA_OK);
161 }
162
163 TEST(ConfigLuaObjects, objectsAreReadOnlyFromLua) {
164 CLuaState S;
165 const auto L = S.get();
166
167 Objects::CLuaKeybind{}.setup(L);
168
169 auto keybind = makeShared<SKeybind>();
170 Objects::CLuaKeybind::push(L, keybind);
171 lua_setglobal(L, "kb");
172
173 luaL_dostring(L, "kb.foo = 1");
174 luaL_dostring(L, "x = kb.foo");
175 EXPECT_TRUE(isGlobalNil(L, "x"));
176 }
177
178 TEST(ConfigLuaObjects, keybindSupportsEqAndToString) {
179 CLuaState S;
180 const auto L = S.get();
181
182 Objects::CLuaKeybind{}.setup(L);
183
184 auto keybindA = makeShared<SKeybind>();
185 auto keybindB = makeShared<SKeybind>();
186
187 Objects::CLuaKeybind::push(L, keybindA);
188 lua_setglobal(L, "kb1");
189
190 Objects::CLuaKeybind::push(L, keybindA);
191 lua_setglobal(L, "kb2");
192
193 Objects::CLuaKeybind::push(L, keybindB);
194 lua_setglobal(L, "kb3");
195
196 ASSERT_EQ(luaL_dostring(L, R"(
197 eq12 = (kb1 == kb2)
198 neq13 = (kb1 ~= kb3)
199
200 local s = tostring(kb1)
201 hasPrefix = type(s) == "string" and string.sub(s, 1, 11) == "HL.Keybind("
202 )"),
203 LUA_OK);
204
205 EXPECT_TRUE(getGlobalBool(L, "eq12"));
206 EXPECT_TRUE(getGlobalBool(L, "neq13"));
207 EXPECT_TRUE(getGlobalBool(L, "hasPrefix"));
208 }
209
210 TEST(ConfigLuaObjects, eventSubscriptionRemoveAndIsActive) {
211 CLuaState S;
212 const auto L = S.get();
213
214 Objects::CLuaEventSubscription{}.setup(L);
215 CLuaEventHandler handler(L);
216
217 ASSERT_EQ(luaL_dostring(L, R"(
218 count = 0
219 function onReload()
220 count = count + 1
221 end
222 )"),
223 LUA_OK);
224
225 lua_getglobal(L, "onReload");
226 ASSERT_TRUE(lua_isfunction(L, -1));
227 const int ref = luaL_ref(L, LUA_REGISTRYINDEX);
228
229 const auto handle = handler.registerEvent("config.reloaded", ref);
230 ASSERT_TRUE(handle.has_value());
231
232 Objects::CLuaEventSubscription::push(L, &handler, *handle);
233 lua_setglobal(L, "sub");
234
235 Event::bus()->m_events.config.reloaded.emit();
236 EXPECT_EQ(getGlobalInt(L, "count"), 1);
237
238 ASSERT_EQ(luaL_dostring(L, R"(
239 assert(sub:is_active() == true)
240 sub:remove()
241 assert(sub:is_active() == false)
242 sub:remove()
243 )"),
244 LUA_OK);
245
246 Event::bus()->m_events.config.reloaded.emit();
247 EXPECT_EQ(getGlobalInt(L, "count"), 1);
248 }
249
250 TEST(ConfigLuaObjects, eventSubscriptionSupportsEqAndToString) {
251 CLuaState S;
252 const auto L = S.get();
253
254 Objects::CLuaEventSubscription{}.setup(L);
255 CLuaEventHandler handler(L);
256
257 ASSERT_EQ(luaL_dostring(L, R"(
258 function cbA() end
259 function cbB() end
260 )"),
261 LUA_OK);
262
263 lua_getglobal(L, "cbA");
264 ASSERT_TRUE(lua_isfunction(L, -1));
265 const int refA = luaL_ref(L, LUA_REGISTRYINDEX);
266
267 lua_getglobal(L, "cbB");
268 ASSERT_TRUE(lua_isfunction(L, -1));
269 const int refB = luaL_ref(L, LUA_REGISTRYINDEX);
270
271 const auto handleA = handler.registerEvent("config.reloaded", refA);
272 const auto handleB = handler.registerEvent("config.reloaded", refB);
273 ASSERT_TRUE(handleA.has_value());
274 ASSERT_TRUE(handleB.has_value());
275
276 Objects::CLuaEventSubscription::push(L, &handler, *handleA);
277 lua_setglobal(L, "sub1");
278
279 Objects::CLuaEventSubscription::push(L, &handler, *handleA);
280 lua_setglobal(L, "sub2");
281
282 Objects::CLuaEventSubscription::push(L, &handler, *handleB);
283 lua_setglobal(L, "sub3");
284
285 ASSERT_EQ(luaL_dostring(L, R"(
286 eq12 = (sub1 == sub2)
287 neq13 = (sub1 ~= sub3)
288
289 local s = tostring(sub1)
290 hasPrefix = type(s) == "string" and string.sub(s, 1, 21) == "HL.EventSubscription("
291 )"),
292 LUA_OK);
293
294 EXPECT_TRUE(getGlobalBool(L, "eq12"));
295 EXPECT_TRUE(getGlobalBool(L, "neq13"));
296 EXPECT_TRUE(getGlobalBool(L, "hasPrefix"));
297 }