File indexing completed on 2024-05-19 15:45:31
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_micommandqueue.h" 0008 0009 // SUT 0010 #include <mi/micommandqueue.h> 0011 #include <mi/micommand.h> 0012 // Qt 0013 #include <QTest> 0014 #include <QSignalSpy> 0015 #include <QStandardPaths> 0016 0017 Q_DECLARE_METATYPE(KDevMI::MI::CommandFlags) 0018 0019 class TestDummyCommand : public QObject, public KDevMI::MI::MICommand 0020 { 0021 Q_OBJECT 0022 public: 0023 TestDummyCommand(KDevMI::MI::CommandType type, const QString& args = QString(), 0024 KDevMI::MI::CommandFlags flags = {}); 0025 }; 0026 0027 TestDummyCommand::TestDummyCommand(KDevMI::MI::CommandType type, const QString& args, 0028 KDevMI::MI::CommandFlags flags) 0029 : KDevMI::MI::MICommand(type, args, flags) 0030 { 0031 } 0032 0033 void TestMICommandQueue::initTestCase() 0034 { 0035 QStandardPaths::setTestModeEnabled(true); 0036 } 0037 0038 void TestMICommandQueue::testConstructor() 0039 { 0040 KDevMI::MI::CommandQueue commandQueue; 0041 0042 // check 0043 QCOMPARE(commandQueue.count(), 0); 0044 QCOMPARE(commandQueue.isEmpty(), true); 0045 QCOMPARE(commandQueue.haveImmediateCommand(), false); 0046 } 0047 0048 void TestMICommandQueue::testDestructor() 0049 { 0050 auto* commandQueue = new KDevMI::MI::CommandQueue; 0051 0052 // prepare 0053 auto command1 = std::make_unique<TestDummyCommand>(KDevMI::MI::NonMI, QString(), KDevMI::MI::CmdImmediately); 0054 auto command2 = std::make_unique<TestDummyCommand>(KDevMI::MI::NonMI, QString(), KDevMI::MI::CommandFlags {}); 0055 0056 QSignalSpy command1Spy(command1.get(), &QObject::destroyed); 0057 QSignalSpy command2Spy(command2.get(), &QObject::destroyed); 0058 0059 commandQueue->enqueue(std::move(command1)); 0060 commandQueue->enqueue(std::move(command2)); 0061 0062 // execute 0063 delete commandQueue; 0064 0065 // check 0066 QCOMPARE(command1Spy.count(), 1); 0067 QCOMPARE(command2Spy.count(), 1); 0068 } 0069 0070 void TestMICommandQueue::addAndTake_data() 0071 { 0072 QTest::addColumn<KDevMI::MI::CommandFlags>("flags"); 0073 QTest::addColumn<bool>("isImmediate"); 0074 0075 QTest::newRow("none") 0076 << KDevMI::MI::CommandFlags() << false; 0077 QTest::newRow("MaybeStartsRunning") 0078 << KDevMI::MI::CommandFlags(KDevMI::MI::CmdMaybeStartsRunning) << false; 0079 QTest::newRow("Immediately") 0080 << KDevMI::MI::CommandFlags(KDevMI::MI::CmdImmediately) << true; 0081 QTest::newRow("Interrupt") 0082 << KDevMI::MI::CommandFlags(KDevMI::MI::CmdInterrupt) << true; 0083 } 0084 0085 void TestMICommandQueue::addAndTake() 0086 { 0087 QFETCH(KDevMI::MI::CommandFlags, flags); 0088 QFETCH(bool, isImmediate); 0089 0090 KDevMI::MI::CommandQueue commandQueue; 0091 0092 auto command = std::make_unique<TestDummyCommand>(KDevMI::MI::NonMI, QString(), flags); 0093 auto c = command.get(); 0094 0095 // add 0096 commandQueue.enqueue(std::move(command)); 0097 // check 0098 QVERIFY(c->token() != 0); 0099 QCOMPARE(commandQueue.count(), 1); 0100 QCOMPARE(commandQueue.isEmpty(), false); 0101 QCOMPARE(commandQueue.haveImmediateCommand(), isImmediate); 0102 0103 // take 0104 auto nextCommand = commandQueue.nextCommand(); 0105 // check 0106 QCOMPARE(nextCommand.get(), c); 0107 QVERIFY(nextCommand->token() != 0); 0108 QCOMPARE(commandQueue.count(), 0); 0109 QCOMPARE(commandQueue.isEmpty(), true); 0110 QCOMPARE(commandQueue.haveImmediateCommand(), false); 0111 } 0112 0113 void TestMICommandQueue::clearQueue() 0114 { 0115 KDevMI::MI::CommandQueue commandQueue; 0116 0117 // prepare 0118 auto command1 = std::make_unique<TestDummyCommand>(KDevMI::MI::NonMI, QString(), KDevMI::MI::CmdImmediately); 0119 auto command2 = std::make_unique<TestDummyCommand>(KDevMI::MI::NonMI, QString(), KDevMI::MI::CommandFlags {}); 0120 0121 QSignalSpy command1Spy(command1.get(), &QObject::destroyed); 0122 QSignalSpy command2Spy(command2.get(), &QObject::destroyed); 0123 0124 commandQueue.enqueue(std::move(command1)); 0125 commandQueue.enqueue(std::move(command2)); 0126 0127 // execute 0128 commandQueue.clear(); 0129 0130 // check 0131 QCOMPARE(commandQueue.count(), 0); 0132 QCOMPARE(commandQueue.isEmpty(), true); 0133 QCOMPARE(commandQueue.haveImmediateCommand(), false); 0134 QCOMPARE(command1Spy.count(), 1); 0135 QCOMPARE(command2Spy.count(), 1); 0136 } 0137 0138 QTEST_GUILESS_MAIN(TestMICommandQueue) 0139 0140 #include "test_micommandqueue.moc" 0141 #include "moc_test_micommandqueue.cpp"