1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/*
SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "test_micommand.h"
// SUT
#include <mi/micommand.h>
// Qt
#include <QTest>
#include <QStandardPaths>
class TestCommandHandler : public QObject, public KDevMI::MI::MICommandHandler
{
Q_OBJECT
public:
explicit TestCommandHandler(bool autodelete, int expectedRecordsHandled = -1);
~TestCommandHandler() override;
int recordsHandled() const { return m_recordsHandled; }
public: // MICommandHandler API
void handle(const KDevMI::MI::ResultRecord& record) override;
bool autoDelete() override { return m_autodelete; }
private:
bool m_autodelete;
int m_expectedRecordsHandled = 0;
int m_recordsHandled = 0;
};
TestCommandHandler::TestCommandHandler(bool autodelete, int expectedRecordsHandled)
: m_autodelete(autodelete)
, m_expectedRecordsHandled(expectedRecordsHandled)
{}
TestCommandHandler::~TestCommandHandler()
{
QCOMPARE(m_expectedRecordsHandled, m_recordsHandled);
}
void TestCommandHandler::handle(const KDevMI::MI::ResultRecord& record)
{
QCOMPARE(record.reason, QStringLiteral("reason"));
++m_recordsHandled;
}
class TestCommandResultHandler : public QObject
{
Q_OBJECT
public:
void handleResult(const KDevMI::MI::ResultRecord& record);
int recordsHandled() const { return m_recordsHandled; }
private:
int m_recordsHandled = 0;
};
void TestCommandResultHandler::handleResult(const KDevMI::MI::ResultRecord& record)
{
QCOMPARE(record.reason, QStringLiteral("reason"));
++m_recordsHandled;
}
void TestMICommand::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
}
void TestMICommand::testUserCommand()
{
KDevMI::MI::UserCommand command(KDevMI::MI::NonMI, "command");
command.setToken(1);
// check
QCOMPARE(command.token(), (uint)1);
QCOMPARE(command.frame(), -1);
QCOMPARE(command.thread(), -1);
QCOMPARE(command.isUserCommand(), true);
QCOMPARE(command.handlesError(), false);
QCOMPARE(command.flags(), KDevMI::MI::CommandFlags(KDevMI::MI::CmdMaybeStartsRunning));
QCOMPARE(command.command(), QStringLiteral("command"));
QCOMPARE(command.initialString(), QStringLiteral("1command"));
QCOMPARE(command.cmdToSend(), QStringLiteral("1command\n"));
}
void TestMICommand::testMICommandHandler_data()
{
QTest::addColumn<bool>("autodelete");
QTest::newRow("reusable") << false;
QTest::newRow("burnafterusing") << true;
}
void TestMICommand::testMICommandHandler()
{
QFETCH(bool, autodelete);
KDevMI::MI::UserCommand command(KDevMI::MI::NonMI, "command");
command.setToken(1);
// set handle and invoke
QPointer<TestCommandHandler> commandHandler = new TestCommandHandler(autodelete, 1);
command.setHandler(commandHandler);
KDevMI::MI::ResultRecord resultRecord1("reason");
bool success = command.invokeHandler(resultRecord1);
// check
QVERIFY(success);
QCOMPARE(commandHandler.isNull(), autodelete);
if (!autodelete) {
QCOMPARE(commandHandler->recordsHandled(), 1);
}
// try to invoke again without handler
// ensure handler no longer exists
if (!autodelete) {
delete commandHandler;
}
KDevMI::MI::ResultRecord resultRecord2("reason");
success = command.invokeHandler(resultRecord2);
// check
QVERIFY(!success);
}
void TestMICommand::testQObjectCommandHandler()
{
KDevMI::MI::UserCommand command(KDevMI::MI::NonMI, "command");
command.setToken(1);
// set handle and invoke
auto* resultHandler = new TestCommandResultHandler;
command.setHandler(resultHandler, &TestCommandResultHandler::handleResult);
KDevMI::MI::ResultRecord resultRecord1("reason");
bool success = command.invokeHandler(resultRecord1);
// check
QVERIFY(success);
QCOMPARE(resultHandler->recordsHandled(), 1);
// delete handler and try to invoke again
delete resultHandler;
KDevMI::MI::ResultRecord resultRecord2("reason");
success = command.invokeHandler(resultRecord2);
// check
QVERIFY(!success);
}
QTEST_GUILESS_MAIN(TestMICommand)
#include "test_micommand.moc"
#include "moc_test_micommand.cpp"
|