Skip to content

Commit

Permalink
Garantuee test clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
halfgaar committed Mar 3, 2024
1 parent f033a97 commit d4ea583
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions FlashMQTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ add_executable(flashmq-tests
websockettests.cpp
willtests.cpp
dnstests.cpp
testinitializer.h testinitializer.cpp

)

Expand Down
7 changes: 5 additions & 2 deletions FlashMQTests/maintests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "maintests.h"
#include "testhelpers.h"
#include "testinitializer.h"

#include "threadglobals.h"

Expand Down Expand Up @@ -319,7 +320,9 @@ bool MainTests::test(const std::vector<std::string> &tests)
try
{
std::cout << CYAN << "INIT" << COLOR_END << ": " << pair.first << std::endl;
initBeforeEachTest();

TestInitializer testInitializer(this);
testInitializer.init();

const int failCountBefore = assert_fail_count;
const int assertCountBefore = assert_count;
Expand All @@ -330,7 +333,7 @@ bool MainTests::test(const std::vector<std::string> &tests)
const int failCountAfter = assert_fail_count;
const int assertCountAfter = assert_count;

cleanupAfterEachTest(); // TODO: RAII
testInitializer.cleanup();

if (assertCountBefore == assertCountAfter)
{
Expand Down
2 changes: 2 additions & 0 deletions FlashMQTests/maintests.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

class MainTests
{
friend class TestInitializer;

std::unique_ptr<MainAppInThread> mainApp;
std::shared_ptr<ThreadData> dummyThreadData;

Expand Down
29 changes: 29 additions & 0 deletions FlashMQTests/testinitializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "testinitializer.h"

TestInitializer::TestInitializer(MainTests *tests) :
tests(tests)
{

}

void TestInitializer::init()
{
if (!tests)
return;

tests->initBeforeEachTest();
}

void TestInitializer::cleanup()
{
if (!tests)
return;

tests->cleanupAfterEachTest();
tests = nullptr;
}

TestInitializer::~TestInitializer()
{
cleanup();
}
25 changes: 25 additions & 0 deletions FlashMQTests/testinitializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef TESTINITIALIZER_H
#define TESTINITIALIZER_H

#include "maintests.h"

/**
* @brief Simple RAII way to make sure test cleanup is run.
*/
class TestInitializer
{
MainTests *tests = nullptr;

public:
TestInitializer(MainTests *tests);
virtual ~TestInitializer();

TestInitializer(const TestInitializer &other) = delete;
TestInitializer(TestInitializer &&other) = delete;
TestInitializer &operator=(const TestInitializer &other) = delete;

void init();
void cleanup();
};

#endif // TESTINITIALIZER_H

0 comments on commit d4ea583

Please sign in to comment.