1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-04 02:41:19 +00:00
OpenMW/apps/openmw_test_suite/lua/test_storage.cpp

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

117 lines
4.4 KiB
C++
Raw Normal View History

2021-12-13 23:39:01 +00:00
#include <filesystem>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
2023-02-14 08:40:22 +00:00
#include <components/lua/asyncpackage.hpp>
2021-12-13 23:39:01 +00:00
#include <components/lua/storage.hpp>
namespace
{
using namespace testing;
template <typename T>
T get(sol::state_view& lua, std::string luaCode)
2021-12-13 23:39:01 +00:00
{
return lua.safe_script("return " + luaCode).get<T>();
}
TEST(LuaUtilStorageTest, Basic)
{
// Note: LuaUtil::Callback can be used only if Lua is initialized via LuaUtil::LuaState
LuaUtil::LuaState luaState{ nullptr, nullptr };
sol::state_view& mLua = luaState.sol();
2021-12-13 23:39:01 +00:00
LuaUtil::LuaStorage::initLuaBindings(mLua);
LuaUtil::LuaStorage storage(mLua);
2022-04-23 13:37:08 +00:00
std::vector<std::string> callbackCalls;
2023-02-14 08:40:22 +00:00
sol::table callbackHiddenData(mLua, sol::create);
callbackHiddenData[LuaUtil::ScriptsContainer::sScriptIdKey] = LuaUtil::ScriptId{};
sol::table callback(mLua, sol::create);
callback[1] = [&](const std::string& section, const sol::optional<std::string>& key) {
if (key)
callbackCalls.push_back(section + "_" + *key);
else
callbackCalls.push_back(section + "_*");
};
callback[2] = LuaUtil::AsyncPackageId{ nullptr, 0, callbackHiddenData };
2022-04-23 13:37:08 +00:00
2021-12-13 23:39:01 +00:00
mLua["mutable"] = storage.getMutableSection("test");
mLua["ro"] = storage.getReadOnlySection("test");
2022-04-23 13:37:08 +00:00
mLua["ro"]["subscribe"](mLua["ro"], callback);
2021-12-13 23:39:01 +00:00
mLua.safe_script("mutable:set('x', 5)");
EXPECT_EQ(get<int>(mLua, "mutable:get('x')"), 5);
EXPECT_EQ(get<int>(mLua, "ro:get('x')"), 5);
EXPECT_THROW(mLua.safe_script("ro:set('y', 3)"), std::exception);
mLua.safe_script("t1 = mutable:asTable()");
mLua.safe_script("t2 = ro:asTable()");
EXPECT_EQ(get<int>(mLua, "t1.x"), 5);
EXPECT_EQ(get<int>(mLua, "t2.x"), 5);
mLua.safe_script("mutable:reset()");
EXPECT_TRUE(get<bool>(mLua, "ro:get('x') == nil"));
mLua.safe_script("mutable:reset({x=4, y=7})");
EXPECT_EQ(get<int>(mLua, "ro:get('x')"), 4);
EXPECT_EQ(get<int>(mLua, "ro:get('y')"), 7);
2022-04-23 13:37:08 +00:00
EXPECT_THAT(callbackCalls, ::testing::ElementsAre("test_x", "test_*", "test_*"));
2021-12-13 23:39:01 +00:00
}
TEST(LuaUtilStorageTest, Table)
{
sol::state mLua;
LuaUtil::LuaStorage::initLuaBindings(mLua);
LuaUtil::LuaStorage storage(mLua);
mLua["mutable"] = storage.getMutableSection("test");
mLua["ro"] = storage.getReadOnlySection("test");
mLua.safe_script("mutable:set('x', { y = 'abc', z = 7 })");
EXPECT_EQ(get<int>(mLua, "mutable:get('x').z"), 7);
EXPECT_THROW(mLua.safe_script("mutable:get('x').z = 3"), std::exception);
EXPECT_NO_THROW(mLua.safe_script("mutable:getCopy('x').z = 3"));
EXPECT_EQ(get<int>(mLua, "mutable:get('x').z"), 7);
EXPECT_EQ(get<int>(mLua, "ro:get('x').z"), 7);
EXPECT_EQ(get<std::string>(mLua, "ro:get('x').y"), "abc");
}
TEST(LuaUtilStorageTest, Saving)
{
sol::state mLua;
LuaUtil::LuaStorage::initLuaBindings(mLua);
LuaUtil::LuaStorage storage(mLua);
mLua["permanent"] = storage.getMutableSection("permanent");
mLua["temporary"] = storage.getMutableSection("temporary");
mLua.safe_script("temporary:removeOnExit()");
mLua.safe_script("permanent:set('x', 1)");
mLua.safe_script("temporary:set('y', 2)");
const auto tmpFile = std::filesystem::temp_directory_path() / "test_storage.bin";
2021-12-13 23:39:01 +00:00
storage.save(tmpFile);
EXPECT_EQ(get<int>(mLua, "permanent:get('x')"), 1);
EXPECT_EQ(get<int>(mLua, "temporary:get('y')"), 2);
2022-04-23 13:37:08 +00:00
storage.clearTemporaryAndRemoveCallbacks();
2021-12-13 23:39:01 +00:00
mLua["permanent"] = storage.getMutableSection("permanent");
mLua["temporary"] = storage.getMutableSection("temporary");
EXPECT_EQ(get<int>(mLua, "permanent:get('x')"), 1);
EXPECT_TRUE(get<bool>(mLua, "temporary:get('y') == nil"));
mLua.safe_script("permanent:set('x', 3)");
mLua.safe_script("permanent:set('z', 4)");
LuaUtil::LuaStorage storage2(mLua);
storage2.load(tmpFile);
2021-12-13 23:39:01 +00:00
mLua["permanent"] = storage2.getMutableSection("permanent");
mLua["temporary"] = storage2.getMutableSection("temporary");
EXPECT_EQ(get<int>(mLua, "permanent:get('x')"), 1);
EXPECT_TRUE(get<bool>(mLua, "permanent:get('z') == nil"));
EXPECT_TRUE(get<bool>(mLua, "temporary:get('y') == nil"));
}
}