2021-11-18 15:19:54 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <sol/sol.hpp>
|
|
|
|
|
2023-01-29 16:06:09 +00:00
|
|
|
#include <components/lua/luastate.hpp>
|
2021-11-18 15:19:54 +00:00
|
|
|
#include <components/lua_ui/content.hpp>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
using namespace testing;
|
|
|
|
|
2023-01-29 16:06:09 +00:00
|
|
|
struct LuaUiContentTest : Test
|
2021-11-18 15:19:54 +00:00
|
|
|
{
|
2023-01-29 16:06:09 +00:00
|
|
|
LuaUtil::LuaState mLuaState{ nullptr, nullptr };
|
|
|
|
sol::state_view mSol;
|
|
|
|
sol::protected_function mNew;
|
|
|
|
LuaUiContentTest()
|
|
|
|
: mSol(mLuaState.sol())
|
|
|
|
, mNew(LuaUi::Content::makeFactory(mSol))
|
|
|
|
{
|
|
|
|
mSol.open_libraries(sol::lib::base, sol::lib::table);
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaUi::Content::View makeContent(sol::table source)
|
|
|
|
{
|
|
|
|
auto result = mNew.call(source);
|
|
|
|
if (result.get_type() != sol::type::table)
|
|
|
|
throw std::logic_error("Expected table");
|
|
|
|
return LuaUi::Content::View(result.get<sol::table>());
|
|
|
|
}
|
|
|
|
|
|
|
|
sol::table makeTable() { return sol::table(mSol, sol::create); }
|
2021-11-18 15:19:54 +00:00
|
|
|
|
2023-01-29 16:06:09 +00:00
|
|
|
sol::table makeTable(std::string name)
|
|
|
|
{
|
|
|
|
auto result = makeTable();
|
|
|
|
result["name"] = name;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(LuaUiContentTest, Create)
|
2021-11-18 15:19:54 +00:00
|
|
|
{
|
2023-01-29 16:06:09 +00:00
|
|
|
auto table = makeTable();
|
|
|
|
table.add(makeTable());
|
|
|
|
table.add(makeTable());
|
|
|
|
table.add(makeTable());
|
|
|
|
LuaUi::Content::View content = makeContent(table);
|
|
|
|
EXPECT_EQ(content.size(), 3);
|
2021-11-18 15:19:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 16:06:09 +00:00
|
|
|
TEST_F(LuaUiContentTest, Insert)
|
2021-11-18 15:19:54 +00:00
|
|
|
{
|
|
|
|
auto table = makeTable();
|
|
|
|
table.add(makeTable());
|
|
|
|
table.add(makeTable());
|
|
|
|
table.add(makeTable());
|
2023-01-29 16:06:09 +00:00
|
|
|
LuaUi::Content::View 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);
|
2021-11-18 15:19:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 16:06:09 +00:00
|
|
|
TEST_F(LuaUiContentTest, MakeHole)
|
2021-11-18 15:19:54 +00:00
|
|
|
{
|
|
|
|
auto table = makeTable();
|
|
|
|
table.add(makeTable());
|
|
|
|
table.add(makeTable());
|
2023-01-29 16:06:09 +00:00
|
|
|
LuaUi::Content::View content = makeContent(table);
|
|
|
|
sol::table t = makeTable();
|
|
|
|
EXPECT_ANY_THROW(content.assign(3, t));
|
2021-11-18 15:19:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 16:06:09 +00:00
|
|
|
TEST_F(LuaUiContentTest, WrongType)
|
2021-11-18 15:19:54 +00:00
|
|
|
{
|
|
|
|
auto table = makeTable();
|
|
|
|
table.add(makeTable());
|
|
|
|
table.add("a");
|
|
|
|
table.add(makeTable());
|
2023-01-29 16:06:09 +00:00
|
|
|
EXPECT_ANY_THROW(makeContent(table));
|
2021-11-18 15:19:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 16:06:09 +00:00
|
|
|
TEST_F(LuaUiContentTest, NameAccess)
|
2021-11-18 15:19:54 +00:00
|
|
|
{
|
|
|
|
auto table = makeTable();
|
|
|
|
table.add(makeTable());
|
|
|
|
table.add(makeTable("a"));
|
2023-01-29 16:06:09 +00:00
|
|
|
LuaUi::Content::View content = makeContent(table);
|
2021-11-18 15:19:54 +00:00
|
|
|
EXPECT_NO_THROW(content.at("a"));
|
|
|
|
content.remove("a");
|
2023-01-29 16:06:09 +00:00
|
|
|
EXPECT_EQ(content.size(), 1);
|
2021-11-18 15:19:54 +00:00
|
|
|
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"));
|
|
|
|
}
|
|
|
|
|
2023-01-29 16:06:09 +00:00
|
|
|
TEST_F(LuaUiContentTest, IndexOf)
|
2021-11-18 15:19:54 +00:00
|
|
|
{
|
|
|
|
auto table = makeTable();
|
|
|
|
table.add(makeTable());
|
|
|
|
table.add(makeTable());
|
|
|
|
table.add(makeTable());
|
2023-01-29 16:06:09 +00:00
|
|
|
LuaUi::Content::View content = makeContent(table);
|
2021-11-18 15:19:54 +00:00
|
|
|
auto child = makeTable();
|
|
|
|
content.assign(2, child);
|
2023-01-29 16:06:09 +00:00
|
|
|
EXPECT_EQ(content.indexOf(child).value(), 2);
|
|
|
|
EXPECT_TRUE(!content.indexOf(makeTable()).has_value());
|
2021-11-18 15:19:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 16:06:09 +00:00
|
|
|
TEST_F(LuaUiContentTest, BoundsChecks)
|
2021-11-18 15:19:54 +00:00
|
|
|
{
|
|
|
|
auto table = makeTable();
|
2023-01-29 16:06:09 +00:00
|
|
|
LuaUi::Content::View content = makeContent(table);
|
2021-11-18 15:19:54 +00:00
|
|
|
EXPECT_ANY_THROW(content.at(0));
|
2023-01-29 17:52:18 +00:00
|
|
|
EXPECT_EQ(content.size(), 0);
|
2021-11-18 15:19:54 +00:00
|
|
|
content.assign(content.size(), makeTable());
|
2023-01-29 17:52:18 +00:00
|
|
|
EXPECT_EQ(content.size(), 1);
|
2021-11-18 15:19:54 +00:00
|
|
|
content.assign(content.size(), makeTable());
|
2023-01-29 17:52:18 +00:00
|
|
|
EXPECT_EQ(content.size(), 2);
|
2021-11-18 15:19:54 +00:00
|
|
|
content.assign(content.size(), makeTable());
|
2023-01-29 16:06:09 +00:00
|
|
|
EXPECT_EQ(content.size(), 3);
|
2021-11-18 15:19:54 +00:00
|
|
|
EXPECT_ANY_THROW(content.at(3));
|
|
|
|
EXPECT_ANY_THROW(content.remove(3));
|
2023-01-29 17:52:18 +00:00
|
|
|
content.remove(2);
|
2021-11-18 15:19:54 +00:00
|
|
|
EXPECT_EQ(content.size(), 2);
|
2023-01-29 17:52:18 +00:00
|
|
|
EXPECT_ANY_THROW(content.at(2));
|
2021-11-18 15:19:54 +00:00
|
|
|
}
|
|
|
|
}
|