Debian Codegraph @

tests/config/lua/ConfigValueTypes.cpp

raw symbols SCIP
1 #include <config/lua/types/LuaConfigBool.hpp>
2 #include <config/lua/types/LuaConfigColor.hpp>
3 #include <config/lua/types/LuaConfigCssGap.hpp>
4 #include <config/lua/types/LuaConfigExpressionVec2.hpp>
5 #include <config/lua/types/LuaConfigFloat.hpp>
6 #include <config/lua/types/LuaConfigFontWeight.hpp>
7 #include <config/lua/types/LuaConfigGradient.hpp>
8 #include <config/lua/types/LuaConfigInt.hpp>
9 #include <config/lua/types/LuaConfigString.hpp>
10 #include <config/lua/types/LuaConfigVec2.hpp>
11
12 #include <macros.hpp>
13
14 #include <gtest/gtest.h>
15
16 #include <numbers>
17
18 extern "C" {
19 #include <lualib.h>
20 #include <lauxlib.h>
21 }
22
23 using namespace Config;
24 using namespace Config::Lua;
25
26 namespace {
27 class CLuaState {
28 public:
29 CLuaState() : m_lua(luaL_newstate()) {
30 luaL_openlibs(m_lua);
31 }
32
33 ~CLuaState() {
34 if (m_lua)
35 lua_close(m_lua);
36 }
37
38 lua_State* get() const {
39 return m_lua;
40 }
41
42 private:
43 lua_State* m_lua = nullptr;
44 };
45
46 void pushVec2Table(lua_State* L, double x, double y) {
47 lua_createtable(L, 2, 2);
48 lua_pushnumber(L, x);
49 lua_rawseti(L, -2, 1);
50 lua_pushnumber(L, y);
51 lua_rawseti(L, -2, 2);
52 }
53
54 void pushExpressionVec2Table(lua_State* L, const char* x, const char* y) {
55 lua_createtable(L, 2, 2);
56 lua_pushstring(L, x);
57 lua_rawseti(L, -2, 1);
58 lua_pushstring(L, y);
59 lua_rawseti(L, -2, 2);
60 }
61 }
62
63 TEST(ConfigLuaValueTypes, boolParseAndReset) {
64 CLuaState S;
65 const auto L = S.get();
66
67 CLuaConfigBool value(false);
68 EXPECT_FALSE(value.parsed());
69 EXPECT_FALSE(value.setByUser());
70
71 lua_pushboolean(L, true);
72 const auto err = value.parse(L);
73 lua_pop(L, 1);
74
75 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
76 EXPECT_TRUE(value.parsed());
77 EXPECT_TRUE(value.setByUser());
78
79 value.reset();
80 EXPECT_FALSE(value.parsed());
81
82 value.resetSetByUser();
83 EXPECT_FALSE(value.setByUser());
84 }
85
86 TEST(ConfigLuaValueTypes, boolBadType) {
87 CLuaState S;
88 const auto L = S.get();
89
90 CLuaConfigBool value(false);
91
92 lua_pushinteger(L, 2);
93 const auto err = value.parse(L);
94 lua_pop(L, 1);
95
96 EXPECT_EQ(err.errorCode, PARSE_ERROR_BAD_TYPE);
97 EXPECT_NE(err.message.find("requires a bool"), std::string::npos);
98 }
99
100 TEST(ConfigLuaValueTypes, intBooleanCastAndRangeAndMap) {
101 CLuaState S;
102 const auto L = S.get();
103
104 CLuaConfigInt ranged(0, 0, 10);
105
106 lua_pushboolean(L, true);
107 auto err = ranged.parse(L);
108 lua_pop(L, 1);
109 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
110 EXPECT_EQ(ranged.parsed(), 1);
111
112 lua_pushinteger(L, 11);
113 err = ranged.parse(L);
114 lua_pop(L, 1);
115 EXPECT_EQ(err.errorCode, PARSE_ERROR_OUT_OF_RANGE);
116
117 CLuaConfigInt mapped(0, std::nullopt, std::nullopt, std::unordered_map<std::string, INTEGER>{{"auto", -1}, {"on", 1}});
118
119 lua_pushstring(L, "auto");
120 err = mapped.parse(L);
121 lua_pop(L, 1);
122 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
123 EXPECT_EQ(mapped.parsed(), -1);
124
125 lua_pushstring(L, "missing");
126 err = mapped.parse(L);
127 lua_pop(L, 1);
128 EXPECT_EQ(err.errorCode, PARSE_ERROR_BAD_VALUE);
129 EXPECT_NE(err.message.find("unknown string value"), std::string::npos);
130 }
131
132 TEST(ConfigLuaValueTypes, floatParseAndRange) {
133 CLuaState S;
134 const auto L = S.get();
135
136 CLuaConfigFloat value(0.F, -1.F, 1.F);
137
138 lua_pushnumber(L, 0.5);
139 auto err = value.parse(L);
140 lua_pop(L, 1);
141 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
142 EXPECT_FLOAT_EQ(value.parsed(), 0.5F);
143
144 lua_pushinteger(L, 2);
145 err = value.parse(L);
146 lua_pop(L, 1);
147 EXPECT_EQ(err.errorCode, PARSE_ERROR_OUT_OF_RANGE);
148 }
149
150 TEST(ConfigLuaValueTypes, stringValidatorAndEmptyPush) {
151 CLuaState S;
152 const auto L = S.get();
153
154 CLuaConfigString validated("default", std::function<std::expected<void, std::string>(const STRING&)>{[](const STRING& s) -> std::expected<void, std::string> {
155 if (s.starts_with("ok:"))
156 return {};
157 return std::unexpected("must start with ok:");
158 }});
159
160 lua_pushstring(L, "ok:value");
161 auto err = validated.parse(L);
162 lua_pop(L, 1);
163 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
164 EXPECT_EQ(validated.parsed(), "ok:value");
165
166 lua_pushstring(L, "bad");
167 err = validated.parse(L);
168 lua_pop(L, 1);
169 EXPECT_EQ(err.errorCode, PARSE_ERROR_BAD_VALUE);
170 EXPECT_NE(err.message.find("must start with ok:"), std::string::npos);
171
172 CLuaConfigString emptyDefault(STRVAL_EMPTY);
173 emptyDefault.push(L);
174 ASSERT_TRUE(lua_isstring(L, -1));
175 EXPECT_STREQ(lua_tostring(L, -1), "");
176 lua_pop(L, 1);
177 }
178
179 TEST(ConfigLuaValueTypes, colorParseAndPush) {
180 CLuaState S;
181 const auto L = S.get();
182
183 CLuaConfigColor value(0);
184
185 lua_pushstring(L, "0xFF00FF00");
186 auto err = value.parse(L);
187 lua_pop(L, 1);
188 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
189 EXPECT_EQ(value.parsed(), 0xFF00FF00);
190
191 value.push(L);
192 ASSERT_TRUE(lua_isstring(L, -1));
193 EXPECT_STREQ(lua_tostring(L, -1), "0xFF00FF00");
194 lua_pop(L, 1);
195
196 lua_pushstring(L, "@@@notacolor@@@");
197 err = value.parse(L);
198 lua_pop(L, 1);
199 EXPECT_EQ(err.errorCode, PARSE_ERROR_BAD_VALUE);
200 }
201
202 TEST(ConfigLuaValueTypes, vec2ParseValidateAndPush) {
203 CLuaState S;
204 const auto L = S.get();
205
206 CLuaConfigVec2 value({0, 0}, std::function<std::expected<void, std::string>(const VEC2&)>{[](const VEC2& v) -> std::expected<void, std::string> {
207 if (v.x < 0 || v.y < 0)
208 return std::unexpected("must be non-negative");
209 return {};
210 }});
211
212 pushVec2Table(L, 10, 20);
213 auto err = value.parse(L);
214 lua_pop(L, 1);
215 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
216 EXPECT_FLOAT_EQ(value.parsed().x, 10.F);
217 EXPECT_FLOAT_EQ(value.parsed().y, 20.F);
218
219 value.push(L);
220 ASSERT_TRUE(lua_istable(L, -1));
221 lua_rawgeti(L, -1, 1);
222 EXPECT_DOUBLE_EQ(lua_tonumber(L, -1), 10);
223 lua_pop(L, 1);
224 lua_getfield(L, -1, "y");
225 EXPECT_DOUBLE_EQ(lua_tonumber(L, -1), 20);
226 lua_pop(L, 2);
227
228 pushVec2Table(L, -1, 0);
229 err = value.parse(L);
230 lua_pop(L, 1);
231 EXPECT_EQ(err.errorCode, PARSE_ERROR_BAD_VALUE);
232 EXPECT_NE(err.message.find("must be non-negative"), std::string::npos);
233
234 lua_createtable(L, 3, 0);
235 lua_pushnumber(L, 1);
236 lua_rawseti(L, -2, 1);
237 lua_pushnumber(L, 2);
238 lua_rawseti(L, -2, 2);
239 lua_pushnumber(L, 3);
240 lua_rawseti(L, -2, 3);
241 err = value.parse(L);
242 lua_pop(L, 1);
243 EXPECT_EQ(err.errorCode, PARSE_ERROR_BAD_VALUE);
244 }
245
246 TEST(ConfigLuaValueTypes, expressionVec2AcceptsStringNumberAndTableForms) {
247 CLuaState S;
248 const auto L = S.get();
249
250 CLuaConfigExpressionVec2 value;
251
252 lua_pushstring(L, "monitor_w*0.5 monitor_h*0.25");
253 auto err = value.parse(L);
254 lua_pop(L, 1);
255 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
256 EXPECT_EQ(value.parsed().x, "monitor_w*0.5");
257 EXPECT_EQ(value.parsed().y, "monitor_h*0.25");
258
259 pushVec2Table(L, 150, 200);
260 err = value.parse(L);
261 lua_pop(L, 1);
262 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
263 EXPECT_EQ(value.parsed().x, "150");
264 EXPECT_EQ(value.parsed().y, "200");
265
266 pushExpressionVec2Table(L, "monitor_h * 0.5", "monitor_h * 0.25");
267 err = value.parse(L);
268 lua_pop(L, 1);
269 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
270 EXPECT_EQ(value.parsed().x, "monitor_h * 0.5");
271 EXPECT_EQ(value.parsed().y, "monitor_h * 0.25");
272
273 value.push(L);
274 ASSERT_TRUE(lua_istable(L, -1));
275 lua_rawgeti(L, -1, 1);
276 EXPECT_STREQ(lua_tostring(L, -1), "monitor_h * 0.5");
277 lua_pop(L, 1);
278 lua_getfield(L, -1, "y");
279 EXPECT_STREQ(lua_tostring(L, -1), "monitor_h * 0.25");
280 lua_pop(L, 2);
281
282 lua_createtable(L, 1, 0);
283 lua_pushnumber(L, 1);
284 lua_rawseti(L, -2, 1);
285 err = value.parse(L);
286 lua_pop(L, 1);
287 EXPECT_EQ(err.errorCode, PARSE_ERROR_BAD_VALUE);
288 }
289
290 TEST(ConfigLuaValueTypes, cssGapParseFormsAndRange) {
291 CLuaState S;
292 const auto L = S.get();
293
294 CLuaConfigCssGap value(1, 0, 10);
295
296 lua_pushinteger(L, 7);
297 auto err = value.parse(L);
298 lua_pop(L, 1);
299 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
300 EXPECT_EQ(value.parsed().m_top, 7);
301 EXPECT_EQ(value.parsed().m_right, 7);
302 EXPECT_EQ(value.parsed().m_bottom, 7);
303 EXPECT_EQ(value.parsed().m_left, 7);
304
305 lua_createtable(L, 0, 2);
306 lua_pushinteger(L, 3);
307 lua_setfield(L, -2, "top");
308 lua_pushinteger(L, 4);
309 lua_setfield(L, -2, "left");
310 err = value.parse(L);
311 lua_pop(L, 1);
312 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
313 EXPECT_EQ(value.parsed().m_top, 3);
314 EXPECT_EQ(value.parsed().m_right, 0);
315 EXPECT_EQ(value.parsed().m_bottom, 0);
316 EXPECT_EQ(value.parsed().m_left, 4);
317
318 lua_createtable(L, 0, 1);
319 lua_pushinteger(L, 100);
320 lua_setfield(L, -2, "top");
321 err = value.parse(L);
322 lua_pop(L, 1);
323 EXPECT_EQ(err.errorCode, PARSE_ERROR_OUT_OF_RANGE);
324
325 lua_createtable(L, 0, 1);
326 lua_pushstring(L, "bad");
327 lua_setfield(L, -2, "top");
328 err = value.parse(L);
329 lua_pop(L, 1);
330 EXPECT_EQ(err.errorCode, PARSE_ERROR_BAD_TYPE);
331 }
332
333 TEST(ConfigLuaValueTypes, fontWeightParse) {
334 CLuaState S;
335 const auto L = S.get();
336
337 CLuaConfigFontWeight value(400);
338
339 lua_pushstring(L, "bold");
340 auto err = value.parse(L);
341 lua_pop(L, 1);
342 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
343 EXPECT_EQ(value.parsed().m_value, 700);
344
345 lua_pushinteger(L, -1);
346 err = value.parse(L);
347 lua_pop(L, 1);
348 EXPECT_EQ(err.errorCode, PARSE_ERROR_OUT_OF_RANGE);
349
350 lua_pushstring(L, "definitely-not-a-weight");
351 err = value.parse(L);
352 lua_pop(L, 1);
353 EXPECT_EQ(err.errorCode, PARSE_ERROR_BAD_VALUE);
354 }
355
356 TEST(ConfigLuaValueTypes, gradientParseAndPush) {
357 CLuaState S;
358 const auto L = S.get();
359
360 CLuaConfigGradient value(CHyprColor(0xFF000000));
361
362 lua_pushstring(L, "0xFF112233");
363 auto err = value.parse(L);
364 lua_pop(L, 1);
365 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
366 ASSERT_EQ(value.parsed().m_colors.size(), 1);
367
368 lua_createtable(L, 0, 2);
369 lua_createtable(L, 2, 0);
370 lua_pushstring(L, "0xFF0000FF");
371 lua_rawseti(L, -2, 1);
372 lua_pushstring(L, "0xFFFF0000");
373 lua_rawseti(L, -2, 2);
374 lua_setfield(L, -2, "colors");
375 lua_pushnumber(L, 90);
376 lua_setfield(L, -2, "angle");
377
378 err = value.parse(L);
379 lua_pop(L, 1);
380 EXPECT_EQ(err.errorCode, PARSE_ERROR_OK);
381 ASSERT_EQ(value.parsed().m_colors.size(), 2);
382 EXPECT_NEAR(value.parsed().m_angle, std::numbers::pi_v<float> / 2.F, 0.0001F);
383
384 value.push(L);
385 ASSERT_TRUE(lua_istable(L, -1));
386 lua_getfield(L, -1, "colors");
387 ASSERT_TRUE(lua_istable(L, -1));
388 lua_rawgeti(L, -1, 1);
389 EXPECT_STREQ(lua_tostring(L, -1), "0xFF0000FF");
390 lua_pop(L, 1);
391 lua_pop(L, 1);
392 lua_getfield(L, -1, "angle");
393 EXPECT_NEAR(lua_tonumber(L, -1), 90.0, 0.001);
394 lua_pop(L, 2);
395
396 lua_createtable(L, 0, 0);
397 err = value.parse(L);
398 lua_pop(L, 1);
399 EXPECT_EQ(err.errorCode, PARSE_ERROR_BAD_VALUE);
400 }