File indexing completed on 2024-05-12 04:39:45
0001 /* 0002 SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "test_micommand.h" 0008 0009 // SUT 0010 #include <mi/micommand.h> 0011 // Qt 0012 #include <QTest> 0013 #include <QStandardPaths> 0014 0015 class TestCommandHandler : public QObject, public KDevMI::MI::MICommandHandler 0016 { 0017 Q_OBJECT 0018 public: 0019 explicit TestCommandHandler(bool autodelete, int expectedRecordsHandled = -1); 0020 ~TestCommandHandler() override; 0021 int recordsHandled() const { return m_recordsHandled; } 0022 public: // MICommandHandler API 0023 void handle(const KDevMI::MI::ResultRecord& record) override; 0024 bool autoDelete() override { return m_autodelete; } 0025 private: 0026 bool m_autodelete; 0027 int m_expectedRecordsHandled = 0; 0028 int m_recordsHandled = 0; 0029 }; 0030 0031 TestCommandHandler::TestCommandHandler(bool autodelete, int expectedRecordsHandled) 0032 : m_autodelete(autodelete) 0033 , m_expectedRecordsHandled(expectedRecordsHandled) 0034 {} 0035 0036 TestCommandHandler::~TestCommandHandler() 0037 { 0038 QCOMPARE(m_expectedRecordsHandled, m_recordsHandled); 0039 } 0040 0041 void TestCommandHandler::handle(const KDevMI::MI::ResultRecord& record) 0042 { 0043 QCOMPARE(record.reason, QStringLiteral("reason")); 0044 0045 ++m_recordsHandled; 0046 } 0047 0048 0049 0050 class TestCommandResultHandler : public QObject 0051 { 0052 Q_OBJECT 0053 public: 0054 void handleResult(const KDevMI::MI::ResultRecord& record); 0055 int recordsHandled() const { return m_recordsHandled; } 0056 private: 0057 int m_recordsHandled = 0; 0058 }; 0059 0060 void TestCommandResultHandler::handleResult(const KDevMI::MI::ResultRecord& record) 0061 { 0062 QCOMPARE(record.reason, QStringLiteral("reason")); 0063 0064 ++m_recordsHandled; 0065 } 0066 0067 void TestMICommand::initTestCase() 0068 { 0069 QStandardPaths::setTestModeEnabled(true); 0070 } 0071 0072 void TestMICommand::testUserCommand() 0073 { 0074 KDevMI::MI::UserCommand command(KDevMI::MI::NonMI, "command"); 0075 command.setToken(1); 0076 0077 // check 0078 QCOMPARE(command.token(), (uint)1); 0079 QCOMPARE(command.frame(), -1); 0080 QCOMPARE(command.thread(), -1); 0081 QCOMPARE(command.isUserCommand(), true); 0082 QCOMPARE(command.handlesError(), false); 0083 QCOMPARE(command.flags(), KDevMI::MI::CommandFlags(KDevMI::MI::CmdMaybeStartsRunning)); 0084 QCOMPARE(command.command(), QStringLiteral("command")); 0085 QCOMPARE(command.initialString(), QStringLiteral("1command")); 0086 QCOMPARE(command.cmdToSend(), QStringLiteral("1command\n")); 0087 } 0088 0089 void TestMICommand::testMICommandHandler_data() 0090 { 0091 QTest::addColumn<bool>("autodelete"); 0092 0093 QTest::newRow("reusable") << false; 0094 QTest::newRow("burnafterusing") << true; 0095 } 0096 0097 void TestMICommand::testMICommandHandler() 0098 { 0099 QFETCH(bool, autodelete); 0100 0101 KDevMI::MI::UserCommand command(KDevMI::MI::NonMI, "command"); 0102 command.setToken(1); 0103 0104 // set handle and invoke 0105 QPointer<TestCommandHandler> commandHandler = new TestCommandHandler(autodelete, 1); 0106 command.setHandler(commandHandler); 0107 0108 KDevMI::MI::ResultRecord resultRecord1("reason"); 0109 bool success = command.invokeHandler(resultRecord1); 0110 0111 // check 0112 QVERIFY(success); 0113 QCOMPARE(commandHandler.isNull(), autodelete); 0114 if (!autodelete) { 0115 QCOMPARE(commandHandler->recordsHandled(), 1); 0116 } 0117 0118 // try to invoke again without handler 0119 // ensure handler no longer exists 0120 if (!autodelete) { 0121 delete commandHandler; 0122 } 0123 0124 KDevMI::MI::ResultRecord resultRecord2("reason"); 0125 success = command.invokeHandler(resultRecord2); 0126 0127 // check 0128 QVERIFY(!success); 0129 } 0130 0131 void TestMICommand::testQObjectCommandHandler() 0132 { 0133 KDevMI::MI::UserCommand command(KDevMI::MI::NonMI, "command"); 0134 command.setToken(1); 0135 0136 // set handle and invoke 0137 auto* resultHandler = new TestCommandResultHandler; 0138 command.setHandler(resultHandler, &TestCommandResultHandler::handleResult); 0139 0140 KDevMI::MI::ResultRecord resultRecord1("reason"); 0141 bool success = command.invokeHandler(resultRecord1); 0142 0143 // check 0144 QVERIFY(success); 0145 QCOMPARE(resultHandler->recordsHandled(), 1); 0146 0147 // delete handler and try to invoke again 0148 delete resultHandler; 0149 0150 KDevMI::MI::ResultRecord resultRecord2("reason"); 0151 success = command.invokeHandler(resultRecord2); 0152 0153 // check 0154 QVERIFY(!success); 0155 } 0156 0157 0158 QTEST_GUILESS_MAIN(TestMICommand) 0159 0160 #include "test_micommand.moc" 0161 #include "moc_test_micommand.cpp"