2022-10-17 15:53:13 +00:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-02-12 21:21:35 +00:00
|
|
|
#include <components/lua/asyncpackage.hpp>
|
2022-10-17 15:53:13 +00:00
|
|
|
#include <components/lua/luastate.hpp>
|
|
|
|
|
|
|
|
#include "../testing_util.hpp"
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
using namespace testing;
|
|
|
|
using namespace TestingOpenMW;
|
|
|
|
|
|
|
|
struct LuaCoroutineCallbackTest : Test
|
|
|
|
{
|
|
|
|
void SetUp() override
|
|
|
|
{
|
2022-11-13 23:12:48 +00:00
|
|
|
mLua.sol()["callback"] = [&](sol::protected_function fn) -> LuaUtil::Callback {
|
|
|
|
sol::table hiddenData(mLua.sol(), sol::create);
|
|
|
|
hiddenData[LuaUtil::ScriptsContainer::sScriptIdKey] = LuaUtil::ScriptId{};
|
2022-10-17 15:53:13 +00:00
|
|
|
return LuaUtil::Callback{ std::move(fn), hiddenData };
|
|
|
|
};
|
2022-11-13 23:12:48 +00:00
|
|
|
mLua.sol()["pass"] = [this](LuaUtil::Callback callback) { mCb = callback; };
|
2022-10-17 15:53:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 23:12:48 +00:00
|
|
|
LuaUtil::LuaState mLua{ nullptr, nullptr };
|
2022-10-17 15:53:13 +00:00
|
|
|
LuaUtil::Callback mCb;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(LuaCoroutineCallbackTest, CoroutineCallbacks)
|
|
|
|
{
|
|
|
|
internal::CaptureStdout();
|
2022-11-13 23:12:48 +00:00
|
|
|
mLua.sol().safe_script(R"X(
|
2022-10-17 15:53:13 +00:00
|
|
|
local s = 'test'
|
|
|
|
coroutine.wrap(function()
|
|
|
|
pass(callback(function(v) print(s) end))
|
|
|
|
end)()
|
|
|
|
)X");
|
2022-11-13 23:12:48 +00:00
|
|
|
mLua.sol().collect_garbage();
|
2022-10-17 15:53:13 +00:00
|
|
|
mCb.call();
|
|
|
|
EXPECT_THAT(internal::GetCapturedStdout(), "test\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LuaCoroutineCallbackTest, ErrorInCoroutineCallbacks)
|
|
|
|
{
|
2022-11-13 23:12:48 +00:00
|
|
|
mLua.sol().safe_script(R"X(
|
2022-10-17 15:53:13 +00:00
|
|
|
coroutine.wrap(function()
|
|
|
|
pass(callback(function() error('COROUTINE CALLBACK') end))
|
|
|
|
end)()
|
|
|
|
)X");
|
2022-11-13 23:12:48 +00:00
|
|
|
mLua.sol().collect_garbage();
|
2022-10-17 15:53:13 +00:00
|
|
|
EXPECT_ERROR(mCb.call(), "COROUTINE CALLBACK");
|
|
|
|
}
|
|
|
|
}
|