From bcac56a65c2ca0aa449e9d79b9528c2f960b63db Mon Sep 17 00:00:00 2001 From: Ulrich Mohr Date: Thu, 17 Feb 2022 17:48:32 +0100 Subject: [PATCH] reworked test setup --- unittest/CMakeLists.txt | 2 +- unittest/controller/testThermalController.cpp | 20 +++---- unittest/initTests.cpp | 26 -------- unittest/main.cpp | 28 +-------- unittest/testEnvironment.cpp | 59 +++++++++++++++++++ unittest/testEnvironment.h | 36 +++++++++++ 6 files changed, 106 insertions(+), 65 deletions(-) delete mode 100644 unittest/initTests.cpp create mode 100644 unittest/testEnvironment.cpp create mode 100644 unittest/testEnvironment.h diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 39f3b66e..1786b71e 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -3,6 +3,6 @@ add_subdirectory(mocks) target_sources(${UNITTEST_NAME} PRIVATE main.cpp - initTests.cpp + testEnvironment.cpp printChar.cpp ) \ No newline at end of file diff --git a/unittest/controller/testThermalController.cpp b/unittest/controller/testThermalController.cpp index 6d599475..a0e96d68 100644 --- a/unittest/controller/testThermalController.cpp +++ b/unittest/controller/testThermalController.cpp @@ -1,24 +1,20 @@ -#include -#include -#include -#include #include #include #include -#include "../mocks/EventManagerMock.h" +#include "../testEnvironment.h" TEST_CASE("Thermal Controller", "[ThermalController]") { const object_id_t THERMAL_CONTROLLER_ID = 0x123; ThermalController controller(THERMAL_CONTROLLER_ID, objects::NO_OBJECT); - REQUIRE(controller.initialize() == HasReturnvaluesIF::RETURN_OK); + + testEnvironment::initialize(); + REQUIRE(controller.initializeAfterTaskCreation() == HasReturnvaluesIF::RETURN_OK); - EventManagerMock* eventmanager = - ObjectManager::instance()->get(objects::EVENT_MANAGER); - REQUIRE(eventmanager->isEventInEventList(THERMAL_CONTROLLER_ID, HasModesIF::MODE_INFO) == false); + testEnvironment::eventManager->clearEventList(); MessageQueueId_t controllerQueue = controller.getCommandQueue(); @@ -34,9 +30,9 @@ TEST_CASE("Thermal Controller", "[ThermalController]") { REQUIRE(controller.performOperation(0) == HasReturnvaluesIF::RETURN_OK); - REQUIRE(eventmanager->isEventInEventList(THERMAL_CONTROLLER_ID, HasModesIF::MODE_INFO, - ControllerBase::MODE_NORMAL, - HasModesIF::SUBMODE_NONE) == true); + REQUIRE(testEnvironment::eventManager->isEventInEventList( + THERMAL_CONTROLLER_ID, HasModesIF::MODE_INFO, ControllerBase::MODE_NORMAL, + HasModesIF::SUBMODE_NONE) == true); thermalControllerDefinitions::ComponentTemperatures componentTemperatures(THERMAL_CONTROLLER_ID); diff --git a/unittest/initTests.cpp b/unittest/initTests.cpp deleted file mode 100644 index 5d30bdca..00000000 --- a/unittest/initTests.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include - -#ifdef LINUX -ServiceInterfaceStream sif::debug("DEBUG "); -ServiceInterfaceStream sif::info("INFO "); -ServiceInterfaceStream sif::warning("WARNING"); -ServiceInterfaceStream sif::error("ERROR ", false, true, true); -#else -ServiceInterfaceStream sif::debug("DEBUG", true); -ServiceInterfaceStream sif::info("INFO", true); -ServiceInterfaceStream sif::warning("WARNING", true); -ServiceInterfaceStream sif::error("ERROR", true, false, true); -#endif - -#include -#include - -namespace messagetypes { -enum MESSAGE_TYPE { - MISSION_MESSAGE_TYPE_START = FW_MESSAGES_COUNT, -}; - -void clearMissionMessage(CommandMessage* message); -} // namespace messagetypes - -void messagetypes::clearMissionMessage(CommandMessage* message) {} diff --git a/unittest/main.cpp b/unittest/main.cpp index ca2d7420..6438577c 100644 --- a/unittest/main.cpp +++ b/unittest/main.cpp @@ -1,33 +1,9 @@ -#include -#include -#include -#include -#include -#include -#include - #include -#include "mocks/EventManagerMock.h" -#include "mocks/HouseKeepingMock.h" - -void factory(void* args) { - new HouseKeepingMock(); - new EventManagerMock(); - new HealthTable(objects::HEALTH_TABLE); - new InternalErrorReporter(objects::INTERNAL_ERROR_REPORTER); - new TimeStamper(objects::TIME_STAMPER); - - { - PoolManager::LocalPoolConfig poolCfg = {{300, 16}, {200, 32}, {150, 64}, {150, 128}, - {100, 256}, {50, 512}, {50, 1024}, {10, 2048}}; - new PoolManager(objects::IPC_STORE, poolCfg); - } -} +#include "testEnvironment.h" int main(int argc, char* argv[]) { - ObjectManager::instance()->setObjectFactoryFunction(factory, nullptr); - ObjectManager::instance()->initialize(); + testEnvironment::setup(); // Catch internal function call int result = Catch::Session().run(argc, argv); diff --git a/unittest/testEnvironment.cpp b/unittest/testEnvironment.cpp new file mode 100644 index 00000000..e4d5bed2 --- /dev/null +++ b/unittest/testEnvironment.cpp @@ -0,0 +1,59 @@ +#include "testEnvironment.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef LINUX +ServiceInterfaceStream sif::debug("DEBUG "); +ServiceInterfaceStream sif::info("INFO "); +ServiceInterfaceStream sif::warning("WARNING"); +ServiceInterfaceStream sif::error("ERROR ", false, true, true); +#else +ServiceInterfaceStream sif::debug("DEBUG", true); +ServiceInterfaceStream sif::info("INFO", true); +ServiceInterfaceStream sif::warning("WARNING", true); +ServiceInterfaceStream sif::error("ERROR", true, false, true); +#endif + +namespace testEnvironment { + +void factory(void* args) { + new HouseKeepingMock(); + eventManager = new EventManagerMock(); + new HealthTable(objects::HEALTH_TABLE); + new InternalErrorReporter(objects::INTERNAL_ERROR_REPORTER); + new TimeStamper(objects::TIME_STAMPER); + + { + PoolManager::LocalPoolConfig poolCfg = {{300, 16}, {200, 32}, {150, 64}, {150, 128}, + {100, 256}, {50, 512}, {50, 1024}, {10, 2048}}; + new PoolManager(objects::IPC_STORE, poolCfg); + } +} + +void setup() { ObjectManager::instance()->setObjectFactoryFunction(factory, nullptr); } + +void initialize() { ObjectManager::instance()->initialize(); } + +} // namespace testEnvironment + +EventManagerMock* testEnvironment::eventManager = nullptr; + +#include +#include + +namespace messagetypes { +enum MESSAGE_TYPE { + MISSION_MESSAGE_TYPE_START = FW_MESSAGES_COUNT, +}; + +void clearMissionMessage(CommandMessage* message); +} // namespace messagetypes + +void messagetypes::clearMissionMessage(CommandMessage* message) {} diff --git a/unittest/testEnvironment.h b/unittest/testEnvironment.h new file mode 100644 index 00000000..79ff8544 --- /dev/null +++ b/unittest/testEnvironment.h @@ -0,0 +1,36 @@ +#ifndef UNITTEST_TESTENVIRONMENT_H_ +#define UNITTEST_TESTENVIRONMENT_H_ + +#include "mocks/EventManagerMock.h" +#include "mocks/HouseKeepingMock.h" + +/* + * This namespace sets up a general environment for unittests + * + * Only objects generally used in all unittest are created here. Objects needed for + * individual tests are to be contstructed in the individual tests. + * + * The object manager can be initialized by the initialize() call + * + * It also caches pointers to generally useful objects so they do + * not need to be gotten from the object manager each time + */ +namespace testEnvironment { + +/* + * Setup code goes here, called by main() befor any tests + */ +void setup(); + +/* + * Initializes the object manager, to be called at the start of each test, after test specific + * objects are constructed + * + * All objects defined in the factory method are created here + */ +void initialize(); + +extern EventManagerMock* eventManager; +} // namespace testEnvironment + +#endif /*UNITTEST_TESTENVIRONMENT_H_*/ \ No newline at end of file