Changeset View
Changeset View
Standalone View
Standalone View
plugins/debuggercommon/tests/test_micommandqueue.cpp
- This file was added.
1 | /* This file is part of KDevelop | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * Copyright 2018 Friedrich W. H. Kossebau <kossebau@kde.org> | ||||
4 | * | ||||
5 | * This program is free software; you can redistribute it and/or modify | ||||
6 | * it under the terms of the GNU Library General Public License as | ||||
7 | * published by the Free Software Foundation; either version 2 of the | ||||
8 | * License, or (at your option) any later version. | ||||
9 | * | ||||
10 | * This program is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
13 | * GNU General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU Library General Public | ||||
16 | * License along with this program; if not, write to the | ||||
17 | * Free Software Foundation, Inc., | ||||
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "test_micommandqueue.h" | ||||
22 | | ||||
23 | // SUT | ||||
24 | #include <mi/micommandqueue.h> | ||||
25 | #include <mi/micommand.h> | ||||
26 | // Qt | ||||
27 | #include <QTest> | ||||
28 | #include <QSignalSpy> | ||||
29 | | ||||
30 | Q_DECLARE_METATYPE(KDevMI::MI::CommandFlags) | ||||
31 | | ||||
32 | class TestDummyCommand : public QObject, public KDevMI::MI::MICommand | ||||
33 | { | ||||
34 | Q_OBJECT | ||||
35 | public: | ||||
36 | TestDummyCommand(KDevMI::MI::CommandType type, const QString& args = QString(), | ||||
37 | KDevMI::MI::CommandFlags flags = {}); | ||||
38 | }; | ||||
39 | | ||||
40 | TestDummyCommand::TestDummyCommand(KDevMI::MI::CommandType type, const QString& args, | ||||
41 | KDevMI::MI::CommandFlags flags) | ||||
42 | : KDevMI::MI::MICommand(type, args, flags) | ||||
43 | { | ||||
44 | } | ||||
45 | | ||||
46 | void TestMICommandQueue::testConstructor() | ||||
47 | { | ||||
48 | KDevMI::MI::CommandQueue commandQueue; | ||||
49 | | ||||
50 | // check | ||||
51 | QCOMPARE(commandQueue.count(), 0); | ||||
52 | QCOMPARE(commandQueue.isEmpty(), true); | ||||
53 | QCOMPARE(commandQueue.haveImmediateCommand(), false); | ||||
54 | } | ||||
55 | | ||||
56 | void TestMICommandQueue::testDestructor() | ||||
57 | { | ||||
58 | auto* commandQueue = new KDevMI::MI::CommandQueue; | ||||
59 | | ||||
60 | // prepare | ||||
61 | auto* command1 = new TestDummyCommand(KDevMI::MI::NonMI, QString(), KDevMI::MI::CmdImmediately); | ||||
62 | auto* command2 = new TestDummyCommand(KDevMI::MI::NonMI, QString(), {}); | ||||
63 | | ||||
64 | QSignalSpy command1Spy(command1, &QObject::destroyed); | ||||
65 | QSignalSpy command2Spy(command2, &QObject::destroyed); | ||||
66 | | ||||
67 | commandQueue->enqueue(command1); | ||||
68 | commandQueue->enqueue(command2); | ||||
69 | | ||||
70 | // execute | ||||
71 | delete commandQueue; | ||||
72 | | ||||
73 | // check | ||||
74 | QCOMPARE(command1Spy.count(), 1); | ||||
75 | QCOMPARE(command2Spy.count(), 1); | ||||
76 | } | ||||
77 | | ||||
78 | void TestMICommandQueue::addAndTake_data() | ||||
79 | { | ||||
80 | QTest::addColumn<KDevMI::MI::CommandFlags>("flags"); | ||||
81 | QTest::addColumn<bool>("isImmediate"); | ||||
82 | | ||||
83 | QTest::newRow("none") | ||||
84 | << KDevMI::MI::CommandFlags() << false; | ||||
85 | QTest::newRow("MaybeStartsRunning") | ||||
86 | << KDevMI::MI::CommandFlags(KDevMI::MI::CmdMaybeStartsRunning) << false; | ||||
87 | QTest::newRow("Immediately") | ||||
88 | << KDevMI::MI::CommandFlags(KDevMI::MI::CmdImmediately) << true; | ||||
89 | QTest::newRow("Interrupt") | ||||
90 | << KDevMI::MI::CommandFlags(KDevMI::MI::CmdInterrupt) << true; | ||||
91 | } | ||||
92 | | ||||
93 | void TestMICommandQueue::addAndTake() | ||||
94 | { | ||||
95 | QFETCH(KDevMI::MI::CommandFlags, flags); | ||||
96 | QFETCH(bool, isImmediate); | ||||
97 | | ||||
98 | KDevMI::MI::CommandQueue commandQueue; | ||||
99 | | ||||
100 | auto* command = new TestDummyCommand(KDevMI::MI::NonMI, QString(), flags); | ||||
101 | | ||||
102 | // add | ||||
103 | commandQueue.enqueue(command); | ||||
104 | // check | ||||
105 | QVERIFY(command->token() != 0); | ||||
106 | QCOMPARE(commandQueue.count(), 1); | ||||
107 | QCOMPARE(commandQueue.isEmpty(), false); | ||||
108 | QCOMPARE(commandQueue.haveImmediateCommand(), isImmediate); | ||||
109 | | ||||
110 | // take | ||||
111 | auto* nextCommand = commandQueue.nextCommand(); | ||||
112 | // check | ||||
113 | QCOMPARE(nextCommand, command); | ||||
114 | QVERIFY(nextCommand->token() != 0); | ||||
115 | QCOMPARE(commandQueue.count(), 0); | ||||
116 | QCOMPARE(commandQueue.isEmpty(), true); | ||||
117 | QCOMPARE(commandQueue.haveImmediateCommand(), false); | ||||
118 | } | ||||
119 | | ||||
120 | void TestMICommandQueue::clearQueue() | ||||
121 | { | ||||
122 | KDevMI::MI::CommandQueue commandQueue; | ||||
123 | | ||||
124 | // prepare | ||||
125 | auto* command1 = new TestDummyCommand(KDevMI::MI::NonMI, QString(), KDevMI::MI::CmdImmediately); | ||||
126 | auto* command2 = new TestDummyCommand(KDevMI::MI::NonMI, QString(), {}); | ||||
127 | | ||||
128 | QSignalSpy command1Spy(command1, &QObject::destroyed); | ||||
129 | QSignalSpy command2Spy(command2, &QObject::destroyed); | ||||
130 | | ||||
131 | commandQueue.enqueue(command1); | ||||
132 | commandQueue.enqueue(command2); | ||||
133 | | ||||
134 | // execute | ||||
135 | commandQueue.clear(); | ||||
136 | | ||||
137 | // check | ||||
138 | QCOMPARE(commandQueue.count(), 0); | ||||
139 | QCOMPARE(commandQueue.isEmpty(), true); | ||||
140 | QCOMPARE(commandQueue.haveImmediateCommand(), false); | ||||
141 | QCOMPARE(command1Spy.count(), 1); | ||||
142 | QCOMPARE(command2Spy.count(), 1); | ||||
143 | } | ||||
144 | | ||||
145 | QTEST_GUILESS_MAIN(TestMICommandQueue) | ||||
146 | | ||||
147 | #include "test_micommandqueue.moc" |