mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-16 15:29:55 +00:00
Simplify and fix the storage subscribe test
This commit is contained in:
parent
35448bf0fe
commit
851e291501
4 changed files with 38 additions and 26 deletions
|
@ -15,7 +15,7 @@ namespace
|
||||||
return lua.safe_script("return " + luaCode).get<T>();
|
return lua.safe_script("return " + luaCode).get<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(LuaUtilStorageTest, Basic)
|
TEST(LuaUtilStorageTest, Subscribe)
|
||||||
{
|
{
|
||||||
// Note: LuaUtil::Callback can be used only if Lua is initialized via LuaUtil::LuaState
|
// Note: LuaUtil::Callback can be used only if Lua is initialized via LuaUtil::LuaState
|
||||||
LuaUtil::LuaState luaState{ nullptr, nullptr };
|
LuaUtil::LuaState luaState{ nullptr, nullptr };
|
||||||
|
@ -24,21 +24,21 @@ namespace
|
||||||
LuaUtil::LuaStorage storage(mLua);
|
LuaUtil::LuaStorage storage(mLua);
|
||||||
storage.setActive(true);
|
storage.setActive(true);
|
||||||
|
|
||||||
std::vector<std::string> callbackCalls;
|
|
||||||
sol::table callbackHiddenData(mLua, sol::create);
|
sol::table callbackHiddenData(mLua, sol::create);
|
||||||
callbackHiddenData[LuaUtil::ScriptsContainer::sScriptIdKey] = LuaUtil::ScriptId{};
|
callbackHiddenData[LuaUtil::ScriptsContainer::sScriptIdKey] = LuaUtil::ScriptId{};
|
||||||
sol::table callback(mLua, sol::create);
|
LuaUtil::getAsyncPackageInitializer(
|
||||||
callback[1] = [&](const std::string& section, const sol::optional<std::string>& key) {
|
mLua.lua_state(), []() { return 0.0; }, []() { return 0.0; })(callbackHiddenData);
|
||||||
if (key)
|
mLua["async"] = LuaUtil::AsyncPackageId{ nullptr, 0, callbackHiddenData };
|
||||||
callbackCalls.push_back(section + "_" + *key);
|
|
||||||
else
|
|
||||||
callbackCalls.push_back(section + "_*");
|
|
||||||
};
|
|
||||||
callback[2] = LuaUtil::AsyncPackageId{ nullptr, 0, callbackHiddenData };
|
|
||||||
|
|
||||||
mLua["mutable"] = storage.getMutableSection("test");
|
mLua["mutable"] = storage.getMutableSection("test");
|
||||||
mLua["ro"] = storage.getReadOnlySection("test");
|
mLua["ro"] = storage.getReadOnlySection("test");
|
||||||
mLua["ro"]["subscribe"](mLua["ro"], callback);
|
|
||||||
|
mLua.safe_script(R"(
|
||||||
|
callbackCalls = {}
|
||||||
|
ro:subscribe(async:callback(function(section, key)
|
||||||
|
table.insert(callbackCalls, section .. '_' .. (key or '*'))
|
||||||
|
end))
|
||||||
|
)");
|
||||||
|
|
||||||
mLua.safe_script("mutable:set('x', 5)");
|
mLua.safe_script("mutable:set('x', 5)");
|
||||||
EXPECT_EQ(get<int>(mLua, "mutable:get('x')"), 5);
|
EXPECT_EQ(get<int>(mLua, "mutable:get('x')"), 5);
|
||||||
|
@ -58,7 +58,7 @@ namespace
|
||||||
EXPECT_EQ(get<int>(mLua, "ro:get('x')"), 4);
|
EXPECT_EQ(get<int>(mLua, "ro:get('x')"), 4);
|
||||||
EXPECT_EQ(get<int>(mLua, "ro:get('y')"), 7);
|
EXPECT_EQ(get<int>(mLua, "ro:get('y')"), 7);
|
||||||
|
|
||||||
EXPECT_THAT(callbackCalls, ::testing::ElementsAre("test_x", "test_*", "test_*"));
|
EXPECT_THAT(get<std::string>(mLua, "table.concat(callbackCalls, ', ')"), "test_x, test_*, test_*");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(LuaUtilStorageTest, Table)
|
TEST(LuaUtilStorageTest, Table)
|
||||||
|
|
|
@ -24,13 +24,32 @@ namespace LuaUtil
|
||||||
|
|
||||||
Callback Callback::fromLua(const sol::table& t)
|
Callback Callback::fromLua(const sol::table& t)
|
||||||
{
|
{
|
||||||
const sol::object& function = t.get_or(1, sol::nil);
|
const sol::object& function = t.raw_get<sol::object>(1);
|
||||||
const sol::object& asyncPackageId = t.get_or(2, sol::nil);
|
const sol::object& asyncPackageId = t.raw_get<sol::object>(2);
|
||||||
if (!function.is<sol::main_protected_function>() || !asyncPackageId.is<AsyncPackageId>())
|
if (!function.is<sol::main_protected_function>() || !asyncPackageId.is<AsyncPackageId>())
|
||||||
throw std::domain_error("Expected an async:callback, received a table");
|
throw std::domain_error("Expected an async:callback, received a table");
|
||||||
return Callback{ function.as<sol::main_protected_function>(), asyncPackageId.as<AsyncPackageId>().mHiddenData };
|
return Callback{ function.as<sol::main_protected_function>(), asyncPackageId.as<AsyncPackageId>().mHiddenData };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sol::table Callback::makeMetatable(lua_State* L)
|
||||||
|
{
|
||||||
|
sol::table callbackMeta = sol::table::create(L);
|
||||||
|
callbackMeta[sol::meta_function::call] = [](const sol::table& callback, sol::variadic_args va) {
|
||||||
|
return Callback::fromLua(callback).call(sol::as_args(va));
|
||||||
|
};
|
||||||
|
callbackMeta[sol::meta_function::to_string] = [] { return "Callback"; };
|
||||||
|
callbackMeta[sol::meta_function::metatable] = false;
|
||||||
|
callbackMeta["isCallback"] = true;
|
||||||
|
return callbackMeta;
|
||||||
|
}
|
||||||
|
sol::table Callback::make(const AsyncPackageId& asyncId, sol::main_protected_function fn, sol::table metatable)
|
||||||
|
{
|
||||||
|
sol::table c = sol::table::create(fn.lua_state(), 2);
|
||||||
|
c.raw_set(1, std::move(fn), 2, asyncId);
|
||||||
|
c[sol::metatable_key] = metatable;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
bool Callback::isLuaCallback(const sol::object& t)
|
bool Callback::isLuaCallback(const sol::object& t)
|
||||||
{
|
{
|
||||||
if (!t.is<sol::table>())
|
if (!t.is<sol::table>())
|
||||||
|
@ -73,18 +92,9 @@ namespace LuaUtil
|
||||||
TimerType::GAME_TIME, gameTimeFn() + delay, asyncId.mScriptId, std::move(callback));
|
TimerType::GAME_TIME, gameTimeFn() + delay, asyncId.mScriptId, std::move(callback));
|
||||||
};
|
};
|
||||||
|
|
||||||
sol::table callbackMeta = sol::table::create(L);
|
sol::table callbackMeta = Callback::makeMetatable(L);
|
||||||
callbackMeta[sol::meta_function::call] = [](const sol::table& callback, sol::variadic_args va) {
|
|
||||||
return Callback::fromLua(callback).call(sol::as_args(va));
|
|
||||||
};
|
|
||||||
callbackMeta[sol::meta_function::to_string] = [] { return "Callback"; };
|
|
||||||
callbackMeta[sol::meta_function::metatable] = false;
|
|
||||||
callbackMeta["isCallback"] = true;
|
|
||||||
api["callback"] = [callbackMeta](const AsyncPackageId& asyncId, sol::main_protected_function fn) -> sol::table {
|
api["callback"] = [callbackMeta](const AsyncPackageId& asyncId, sol::main_protected_function fn) -> sol::table {
|
||||||
sol::table c = sol::table::create(fn.lua_state(), 2);
|
return Callback::make(asyncId, fn, callbackMeta);
|
||||||
c.raw_set(1, std::move(fn), 2, asyncId);
|
|
||||||
c[sol::metatable_key] = callbackMeta;
|
|
||||||
return c;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
auto initializer = [](sol::table hiddenData) {
|
auto initializer = [](sol::table hiddenData) {
|
||||||
|
|
|
@ -24,6 +24,8 @@ namespace LuaUtil
|
||||||
|
|
||||||
static bool isLuaCallback(const sol::object&);
|
static bool isLuaCallback(const sol::object&);
|
||||||
static Callback fromLua(const sol::table&);
|
static Callback fromLua(const sol::table&);
|
||||||
|
static sol::table makeMetatable(lua_State* L);
|
||||||
|
static sol::table make(const AsyncPackageId& asyncId, sol::main_protected_function fn, sol::table metatable);
|
||||||
|
|
||||||
bool isValid() const { return mHiddenData[ScriptsContainer::sScriptIdKey] != sol::nil; }
|
bool isValid() const { return mHiddenData[ScriptsContainer::sScriptIdKey] != sol::nil; }
|
||||||
|
|
||||||
|
|
|
@ -377,7 +377,7 @@ namespace LuaUtil
|
||||||
sol::protected_function_result LuaState::throwIfError(sol::protected_function_result&& res)
|
sol::protected_function_result LuaState::throwIfError(sol::protected_function_result&& res)
|
||||||
{
|
{
|
||||||
if (!res.valid() && static_cast<int>(res.get_type()) == LUA_TSTRING)
|
if (!res.valid() && static_cast<int>(res.get_type()) == LUA_TSTRING)
|
||||||
throw std::runtime_error("Lua error: " + res.get<std::string>());
|
throw std::runtime_error(std::string("Lua error: ") += res.get<sol::error>().what());
|
||||||
else
|
else
|
||||||
return std::move(res);
|
return std::move(res);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue