1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 00:23:53 +00:00
openmw/apps/openmw_test_suite/lua/test_ui_content.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

136 lines
4.1 KiB
C++
Raw Normal View History

2021-11-18 15:19:54 +00:00
#include <gtest/gtest.h>
#include <sol/sol.hpp>
#include <components/lua/luastate.hpp>
2021-11-18 15:19:54 +00:00
#include <components/lua_ui/content.hpp>
namespace
{
using namespace testing;
struct LuaUiContentTest : Test
2021-11-18 15:19:54 +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
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
{
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
}
TEST_F(LuaUiContentTest, Insert)
2021-11-18 15:19:54 +00:00
{
auto table = makeTable();
table.add(makeTable());
table.add(makeTable());
table.add(makeTable());
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
}
TEST_F(LuaUiContentTest, MakeHole)
2021-11-18 15:19:54 +00:00
{
auto table = makeTable();
table.add(makeTable());
table.add(makeTable());
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
}
TEST_F(LuaUiContentTest, WrongType)
2021-11-18 15:19:54 +00:00
{
auto table = makeTable();
table.add(makeTable());
table.add("a");
table.add(makeTable());
EXPECT_ANY_THROW(makeContent(table));
2021-11-18 15:19:54 +00:00
}
TEST_F(LuaUiContentTest, NameAccess)
2021-11-18 15:19:54 +00:00
{
auto table = makeTable();
table.add(makeTable());
table.add(makeTable("a"));
LuaUi::Content::View content = makeContent(table);
2021-11-18 15:19:54 +00:00
EXPECT_NO_THROW(content.at("a"));
content.remove("a");
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"));
}
TEST_F(LuaUiContentTest, IndexOf)
2021-11-18 15:19:54 +00:00
{
auto table = makeTable();
table.add(makeTable());
table.add(makeTable());
table.add(makeTable());
LuaUi::Content::View content = makeContent(table);
2021-11-18 15:19:54 +00:00
auto child = makeTable();
content.assign(2, child);
EXPECT_EQ(content.indexOf(child).value(), 2);
EXPECT_TRUE(!content.indexOf(makeTable()).has_value());
2021-11-18 15:19:54 +00:00
}
TEST_F(LuaUiContentTest, BoundsChecks)
2021-11-18 15:19:54 +00:00
{
auto table = makeTable();
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());
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
}
}