Skip to content

Commit

Permalink
Timer tests (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaspar-Metsa authored Feb 2, 2024
1 parent e8c02bf commit a53f3d6
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cmake-build-debug/
winbuild/
build/
out/
Expand Down
2 changes: 1 addition & 1 deletion src/timers/CTimers.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CTimers final : public QObject
std::list<TTimer> m_countdowns;
QTimer m_timer;

private:
public:
std::string getTimers();
std::string getCountdowns();

Expand Down
19 changes: 19 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,22 @@ set_target_properties(
UNITY_BUILD ${USE_UNITY_BUILD}
)
add_test(NAME TestAdventure COMMAND TestAdventure)

# Timer
set(timer_qq
../src/timers/CTimers.cpp
../src/timers/CTimers.h
)
set(TestTimerQQ TestCTimers.cpp)
add_executable(TestTimer ${timer_qq} ${TestTimerQQ})
add_dependencies(TestTimer glm)
target_link_libraries(TestTimer Qt5::Test coverage_config)
set_target_properties(
TestTimer PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
COMPILE_FLAGS "${WARNING_FLAGS}"
UNITY_BUILD ${USE_UNITY_BUILD}
)
add_test(NAME TestTimer COMMAND TestTimer)
128 changes: 128 additions & 0 deletions tests/TestCTimers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2023 The MMapper Authors

#include "TestCTimers.h"

#include <QtTest/QtTest>

#include "../src/timers/CTimers.h"

TestCTimers::TestCTimers() = default;

TestCTimers::~TestCTimers() = default;

void TestCTimers::testAddRemoveTimer()
{
CTimers timers(nullptr);
QString timerName = "TestTimer";
QString timerDesc = "Test Description";

timers.addTimer(timerName.toStdString(), timerDesc.toStdString());

// Verify the added timer is present
QString timersList = QString::fromStdString(timers.getTimers());
QVERIFY(timersList.contains(timerName));
QVERIFY(timersList.contains(timerDesc));

QVERIFY(timers.removeTimer(timerName.toStdString()));

// Verify the timer is removed
timersList = QString::fromStdString(timers.getTimers());
QVERIFY(!timersList.contains(timerName));
}

void TestCTimers::testAddRemoveCountdown()
{
CTimers timers(nullptr);
QString countdownName = "TestCountdown";
QString countdownDesc = "Test Countdown Description";
int64_t countdownTimeMs = 10000; // 10 seconds

timers.addCountdown(countdownName.toStdString(), countdownDesc.toStdString(), countdownTimeMs);

// Verify the added countdown is added
QString countdownsList = QString::fromStdString(timers.getCountdowns());
QVERIFY(countdownsList.contains(countdownName));
QVERIFY(countdownsList.contains(countdownDesc));

QVERIFY(timers.removeCountdown(countdownName.toStdString()));

// Verify the countdown is removed
countdownsList = QString::fromStdString(timers.getCountdowns());
QVERIFY(!countdownsList.contains(countdownName));
}

void TestCTimers::testElapsedTime()
{
CTimers timers(nullptr);
QString timerName = "ElapsedTimeTestTimer";
QString timerDesc = "Elapsed Time Test Description";

timers.addTimer(timerName.toStdString(), timerDesc.toStdString());

QString timersList = QString::fromStdString(timers.getTimers());
QVERIFY(timersList.contains("up for - 0:00"));

timers.removeTimer(timerName.toStdString());
}

void TestCTimers::testCountdownCompletion()
{
CTimers timers(nullptr);
QString countdownName = "CompletionTestCountdown";
QString countdownDesc = "Countdown Completion Test";
int64_t countdownTimeMs = 10000; // 10 seconds

timers.addCountdown(countdownName.toStdString(), countdownDesc.toStdString(), countdownTimeMs);
QString countdownsListBefore = QString::fromStdString(timers.getCountdowns());
QVERIFY(countdownsListBefore.contains(countdownName)); // Verify the added countdown is present
QString countdownsList = QString::fromStdString(timers.getCountdowns());
QVERIFY(countdownsList.contains("(up for - 0:00, left - 0:10)"));
}

void TestCTimers::testClearFunctionality()
{
CTimers timers(nullptr);

// Add multiple timers and countdowns
timers.addTimer("Timer1", "Description1");
timers.addTimer("Timer2", "Description1");
timers.addCountdown("Countdown1", "Description1", 5000);
timers.addCountdown("Countdown2", "Description2", 5000);

// Clear all timers and countdowns
timers.clear();

// Verify all timers and countdowns are cleared
QVERIFY(QString::fromStdString(timers.getTimers()).isEmpty());
QVERIFY(QString::fromStdString(timers.getCountdowns()).isEmpty());
}

void TestCTimers::testMultipleTimersAndCountdowns()
{
CTimers timers(nullptr);

// Add multiple timers
timers.addTimer("Timer1", "Description1");
timers.addTimer("Timer2", "Description2");

// Add multiple countdowns
timers.addCountdown("Countdown1", "Description1", 5000);
timers.addCountdown("Countdown2", "Description2", 10000);

// Verify timers and countdowns are added
QVERIFY(!QString::fromStdString(timers.getTimers()).isEmpty());
QVERIFY(!QString::fromStdString(timers.getCountdowns()).isEmpty());

// Remove a timer and a countdown
QVERIFY(timers.removeTimer("Timer1"));
QVERIFY(timers.removeCountdown("Countdown1"));

// Verify removal
QString timersList = QString::fromStdString(timers.getTimers());
QString countdownsList = QString::fromStdString(timers.getCountdowns());
QVERIFY(!timersList.contains("Timer1"));
QVERIFY(!countdownsList.contains("Countdown1"));
}

QTEST_MAIN(TestCTimers)
22 changes: 22 additions & 0 deletions tests/TestCTimers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2023 The MMapper Authors

#include <QObject>

class TestCTimers final : public QObject
{
Q_OBJECT
public:
TestCTimers();
~TestCTimers() final;

private:
private Q_SLOTS:
void testAddRemoveTimer();
void testAddRemoveCountdown();
void testElapsedTime();
void testCountdownCompletion();
void testClearFunctionality();
void testMultipleTimersAndCountdowns();
};

0 comments on commit a53f3d6

Please sign in to comment.