-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Tests for Room Manager 2. Fixed sometimes failing test in Timer 3. Fixed Warning message during build
- Loading branch information
1 parent
5122f93
commit 6433e4a
Showing
10 changed files
with
419 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ Makefile | |
.vs | ||
.vscode | ||
CMakeSettings.json | ||
cmake-build-debug/ | ||
|
||
|
||
# snapcraft | ||
parts/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
// Copyright (C) 2023 The MMapper Authors | ||
|
||
#include "TestRoomManager.h" | ||
|
||
#include <QtTest> | ||
|
||
#include "proxy/GmcpMessage.h" | ||
#include "roompanel/RoomManager.h" | ||
|
||
TestRoomManager::TestRoomManager(RoomManager &manager) | ||
: manager(manager) | ||
{} | ||
|
||
TestRoomManager::~TestRoomManager() = default; | ||
|
||
void TestRoomManager::init() {} | ||
|
||
void TestRoomManager::cleanup() {} | ||
|
||
QJsonObject gmcpRoomCharsAddObj = {{"desc", "A magpie is flying around looking for some food."}, | ||
{"flags", QJsonArray()}, | ||
{"id", 2}, | ||
{"labels", QJsonArray()}, | ||
{"name", "mystérieuse créature"}, | ||
{"position", "standing"}, | ||
{"type", "npc"}}; | ||
|
||
void TestRoomManager::testSlotReset() | ||
{ | ||
QVERIFY(!manager.getRoom().isIdPresent(2)); | ||
|
||
QString jsonStr = QJsonDocument(gmcpRoomCharsAddObj).toJson(QJsonDocument::Compact); | ||
GmcpMessage addMessage(GmcpMessageTypeEnum::ROOM_CHARS_ADD, jsonStr); | ||
|
||
manager.slot_parseGmcpInput(addMessage); | ||
QVERIFY(manager.getRoom().isIdPresent(2)); // Check if mob with ID 2 is present | ||
|
||
manager.slot_reset(); | ||
QVERIFY(!manager.getRoom().isIdPresent(2)); // Verify ID 2 is not present after reset | ||
} | ||
|
||
void TestRoomManager::testParseGmcpAddValidMessage() | ||
{ | ||
QString jsonStr = QJsonDocument(gmcpRoomCharsAddObj).toJson(QJsonDocument::Compact); | ||
GmcpMessage addMessage(GmcpMessageTypeEnum::ROOM_CHARS_ADD, jsonStr); | ||
|
||
QSignalSpy updateWidgetSpy(&manager, &RoomManager::sig_updateWidget); | ||
manager.slot_parseGmcpInput(addMessage); | ||
QCOMPARE(updateWidgetSpy.count(), 1); | ||
QVERIFY(manager.getRoom().isIdPresent(2)); // Verify mob was added correctly | ||
} | ||
|
||
void TestRoomManager::testParseGmcpInvalidMessage() | ||
{ | ||
// Create an invalid GMCP message (e.g., missing required fields) | ||
QJsonObject invalidObj = {{"invalidField", "value"}}; | ||
QString jsonStr = QJsonDocument(invalidObj).toJson(QJsonDocument::Compact); | ||
GmcpMessage invalidMessage(GmcpMessageTypeEnum::ROOM_CHARS_ADD, jsonStr); | ||
|
||
// Attempt to parse the invalid message | ||
QSignalSpy updateWidgetSpy(&manager, &RoomManager::sig_updateWidget); | ||
manager.slot_parseGmcpInput(invalidMessage); | ||
|
||
// Verify that the widget update signal was not emitted and no mobs were added | ||
QCOMPARE(updateWidgetSpy.count(), 0); | ||
} | ||
|
||
void TestRoomManager::testParseGmcpUpdateValidMessage() | ||
{ | ||
// Step 1: Add a mob to ensure there's something to update | ||
QJsonObject addObj = {{"id", 2}, {"name", "male magpie"}, {"position", "standing"}}; | ||
QString addJsonStr = QJsonDocument(addObj).toJson(QJsonDocument::Compact); | ||
GmcpMessage addMessage(GmcpMessageTypeEnum::ROOM_CHARS_ADD, addJsonStr); | ||
manager.slot_parseGmcpInput(addMessage); | ||
QVERIFY(manager.getRoom().isIdPresent(2)); // Verify mob was added correctly | ||
|
||
// Step 2: Create an update message for the same mob with new information | ||
QJsonObject updateObj = {{"id", 2}, {"name", "angry male magpie"}, {"position", "sleeping"}}; | ||
QString updateJsonStr = QJsonDocument(updateObj).toJson(QJsonDocument::Compact); | ||
GmcpMessage updateMessage(GmcpMessageTypeEnum::ROOM_CHARS_UPDATE, updateJsonStr); | ||
|
||
// Prepare to capture the sig_updateWidget signal | ||
QSignalSpy updateWidgetSpy(&manager, &RoomManager::sig_updateWidget); | ||
|
||
// Step 3: Send the update message | ||
manager.slot_parseGmcpInput(updateMessage); | ||
|
||
// Verify that the sig_updateWidget signal was emitted exactly once | ||
QCOMPARE(updateWidgetSpy.count(), 1); | ||
|
||
// Step 4: Verify that the mob's information was updated correctly | ||
auto updatedMob = manager.getRoom().getMobById(2); | ||
// Ensure the mob exists, using the fact that shared_ptr evaluates to true if | ||
// it points to something | ||
QVERIFY(updatedMob); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
QCoreApplication app(argc, argv); | ||
|
||
RoomManager manager(nullptr); | ||
TestRoomManager test(manager); | ||
|
||
return QTest::qExec(&test, argc, argv); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
// Copyright (C) 2023 The MMapper Authors | ||
|
||
#include "roompanel/RoomManager.h" | ||
#include <QObject> | ||
|
||
class TestRoomManager final : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit TestRoomManager(RoomManager &manager); | ||
~TestRoomManager() final; | ||
|
||
private: | ||
private Q_SLOTS: | ||
void init(); | ||
void cleanup(); | ||
void testSlotReset(); | ||
void testParseGmcpAddValidMessage(); | ||
void testParseGmcpInvalidMessage(); | ||
void testParseGmcpUpdateValidMessage(); | ||
|
||
private: | ||
RoomManager &manager; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
// Copyright (C) 2023 The MMapper Authors | ||
|
||
#include "TestRoomMob.h" | ||
|
||
#include <QtTest> | ||
|
||
#include "roompanel/RoomMob.h" | ||
|
||
TestRoomMob::TestRoomMob() = default; | ||
|
||
TestRoomMob::~TestRoomMob() = default; | ||
|
||
void TestRoomMob::testInitialization() | ||
{ | ||
RoomMobData mobData; | ||
QCOMPARE(mobData.getId(), RoomMobData::NOID); | ||
|
||
// Verify that all fields are initialized to empty QVariant | ||
for (uint8_t i = 0; i < NUM_MOB_FIELDS; ++i) { | ||
QVERIFY(mobData.getField(static_cast<MobFieldEnum>(i)).isNull()); | ||
} | ||
} | ||
|
||
void TestRoomMob::testSetGetId() | ||
{ | ||
RoomMobData mobData; | ||
RoomMobData::Id testId = 123; | ||
mobData.setId(testId); | ||
QCOMPARE(mobData.getId(), testId); | ||
} | ||
|
||
void TestRoomMob::testSetGetField() | ||
{ | ||
// Create an instance of RoomMobData to test | ||
RoomMobData mobData; | ||
|
||
// Prepare a test value to set for the mob's name field | ||
QVariant testValue = QVariant::fromValue(QString("MobName")); | ||
|
||
// Set the NAME field of mobData to the test value | ||
mobData.setField(MobFieldEnum::NAME, testValue); | ||
|
||
// Assert that the value retrieved from the NAME field is equal to the test value. | ||
// This checks if the setField and getField methods work correctly. | ||
QCOMPARE(mobData.getField(MobFieldEnum::NAME), testValue); | ||
} | ||
|
||
void TestRoomMob::testAllocAndUpdate() | ||
{ | ||
auto roomMob = RoomMob::alloc(); | ||
QVERIFY(roomMob != nullptr); | ||
|
||
RoomMobUpdate update; | ||
update.setId(roomMob->getId()); | ||
QVariant newValue = QVariant::fromValue(QString("SomeMobName")); | ||
update.setField(MobFieldEnum::NAME, newValue); | ||
update.setFlags(RoomMobUpdate::Flags(MobFieldEnum::NAME)); | ||
|
||
bool updated = roomMob->updateFrom(std::move(update)); | ||
QVERIFY(updated); | ||
QCOMPARE(roomMob->getField(MobFieldEnum::NAME), newValue); | ||
} | ||
|
||
void TestRoomMob::testFlagsAndFields() | ||
{ | ||
RoomMobUpdate update; | ||
|
||
update.setFlags(MobFieldFlags(MobFieldEnum::NAME)); // Directly set using constructor | ||
|
||
QVERIFY(update.getFlags().contains(MobFieldEnum::NAME)); | ||
QVERIFY(!update.getFlags().contains(MobFieldEnum::DESC)); | ||
|
||
QVariant testNameValue = QVariant::fromValue(QString("TestName")); | ||
update.setField(MobFieldEnum::NAME, testNameValue); | ||
|
||
// Verify the field is correctly updated | ||
QCOMPARE(update.getField(MobFieldEnum::NAME), testNameValue); | ||
} | ||
|
||
QTEST_MAIN(TestRoomMob) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
// Copyright (C) 2023 The MMapper Authors | ||
|
||
#include <QObject> | ||
|
||
class TestRoomMob final : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
TestRoomMob(); | ||
~TestRoomMob() final; | ||
|
||
private: | ||
private Q_SLOTS: | ||
void testInitialization(); | ||
void testSetGetId(); | ||
void testSetGetField(); | ||
void testAllocAndUpdate(); | ||
void testFlagsAndFields(); | ||
}; |
Oops, something went wrong.