From 8107fc451f18f7d8108e59b57df319d96e2b7339 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Mon, 22 Dec 2025 17:24:29 +0100 Subject: [PATCH 1/2] Remove dead code --- apps/components_tests/lua/testuicontent.cpp | 94 +++------------------ components/lua_ui/content.hpp | 68 --------------- 2 files changed, 14 insertions(+), 148 deletions(-) diff --git a/apps/components_tests/lua/testuicontent.cpp b/apps/components_tests/lua/testuicontent.cpp index fcdfd8a1b3..ddc5e82294 100644 --- a/apps/components_tests/lua/testuicontent.cpp +++ b/apps/components_tests/lua/testuicontent.cpp @@ -28,20 +28,12 @@ namespace } sol::table makeTable() { return sol::table(mLuaState.unsafeState(), sol::create); } - - sol::table makeTable(std::string name) - { - auto result = makeTable(); - result["name"] = name; - return result; - } }; TEST_F(LuaUiContentTest, ProtectedMetatable) { sol::state_view sol = mLuaState.unsafeState(); sol["makeContent"] = mNew; - sol["M"] = makeContent(makeTable()).getMetatable(); std::string testScript = R"( assert(not pcall(function() setmetatable(makeContent{}, {}) end), 'Metatable is not protected') assert(getmetatable(makeContent{}) == false, 'Metatable is not protected') @@ -59,31 +51,6 @@ namespace EXPECT_EQ(content.size(), 3); } - TEST_F(LuaUiContentTest, Insert) - { - auto table = makeTable(); - table.add(makeTable()); - table.add(makeTable()); - table.add(makeTable()); - LuaUi::ContentView content = makeContent(table); - content.insert(2, makeTable("inserted")); - EXPECT_EQ(content.size(), 4); - auto inserted = content.at("inserted"); - auto index = content.indexOf(inserted); - EXPECT_TRUE(index.has_value()); - EXPECT_EQ(index.value(), 2); - } - - TEST_F(LuaUiContentTest, MakeHole) - { - auto table = makeTable(); - table.add(makeTable()); - table.add(makeTable()); - LuaUi::ContentView content = makeContent(table); - sol::table t = makeTable(); - EXPECT_ANY_THROW(content.assign(3, t)); - } - TEST_F(LuaUiContentTest, WrongType) { auto table = makeTable(); @@ -93,54 +60,21 @@ namespace EXPECT_ANY_THROW(makeContent(table)); } - TEST_F(LuaUiContentTest, NameAccess) - { - auto table = makeTable(); - table.add(makeTable()); - table.add(makeTable("a")); - LuaUi::ContentView content = makeContent(table); - EXPECT_NO_THROW(content.at("a")); - content.remove("a"); - EXPECT_EQ(content.size(), 1); - content.assign(content.size(), makeTable("b")); - content.assign("b", makeTable()); - EXPECT_ANY_THROW(content.at("b")); - EXPECT_EQ(content.size(), 2); - content.assign(content.size(), makeTable("c")); - content.assign(content.size(), makeTable("c")); - content.remove("c"); - EXPECT_ANY_THROW(content.at("c")); - } - - TEST_F(LuaUiContentTest, IndexOf) - { - auto table = makeTable(); - table.add(makeTable()); - table.add(makeTable()); - table.add(makeTable()); - LuaUi::ContentView content = makeContent(table); - auto child = makeTable(); - content.assign(2, child); - EXPECT_EQ(content.indexOf(child).value(), 2); - EXPECT_TRUE(!content.indexOf(makeTable()).has_value()); - } - TEST_F(LuaUiContentTest, BoundsChecks) { - auto table = makeTable(); - LuaUi::ContentView content = makeContent(table); - EXPECT_ANY_THROW(content.at(0)); - EXPECT_EQ(content.size(), 0); - content.assign(content.size(), makeTable()); - EXPECT_EQ(content.size(), 1); - content.assign(content.size(), makeTable()); - EXPECT_EQ(content.size(), 2); - content.assign(content.size(), makeTable()); - EXPECT_EQ(content.size(), 3); - EXPECT_ANY_THROW(content.at(3)); - EXPECT_ANY_THROW(content.remove(3)); - content.remove(2); - EXPECT_EQ(content.size(), 2); - EXPECT_ANY_THROW(content.at(2)); + { + auto table = makeTable(); + LuaUi::ContentView content = makeContent(table); + EXPECT_ANY_THROW(content.at(0)); + EXPECT_EQ(content.size(), 0); + } + { + auto table = makeTable(); + table[1] = makeTable(); + LuaUi::ContentView content = makeContent(table); + EXPECT_EQ(content.size(), 1); + EXPECT_ANY_THROW(content.at(1)); + content.at(0); + } } } diff --git a/components/lua_ui/content.hpp b/components/lua_ui/content.hpp index aff38fca57..e80bc2923f 100644 --- a/components/lua_ui/content.hpp +++ b/components/lua_ui/content.hpp @@ -1,9 +1,6 @@ #ifndef COMPONENTS_LUAUI_CONTENT #define COMPONENTS_LUAUI_CONTENT -#include -#include - #include #include @@ -27,22 +24,6 @@ namespace LuaUi size_t size() const { return mTable.size(); } - void assign(std::string_view name, const sol::table& table) - { - if (indexOf(name).has_value()) - mTable[name] = table; - else - throw std::domain_error("Invalid Content key"); - } - void assign(size_t index, const sol::table& table) - { - if (index <= size()) - mTable[toLua(index)] = table; - else - throw std::range_error("Invalid Content index"); - } - void insert(size_t index, const sol::table& table) { callMethod("insert", toLua(index), table); } - sol::object at(size_t index) const { if (index < size()) @@ -50,60 +31,11 @@ namespace LuaUi else throw std::range_error("Invalid Content index"); } - sol::object at(std::string_view name) const - { - if (indexOf(name).has_value()) - return mTable.get(name); - else - throw std::range_error("Invalid Content key"); - } - void remove(size_t index) - { - if (index < size()) - // for some reason mTable[key] = value doesn't call __newindex - getMetatable()[sol::meta_function::new_index].get()( - mTable, toLua(index), sol::nil); - else - throw std::range_error("Invalid Content index"); - } - void remove(std::string_view name) - { - auto index = indexOf(name); - if (index.has_value()) - remove(index.value()); - else - throw std::domain_error("Invalid Content key"); - } - std::optional indexOf(std::string_view name) const - { - sol::object result = callMethod("indexOf", name); - if (result.is()) - return fromLua(LuaUtil::cast(result)); - else - return std::nullopt; - } - std::optional indexOf(const sol::table& table) const - { - sol::object result = callMethod("indexOf", table); - if (result.is()) - return fromLua(LuaUtil::cast(result)); - else - return std::nullopt; - } - - sol::table getMetatable() const { return mTable[sol::metatable_key].get(); } private: sol::main_table mTable; - template - sol::object callMethod(std::string_view name, Arg&&... arg) const - { - return mTable.get(name)(mTable, arg...); - } - static inline size_t toLua(size_t index) { return index + 1; } - static inline size_t fromLua(size_t index) { return index - 1; } }; } From 71b394b88d0c870774e85b134c8ef1c1816b07a3 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Tue, 23 Dec 2025 17:47:29 +0100 Subject: [PATCH 2/2] Reimplement tests in Lua --- apps/components_tests/lua/testuicontent.cpp | 65 +++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/apps/components_tests/lua/testuicontent.cpp b/apps/components_tests/lua/testuicontent.cpp index ddc5e82294..47d230fdb2 100644 --- a/apps/components_tests/lua/testuicontent.cpp +++ b/apps/components_tests/lua/testuicontent.cpp @@ -41,6 +41,35 @@ namespace EXPECT_NO_THROW(sol.safe_script(testScript)); } + TEST_F(LuaUiContentTest, Insert) + { + mLuaState.protectedCall([&](LuaUtil::LuaView& state) { + sol::state_view& sol = state.sol(); + sol["makeContent"] = mNew; + EXPECT_NO_THROW(sol.safe_script(R"( + local content = makeContent({ {}, {}, {} }) + content:insert(2, { name = 'inserted' }) + assert(#content == 4, 'Not inserted') + local inserted = content:indexOf('inserted') + local index = content:indexOf(content[inserted]) + assert(index ~= nil, 'Failed to find inserted') + assert(index == 2, 'Inserted at the wrong index') + )")); + }); + } + + TEST_F(LuaUiContentTest, MakeHole) + { + mLuaState.protectedCall([&](LuaUtil::LuaView& state) { + sol::state_view& sol = state.sol(); + sol["makeContent"] = mNew; + EXPECT_NO_THROW(sol.safe_script(R"( + local content = makeContent({ {}, {} }) + assert(not pcall(function() content[4] = {} end), 'Allowed to make hole') + )")); + }); + } + TEST_F(LuaUiContentTest, Create) { auto table = makeTable(); @@ -60,6 +89,42 @@ namespace EXPECT_ANY_THROW(makeContent(table)); } + TEST_F(LuaUiContentTest, NameAccess) + { + mLuaState.protectedCall([&](LuaUtil::LuaView& state) { + sol::state_view& sol = state.sol(); + sol["makeContent"] = mNew; + EXPECT_NO_THROW(sol.safe_script(R"( + local content = makeContent({ {}, { name = 'a' } }) + assert(content:indexOf('a') ~= nil, 'Could not find named table') + content['a'] = nil + assert(#content == 1, 'Failed to remove') + content:add({ name = 'b' }) + content['b'] = {} + assert(#content == 2, 'Failed to insert') + content:add({ name = 'c' }) + content:add({ name = 'c' }) + content['c'] = nil + assert(content:indexOf('c') == nil, 'Failed to remove value inserted twice'..#content) + )")); + }); + } + + TEST_F(LuaUiContentTest, IndexOf) + { + mLuaState.protectedCall([&](LuaUtil::LuaView& state) { + sol::state_view& sol = state.sol(); + sol["makeContent"] = mNew; + EXPECT_NO_THROW(sol.safe_script(R"( + local content = makeContent({ {}, {}, {} }) + local child = {} + content[3] = child + assert(content:indexOf(child) == 3, 'Failed to assign') + assert(content:indexOf({}) == nil, 'Found non-existent child') + )")); + }); + } + TEST_F(LuaUiContentTest, BoundsChecks) { {