diff --git a/apps/openmw_test_suite/CMakeLists.txt b/apps/openmw_test_suite/CMakeLists.txt index 1a961374ef..81f6fadabe 100644 --- a/apps/openmw_test_suite/CMakeLists.txt +++ b/apps/openmw_test_suite/CMakeLists.txt @@ -27,6 +27,7 @@ file(GLOB UNITTEST_SRC_FILES lua/test_configuration.cpp lua/test_l10n.cpp lua/test_storage.cpp + lua/test_async.cpp lua/test_ui_content.cpp diff --git a/apps/openmw_test_suite/lua/test_async.cpp b/apps/openmw_test_suite/lua/test_async.cpp new file mode 100644 index 0000000000..c1e1fd6c86 --- /dev/null +++ b/apps/openmw_test_suite/lua/test_async.cpp @@ -0,0 +1,55 @@ +#include "gmock/gmock.h" +#include + +#include +#include + +#include "../testing_util.hpp" + +namespace +{ + using namespace testing; + using namespace TestingOpenMW; + + struct LuaCoroutineCallbackTest : Test + { + void SetUp() override + { + mLua.open_libraries(sol::lib::coroutine); + mLua["callback"] = [&](sol::protected_function fn) -> LuaUtil::Callback { + sol::table hiddenData(mLua, sol::create); + hiddenData[LuaUtil::ScriptsContainer::sScriptIdKey] = sol::table(mLua, sol::create); + return LuaUtil::Callback{ std::move(fn), hiddenData }; + }; + mLua["pass"] = [this](LuaUtil::Callback callback) { mCb = callback; }; + } + + sol::state mLua; + LuaUtil::Callback mCb; + }; + + TEST_F(LuaCoroutineCallbackTest, CoroutineCallbacks) + { + internal::CaptureStdout(); + mLua.safe_script(R"X( + local s = 'test' + coroutine.wrap(function() + pass(callback(function(v) print(s) end)) + end)() + )X"); + mLua.collect_garbage(); + mCb.call(); + EXPECT_THAT(internal::GetCapturedStdout(), "test\n"); + } + + TEST_F(LuaCoroutineCallbackTest, ErrorInCoroutineCallbacks) + { + mLua.safe_script(R"X( + coroutine.wrap(function() + pass(callback(function() error('COROUTINE CALLBACK') end)) + end)() + )X"); + mLua.collect_garbage(); + EXPECT_ERROR(mCb.call(), "COROUTINE CALLBACK"); + } +}