From 7f5fb073782555839c11e914092d7b7ae98b62ab Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Fri, 20 Dec 2024 15:58:02 +0100 Subject: Reorganized tests for the Test Explorer --- unit_tests/deep_tests.cpp | 2 +- unit_tests/embedded_tests.cpp | 2 +- unit_tests/init_and_shutdown.cpp | 40 ++++++++++++++++++++++++--- unit_tests/lane_tests.cpp | 60 +++++++++++++++++++--------------------- unit_tests/legacy_tests.cpp | 47 ++++++++++++++++++++++++++++++- unit_tests/linda_tests.cpp | 22 +++++++++++++-- unit_tests/shared.cpp | 24 ++++++++-------- unit_tests/shared.h | 11 ++++++++ 8 files changed, 155 insertions(+), 53 deletions(-) (limited to 'unit_tests') diff --git a/unit_tests/deep_tests.cpp b/unit_tests/deep_tests.cpp index 60dadb7..2c9ccdf 100644 --- a/unit_tests/deep_tests.cpp +++ b/unit_tests/deep_tests.cpp @@ -8,7 +8,7 @@ // ################################################################################################# // ################################################################################################# -TEST_CASE("deep_test") +TEST_CASE("misc.deep_test") { LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; S.requireSuccess( diff --git a/unit_tests/embedded_tests.cpp b/unit_tests/embedded_tests.cpp index a6952b2..bf2dd6e 100644 --- a/unit_tests/embedded_tests.cpp +++ b/unit_tests/embedded_tests.cpp @@ -23,7 +23,7 @@ namespace // ################################################################################################# -TEST_CASE("embedding") +TEST_CASE("lanes.embedding") { LuaState S{ LuaState::WithBaseLibs{ false }, LuaState::WithFixture{ false } }; diff --git a/unit_tests/init_and_shutdown.cpp b/unit_tests/init_and_shutdown.cpp index 83c0350..78c472f 100644 --- a/unit_tests/init_and_shutdown.cpp +++ b/unit_tests/init_and_shutdown.cpp @@ -3,7 +3,7 @@ // ################################################################################################# -TEST_CASE("require 'lanes'") +TEST_CASE("lanes.require 'lanes'") { LuaState L{ LuaState::WithBaseLibs{ false }, LuaState::WithFixture{ false } }; @@ -668,7 +668,7 @@ TEST_CASE("lanes.finally") { LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; // we need Lanes to be up. Since we run several 'scripts', we store it as a global - S.requireSuccess("lanes = require 'lanes'"); + S.requireSuccess("lanes = require 'lanes'.configure()"); // we can set a function S.requireSuccess("lanes.finally(function() end)"); // we can clear it @@ -689,7 +689,7 @@ TEST_CASE("lanes.finally") LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; // we need Lanes to be up. Since we run several 'scripts', we store it as a global - S.requireSuccess("lanes = require 'lanes'"); + S.requireSuccess("lanes = require 'lanes'.configure()"); // works because we have package.preload.fixture = luaopen_fixture S.requireSuccess("fixture = require 'fixture'"); // set our detectable finalizer @@ -699,6 +699,38 @@ TEST_CASE("lanes.finally") // the finalizer should be called REQUIRE(S.finalizerWasCalled); } + + // --------------------------------------------------------------------------------------------- + + SECTION("shutdown with an uncooperative lane") + { + LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; + S.requireSuccess("lanes = require 'lanes'.configure()"); + + // prepare a callback for lanes.finally() + static bool _wasCalled{}; + static bool _allLanesTerminated{}; + auto _finallyCB{ +[](lua_State* const L_) { _wasCalled = true; _allLanesTerminated = lua_toboolean(L_, 1); return 0; } }; + lua_pushcfunction(S, _finallyCB); + lua_setglobal(S, "finallyCB"); + // start a lane that lasts a long time + std::string_view const _script{ + " lanes.finally(finallyCB)" + " g = lanes.gen('*'," + " {name = 'auto'}," + " function()" + " for i = 1,1e37 do end" // no cooperative cancellation checks here! + " end)" + " g()" + }; + S.requireSuccess(_script); + // close the state before the lane ends. + // since we don't wait at all, it is possible that the OS thread for the lane hasn't even started at that point + S.close(); + // the finally handler should have been called, and told all lanes are stopped + REQUIRE(_wasCalled); + REQUIRE(_allLanesTerminated); + } } // ################################################################################################# @@ -719,7 +751,7 @@ namespace // ################################################################################################# -TEST_CASE("on_state_create setting") +TEST_CASE("lanes.on_state_create setting") { LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; diff --git a/unit_tests/lane_tests.cpp b/unit_tests/lane_tests.cpp index 7f5f112..498c96e 100644 --- a/unit_tests/lane_tests.cpp +++ b/unit_tests/lane_tests.cpp @@ -89,35 +89,6 @@ TEST_CASE("lanes.gen") // --------------------------------------------------------------------------------------------- - SECTION("lanes.finally") // TODO: move this section somewhere else, in a dedicated finally TEST_CASE - { - // prepare a callback for lanes.finally() - static bool _wasCalled{}; - static bool _allLanesTerminated{}; - auto _finallyCB{ +[](lua_State* const L_) { _wasCalled = true; _allLanesTerminated = lua_toboolean(L_, 1); return 0; } }; - lua_pushcfunction(S, _finallyCB); - lua_setglobal(S, "finallyCB"); - // start a lane that lasts a long time - std::string_view const _script{ - " lanes.finally(finallyCB)" - " g = lanes.gen('*'," - " {name = 'auto'}," - " function()" - " for i = 1,1e37 do end" // no cooperative cancellation checks here! - " end)" - " g()" - }; - S.requireSuccess(_script); - // close the state before the lane ends. - // since we don't wait at all, it is possible that the OS thread for the lane hasn't even started at that point - S.close(); - // the finally handler should have been called, and told all lanes are stopped - REQUIRE(_wasCalled); - REQUIRE(_allLanesTerminated); - } - - // --------------------------------------------------------------------------------------------- - SECTION("default thread name is ''") { std::string_view const _script{ @@ -172,7 +143,7 @@ TEST_CASE("lanes.gen") // ################################################################################################# // ################################################################################################# -TEST_CASE("lane:cancel") +TEST_CASE("lane.cancel") { LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; @@ -252,7 +223,33 @@ TEST_CASE("lane:cancel") // ################################################################################################# // ################################################################################################# -TEST_CASE("lane scripted tests") +// unfortunately, VS Test adapter does not list individual sections, +// so let's create a separate test case for each file with an ugly macro... + +#define MAKE_TEST_CASE(DIR, FILE, CONDITION)\ +TEST_CASE("scripted tests." #DIR "." #FILE) \ +{ \ + FileRunner _runner(R"(.\lanes\unit_tests\scripts)"); \ + _runner.performTest(FileRunnerParam{ #DIR "/" #FILE, TestType::CONDITION }); \ +} + +#if LUAJIT_FLAVOR() == 0 +MAKE_TEST_CASE(lane, cooperative_shutdown, AssertNoLuaError) +#endif // LUAJIT_FLAVOR +MAKE_TEST_CASE(lane, uncooperative_shutdown, AssertThrows) +MAKE_TEST_CASE(lane, tasking_basic, AssertNoLuaError) +MAKE_TEST_CASE(lane, tasking_cancelling, AssertNoLuaError) +MAKE_TEST_CASE(lane, tasking_comms_criss_cross, AssertNoLuaError) +MAKE_TEST_CASE(lane, tasking_communications, AssertNoLuaError) +MAKE_TEST_CASE(lane, tasking_error, AssertNoLuaError) +MAKE_TEST_CASE(lane, tasking_join_test, AssertNoLuaError) +MAKE_TEST_CASE(lane, tasking_send_receive_code, AssertNoLuaError) +MAKE_TEST_CASE(lane, stdlib_naming, AssertNoLuaError) +MAKE_TEST_CASE(coro, basics, AssertNoLuaError) +MAKE_TEST_CASE(coro, error_handling, AssertNoLuaError) + +/* +TEST_CASE("lanes.scripted tests") { auto const& _testParam = GENERATE( FileRunnerParam{ PUC_LUA_ONLY("lane/cooperative_shutdown"), TestType::AssertNoLuaError }, // 0 @@ -272,3 +269,4 @@ TEST_CASE("lane scripted tests") FileRunner _runner(R"(.\lanes\unit_tests\scripts)"); _runner.performTest(_testParam); } +*/ diff --git a/unit_tests/legacy_tests.cpp b/unit_tests/legacy_tests.cpp index cb91c93..509bb22 100644 --- a/unit_tests/legacy_tests.cpp +++ b/unit_tests/legacy_tests.cpp @@ -7,7 +7,51 @@ // ################################################################################################# // ################################################################################################# -TEST_CASE("legacy scripted tests") +// unfortunately, VS Test adapter does not list individual sections, +// so let's create a separate test case for each file with an ugly macro... + +#define MAKE_TEST_CASE(FILE) \ +TEST_CASE("scripted tests.legacy." #FILE) \ +{ \ + FileRunner _runner(R"(.\lanes\tests\)"); \ + _runner.performTest(FileRunnerParam{ #FILE, TestType::AssertNoLuaError }); \ +} + +MAKE_TEST_CASE(appendud) +MAKE_TEST_CASE(atexit) +MAKE_TEST_CASE(atomic) +MAKE_TEST_CASE(basic) +MAKE_TEST_CASE(cancel) +MAKE_TEST_CASE(cyclic) +MAKE_TEST_CASE(deadlock) +MAKE_TEST_CASE(errhangtest) +MAKE_TEST_CASE(error) +MAKE_TEST_CASE(fibonacci) +MAKE_TEST_CASE(fifo) +MAKE_TEST_CASE(finalizer) +MAKE_TEST_CASE(func_is_string) +MAKE_TEST_CASE(irayo_closure) +MAKE_TEST_CASE(irayo_recursive) +MAKE_TEST_CASE(keeper) +//MAKE_TEST_CASE(linda_perf) +#if LUA_VERSION_NUM == 504 +MAKE_TEST_CASE(manual_register) +#endif // LUA_VERSION_NUM +MAKE_TEST_CASE(nameof) +MAKE_TEST_CASE(objects) +MAKE_TEST_CASE(package) +MAKE_TEST_CASE(pingpong) +MAKE_TEST_CASE(recursive) +MAKE_TEST_CASE(require) +MAKE_TEST_CASE(rupval) +MAKE_TEST_CASE(timer) +#if LUA_VERSION_NUM == 504 +MAKE_TEST_CASE(tobeclosed) +#endif // LUA_VERSION_NUM +MAKE_TEST_CASE(track_lanes) + +/* +TEST_CASE("lanes.legacy scripted tests") { auto const& _testParam = GENERATE( FileRunnerParam{ "appendud", TestType::AssertNoLuaError } // 0 @@ -43,5 +87,6 @@ TEST_CASE("legacy scripted tests") FileRunner _runner(R"(.\lanes\tests\)"); _runner.performTest(_testParam); } +*/ #endif // RUN_LEGACY_TESTS \ No newline at end of file diff --git a/unit_tests/linda_tests.cpp b/unit_tests/linda_tests.cpp index 29eb28c..e956999 100644 --- a/unit_tests/linda_tests.cpp +++ b/unit_tests/linda_tests.cpp @@ -3,7 +3,7 @@ // ################################################################################################# -TEST_CASE("single Keeper Lindas") +TEST_CASE("linda.single Keeper") { LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; S.requireSuccess("lanes = require 'lanes'"); @@ -306,7 +306,7 @@ TEST_CASE("single Keeper Lindas") // ################################################################################################# // ################################################################################################# -TEST_CASE("multi Keeper Lindas") +TEST_CASE("linda.multi Keeper") { LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; @@ -323,7 +323,22 @@ TEST_CASE("multi Keeper Lindas") // ################################################################################################# // ################################################################################################# -TEST_CASE("linda scripted tests") +// unfortunately, VS Test adapter does not list individual sections, +// so let's create a separate test case for each file with an ugly macro... + +#define MAKE_TEST_CASE(DIR, FILE) \ +TEST_CASE("scripted tests." #DIR "." #FILE) \ +{ \ + FileRunner _runner(R"(.\lanes\unit_tests\scripts)"); \ + _runner.performTest(FileRunnerParam{ #DIR "/" #FILE, TestType::AssertNoLuaError }); \ +} + +MAKE_TEST_CASE(linda, send_receive) +MAKE_TEST_CASE(linda, send_registered_userdata) +MAKE_TEST_CASE(linda, multiple_keepers) + +/* +TEST_CASE("linda.scripted tests") { auto const& _testParam = GENERATE( FileRunnerParam{ "linda/send_receive", TestType::AssertNoLuaError }, @@ -334,3 +349,4 @@ TEST_CASE("linda scripted tests") FileRunner _runner(R"(.\lanes\unit_tests\scripts)"); _runner.performTest(_testParam); } +*/ \ No newline at end of file diff --git a/unit_tests/shared.cpp b/unit_tests/shared.cpp index 0825227..d2b4ef7 100644 --- a/unit_tests/shared.cpp +++ b/unit_tests/shared.cpp @@ -98,7 +98,7 @@ namespace // ################################################################################################# // ################################################################################################# -TEST_CASE("stack checker") +TEST_CASE("lanes.stack checker") { LuaState _L{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; StackChecker::CallsCassert = false; @@ -292,10 +292,10 @@ LuaError LuaState::loadFile(std::filesystem::path const& root_, std::string_view void LuaState::requireFailure(std::string_view const& script_) { auto const _result{ doString(script_) }; - REQUIRE(_result != LuaError::OK); if (_result == LuaError::OK) { - INFO(luaG_tostring(L, kIdxTop)); + WARN(luaG_tostring(L, kIdxTop)); } + REQUIRE(_result != LuaError::OK); lua_settop(L, 0); } @@ -304,10 +304,10 @@ void LuaState::requireFailure(std::string_view const& script_) void LuaState::requireNotReturnedString(std::string_view const& script_, std::string_view const& unexpected_) { auto const _result{ doStringAndRet(script_) }; - REQUIRE(_result != unexpected_); if (_result == unexpected_) { - INFO(_result); + WARN(_result); } + REQUIRE(_result != unexpected_); lua_settop(L, 0); } @@ -316,10 +316,10 @@ void LuaState::requireNotReturnedString(std::string_view const& script_, std::st void LuaState::requireReturnedString(std::string_view const& script_, std::string_view const& expected_) { auto const _result{ doStringAndRet(script_) }; - REQUIRE(_result == expected_); if (_result != expected_) { - INFO(_result); + WARN(_result); } + REQUIRE(_result == expected_); lua_settop(L, 0); } @@ -328,10 +328,10 @@ void LuaState::requireReturnedString(std::string_view const& script_, std::strin void LuaState::requireSuccess(std::string_view const& script_) { auto const _result{ doString(script_) }; - REQUIRE(_result == LuaError::OK); if (_result != LuaError::OK) { - INFO(luaG_tostring(L, kIdxTop)); + WARN(luaG_tostring(L, kIdxTop)); } + REQUIRE(_result == LuaError::OK); lua_settop(L, 0); } @@ -340,10 +340,10 @@ void LuaState::requireSuccess(std::string_view const& script_) void LuaState::requireSuccess(std::filesystem::path const& root_, std::string_view const& path_) { auto const _result{ doFile(root_, path_) }; - REQUIRE(_result == LuaError::OK); if (_result != LuaError::OK) { - INFO(luaG_tostring(L, kIdxTop)); + WARN(luaG_tostring(L, kIdxTop)); } + REQUIRE(_result == LuaError::OK); lua_settop(L, 0); } @@ -360,7 +360,7 @@ LuaError LuaState::runChunk() const // ################################################################################################# // ################################################################################################# -TEST_CASE("LuaState::doString") +TEST_CASE("LuaState.doString") { LuaState _L{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; // if the script fails to load, we should find the error message at the top of the stack diff --git a/unit_tests/shared.h b/unit_tests/shared.h index 2a32269..2e319e4 100644 --- a/unit_tests/shared.h +++ b/unit_tests/shared.h @@ -67,6 +67,17 @@ struct FileRunnerParam TestType test; }; +// Define a specialization for FileRunnerParam in Catch::Detail::stringify +namespace Catch { + namespace Detail { + template <> + inline std::string stringify(FileRunnerParam const& param_) + { + return std::string{ param_.script }; + } + } // namespace Detail +} // namespace Catch + class FileRunner : private LuaState { private: -- cgit v1.2.3-55-g6feb