diff --git a/CMakeLists.txt b/CMakeLists.txt index b8254db7f..4c6c60ba9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -278,7 +278,7 @@ if(NOT USE_LUAJIT) add_subdirectory(lib/bitop) endif() -if(BUILD_BENCHMARKS) +if(BUILD_UNITTESTS OR BUILD_BENCHMARKS) add_subdirectory(lib/catch2) endif() diff --git a/lib/catch2/CMakeLists.txt b/lib/catch2/CMakeLists.txt index 3c471d49d..077272571 100644 --- a/lib/catch2/CMakeLists.txt +++ b/lib/catch2/CMakeLists.txt @@ -13,4 +13,5 @@ # + return os << std::fixed << duration.value() << ' ' << duration.unitsAsString(); add_library(catch2 INTERFACE) -target_include_directories(catch2 INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) +add_library(Catch2::Catch2 ALIAS catch2) +target_include_directories(catch2 INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 03e914a5f..8fed18524 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -614,8 +614,8 @@ if(BUILD_CLIENT) if (USE_SPATIAL) target_link_libraries(${PROJECT_NAME} ${SPATIAL_LIBRARY}) endif() - if(BUILD_BENCHMARKS) - target_link_libraries(${PROJECT_NAME} catch2) + if(BUILD_UNITTESTS OR BUILD_BENCHMARKS) + target_link_libraries(${PROJECT_NAME} Catch2::Catch2) endif() endif(BUILD_CLIENT) @@ -676,8 +676,8 @@ if(BUILD_SERVER) ${CURL_LIBRARY} ) endif() - if(BUILD_BENCHMARKS) - target_link_libraries(${PROJECT_NAME}server catch2) + if(BUILD_UNITTESTS OR BUILD_BENCHMARKS) + target_link_libraries(${PROJECT_NAME}server Catch2::Catch2) endif() endif(BUILD_SERVER) diff --git a/src/activeobjectmgr.h b/src/activeobjectmgr.h index 0205aee85..369e8acf5 100644 --- a/src/activeobjectmgr.h +++ b/src/activeobjectmgr.h @@ -31,7 +31,6 @@ class TestServerActiveObjectMgr; template class ActiveObjectMgr { - friend class ::TestClientActiveObjectMgr; friend class ::TestServerActiveObjectMgr; public: diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index e2d19b3e1..c25a47c25 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -17,6 +17,9 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#define CATCH_CONFIG_RUNNER +#include + #include "test.h" #include "nodedef.h" @@ -248,7 +251,9 @@ bool run_tests() << num_total_tests_run << " failed individual tests)." << std::endl << " Testing took " << tdiff << "ms total." << std::endl << "++++++++++++++++++++++++++++++++++++++++" - << "++++++++++++++++++++++++++++++++++++++++" << std::endl; + << "++++++++++++++++++++++++++++++++++++++++" << std::endl + << "Catch test results: " << std::endl; + Catch::Session().run(); return num_modules_failed == 0; } diff --git a/src/unittest/test_clientactiveobjectmgr.cpp b/src/unittest/test_clientactiveobjectmgr.cpp index d3cd83dc9..954feb01d 100644 --- a/src/unittest/test_clientactiveobjectmgr.cpp +++ b/src/unittest/test_clientactiveobjectmgr.cpp @@ -17,11 +17,11 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "client/activeobjectmgr.h" -#include #include "test.h" -#include "profiler.h" +#include "client/activeobjectmgr.h" + +#include class TestClientActiveObject : public ClientActiveObject { @@ -58,9 +58,6 @@ public: void runTests(IGameDef *gamedef); - void testFreeID(); - void testRegisterObject(); - void testRemoveObject(); void testGetActiveSelectableObjects(); }; @@ -68,75 +65,62 @@ static TestClientActiveObjectMgr g_test_instance; void TestClientActiveObjectMgr::runTests(IGameDef *gamedef) { - TEST(testFreeID); - TEST(testRegisterObject) - TEST(testRemoveObject) TEST(testGetActiveSelectableObjects) } //////////////////////////////////////////////////////////////////////////////// -void TestClientActiveObjectMgr::testFreeID() +TEST_CASE("test client active object manager") { client::ActiveObjectMgr caomgr; - std::vector aoids; + auto tcao1 = new TestClientActiveObject(); + REQUIRE(caomgr.registerObject(tcao1) == true); - u16 aoid = caomgr.getFreeId(); - // Ensure it's not the same id - UASSERT(caomgr.getFreeId() != aoid); - - aoids.push_back(aoid); - - // Register basic objects, ensure we never found - for (u8 i = 0; i < UINT8_MAX; i++) { - // Register an object - auto tcao_u = std::make_unique(); - auto tcao = tcao_u.get(); - caomgr.registerObject(std::move(tcao_u)); - aoids.push_back(tcao->getId()); - - // Ensure next id is not in registered list - UASSERT(std::find(aoids.begin(), aoids.end(), caomgr.getFreeId()) == - aoids.end()); + SECTION("When we register a client object, " + "then it should be assigned a unique ID.") + { + for (int i = 0; i < UINT8_MAX; ++i) { + auto other_tcao = new TestClientActiveObject(); + REQUIRE(caomgr.registerObject(other_tcao) == true); + CHECK(other_tcao->getId() != tcao1->getId()); + } } - caomgr.clear(); -} + SECTION("two registered objects") + { + auto tcao2 = new TestClientActiveObject(); + REQUIRE(caomgr.registerObject(tcao2) == true); + auto tcao2_id = tcao2->getId(); -void TestClientActiveObjectMgr::testRegisterObject() -{ - client::ActiveObjectMgr caomgr; - auto tcao_u = std::make_unique(); - auto tcao = tcao_u.get(); - UASSERT(caomgr.registerObject(std::move(tcao_u))); + auto obj1 = caomgr.getActiveObject(tcao1->getId()); + REQUIRE(obj1 != nullptr); - u16 id = tcao->getId(); + auto obj2 = caomgr.getActiveObject(tcao2_id); + REQUIRE(obj2 != nullptr); - auto tcaoToCompare = caomgr.getActiveObject(id); - UASSERT(tcaoToCompare->getId() == id); - UASSERT(tcaoToCompare == tcao); + SECTION("When we query an object by its ID, " + "then we should get back an object with that ID.") + { + CHECK(obj1->getId() == tcao1->getId()); + CHECK(obj2->getId() == tcao2->getId()); + } - tcao_u = std::make_unique(); - tcao = tcao_u.get(); - UASSERT(caomgr.registerObject(std::move(tcao_u))); - UASSERT(caomgr.getActiveObject(tcao->getId()) == tcao); - UASSERT(caomgr.getActiveObject(tcao->getId()) != tcaoToCompare); + SECTION("When we register and query for an object, " + "its memory location should not have changed.") + { + CHECK(obj1 == tcao1); + CHECK(obj2 == tcao2); + } + } - caomgr.clear(); -} - -void TestClientActiveObjectMgr::testRemoveObject() -{ - client::ActiveObjectMgr caomgr; - auto tcao_u = std::make_unique(); - auto tcao = tcao_u.get(); - UASSERT(caomgr.registerObject(std::move(tcao_u))); - - u16 id = tcao->getId(); - UASSERT(caomgr.getActiveObject(id) != nullptr) - - caomgr.removeObject(tcao->getId()); - UASSERT(caomgr.getActiveObject(id) == nullptr) + SECTION("Given an object has been removed, " + "when we query for it, " + "then we should get nullptr.") + { + auto id = tcao1->getId(); + caomgr.removeObject(tcao1->getId()); // may invalidate tcao1 + CHECK(caomgr.getActiveObject(id) == nullptr); + } caomgr.clear(); }