File indexing completed on 2024-05-05 03:53:33

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2005 Thomas Braxton <brax108@cox.net>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kcompletioncoretest.h"
0009 #include <QSignalSpy>
0010 #include <QTest>
0011 #define clampet strings[0]
0012 #define coolcat strings[1]
0013 #define carpet strings[2]
0014 #define carp strings[3]
0015 #define wclampet wstrings[0]
0016 #define wcoolcat wstrings[1]
0017 #define wcarpet wstrings[2]
0018 #define wcarp wstrings[3]
0019 
0020 void Test_KCompletion::initTestCase()
0021 {
0022     strings = QStringList{QStringLiteral("clampet@test.org"), //
0023                           QStringLiteral("coolcat@test.org"),
0024                           QStringLiteral("carpet@test.org"),
0025                           QStringLiteral("carp@test.org")};
0026     wstrings = QStringList{QStringLiteral("clampet@test.org:30"),
0027                            QStringLiteral("coolcat@test.org:20"),
0028                            QStringLiteral("carpet@test.org:40"),
0029                            QStringLiteral("carp@test.org:7")};
0030     qRegisterMetaType<QStringList>("QStringList");
0031 }
0032 
0033 void Test_KCompletion::isEmpty()
0034 {
0035     KCompletion completion;
0036     QVERIFY(completion.isEmpty());
0037     QVERIFY(completion.items().isEmpty());
0038 }
0039 
0040 void Test_KCompletion::insertionOrder()
0041 {
0042     KCompletion completion;
0043     completion.setSoundsEnabled(false);
0044     QSignalSpy spy1(&completion, &KCompletion::match);
0045     QSignalSpy spy3(&completion, &KCompletion::multipleMatches);
0046 
0047     completion.setOrder(KCompletion::Insertion);
0048     QVERIFY(completion.order() == KCompletion::Insertion);
0049 
0050     completion.setItems(strings);
0051     QVERIFY(completion.items().count() == strings.count());
0052 
0053     completion.setCompletionMode(KCompletion::CompletionShell);
0054     QCOMPARE(completion.makeCompletion(QStringLiteral("ca")), QStringLiteral("carp"));
0055     QVERIFY(spy1.count() == 1);
0056     QVERIFY(spy1.takeFirst().at(0).toString() == QLatin1String("carp"));
0057     QVERIFY(spy3.count() == 1);
0058     spy3.takeFirst();
0059 
0060     QSignalSpy spy2(&completion, &KCompletion::matches);
0061     completion.makeCompletion(QStringLiteral("ca"));
0062     QCOMPARE(spy2.count(), 1);
0063     QVERIFY(spy3.count() == 0); // shouldn't be signaled on 2nd call
0064     QStringList matches = spy2.takeFirst().at(0).toStringList();
0065     QVERIFY(matches.count() == 2);
0066     QCOMPARE(matches[0], carpet);
0067     QCOMPARE(matches[1], carp);
0068 
0069     completion.setCompletionMode(KCompletion::CompletionAuto);
0070     QCOMPARE(completion.makeCompletion(QStringLiteral("ca")), carpet);
0071     QVERIFY(spy1.count() == 1);
0072     QVERIFY(spy1.takeFirst().at(0).toString() == carpet);
0073 }
0074 
0075 void Test_KCompletion::sortedOrder()
0076 {
0077     KCompletion completion;
0078     completion.setSoundsEnabled(false);
0079     QSignalSpy spy1(&completion, &KCompletion::match);
0080     QSignalSpy spy3(&completion, &KCompletion::multipleMatches);
0081 
0082     completion.setOrder(KCompletion::Sorted);
0083     QVERIFY(completion.order() == KCompletion::Sorted);
0084 
0085     completion.setItems(strings);
0086     QVERIFY(completion.items().count() == 4);
0087 
0088     completion.setCompletionMode(KCompletion::CompletionShell);
0089     QCOMPARE(completion.makeCompletion(QStringLiteral("ca")), QStringLiteral("carp"));
0090     QVERIFY(spy1.count() == 1);
0091     QCOMPARE(spy1.takeFirst().at(0).toString(), QStringLiteral("carp"));
0092     QVERIFY(spy3.count() == 1);
0093     spy3.takeFirst();
0094 
0095     QSignalSpy spy2(&completion, &KCompletion::matches);
0096     completion.makeCompletion(QStringLiteral("ca"));
0097     QCOMPARE(spy2.count(), 1);
0098     QVERIFY(spy3.count() == 0); // shouldn't be signaled on 2nd call
0099 
0100     QStringList matches = spy2.takeFirst().at(0).toStringList();
0101     QVERIFY(matches.count() == 2);
0102     QCOMPARE(matches[0], carp);
0103     QCOMPARE(matches[1], carpet);
0104 
0105     completion.setCompletionMode(KCompletion::CompletionAuto);
0106     QCOMPARE(completion.makeCompletion(QStringLiteral("ca")), carp);
0107     QVERIFY(spy1.count() == 1);
0108     QCOMPARE(spy1.takeFirst().at(0).toString(), carp);
0109 }
0110 
0111 void Test_KCompletion::weightedOrder()
0112 {
0113     KCompletion completion;
0114     completion.setSoundsEnabled(false);
0115     QSignalSpy spy1(&completion, &KCompletion::match);
0116     QSignalSpy spy3(&completion, &KCompletion::multipleMatches);
0117 
0118     completion.setOrder(KCompletion::Weighted);
0119     QVERIFY(completion.order() == KCompletion::Weighted);
0120 
0121     completion.setItems(wstrings);
0122     QVERIFY(completion.items().count() == 4);
0123 
0124     completion.setCompletionMode(KCompletion::CompletionShell);
0125     QCOMPARE(completion.makeCompletion(QStringLiteral("ca")), QStringLiteral("carp"));
0126     spy1.takeFirst(); // empty the list
0127     QVERIFY(spy3.count() == 1);
0128     spy3.takeFirst();
0129 
0130     QSignalSpy spy2(&completion, &KCompletion::matches);
0131     completion.makeCompletion(QStringLiteral("ca"));
0132     QCOMPARE(spy2.count(), 1);
0133     QVERIFY(spy3.count() == 0); // shouldn't be signaled on 2nd call
0134 
0135     QStringList matches = spy2.takeFirst().at(0).toStringList();
0136     QVERIFY(matches.count() == 2);
0137     QCOMPARE(matches[0], carpet);
0138     QCOMPARE(matches[1], carp);
0139 
0140     completion.setCompletionMode(KCompletion::CompletionAuto);
0141     QCOMPARE(completion.makeCompletion(QStringLiteral("ca")), carpet);
0142 
0143     matches = completion.substringCompletion(QStringLiteral("ca"));
0144     QVERIFY(matches.count() == 3);
0145     QCOMPARE(matches[0], carpet);
0146     QCOMPARE(matches[1], coolcat);
0147     QCOMPARE(matches[2], carp);
0148 }
0149 
0150 void Test_KCompletion::substringCompletion_Insertion()
0151 {
0152     KCompletion completion;
0153     completion.setSoundsEnabled(false);
0154     completion.setCompletionMode(KCompletion::CompletionAuto);
0155 
0156     completion.setOrder(KCompletion::Insertion);
0157     completion.setItems(strings);
0158     QVERIFY(completion.items().count() == 4);
0159 
0160     QStringList matches = completion.substringCompletion(QStringLiteral("c"));
0161     QVERIFY(matches.count() == 4);
0162     QCOMPARE(matches[0], clampet);
0163     QCOMPARE(matches[1], coolcat);
0164     QCOMPARE(matches[2], carpet);
0165     QCOMPARE(matches[3], carp);
0166 
0167     matches = completion.substringCompletion(QStringLiteral("ca"));
0168     QVERIFY(matches.count() == 3);
0169     QCOMPARE(matches[0], coolcat);
0170     QCOMPARE(matches[1], carpet);
0171     QCOMPARE(matches[2], carp);
0172 
0173     matches = completion.substringCompletion(QStringLiteral("car"));
0174     QVERIFY(matches.count() == 2);
0175     QCOMPARE(matches[0], carpet);
0176     QCOMPARE(matches[1], carp);
0177 
0178     matches = completion.substringCompletion(QStringLiteral("pet"));
0179     QVERIFY(matches.count() == 2);
0180     QCOMPARE(matches[0], clampet);
0181     QCOMPARE(matches[1], carpet);
0182 }
0183 
0184 void Test_KCompletion::substringCompletion_Sorted()
0185 {
0186     KCompletion completion;
0187     completion.setSoundsEnabled(false);
0188     completion.setCompletionMode(KCompletion::CompletionAuto);
0189 
0190     completion.setOrder(KCompletion::Sorted);
0191     completion.setItems(strings);
0192     QVERIFY(completion.items().count() == 4);
0193 
0194     QStringList matches = completion.substringCompletion(QStringLiteral("c"));
0195     QVERIFY(matches.count() == 4);
0196     QCOMPARE(matches[0], carp);
0197     QCOMPARE(matches[1], carpet);
0198     QCOMPARE(matches[2], clampet);
0199     QCOMPARE(matches[3], coolcat);
0200 
0201     matches = completion.substringCompletion(QStringLiteral("ca"));
0202     QVERIFY(matches.count() == 3);
0203     QCOMPARE(matches[0], carp);
0204     QCOMPARE(matches[1], carpet);
0205     QCOMPARE(matches[2], coolcat);
0206 
0207     matches = completion.substringCompletion(QStringLiteral("car"));
0208     QVERIFY(matches.count() == 2);
0209     QCOMPARE(matches[0], carp);
0210     QCOMPARE(matches[1], carpet);
0211 
0212     matches = completion.substringCompletion(QStringLiteral("pet"));
0213     QVERIFY(matches.count() == 2);
0214     QCOMPARE(matches[0], carpet);
0215     QCOMPARE(matches[1], clampet);
0216 }
0217 
0218 void Test_KCompletion::substringCompletion_Weighted()
0219 {
0220     KCompletion completion;
0221     completion.setSoundsEnabled(false);
0222     completion.setCompletionMode(KCompletion::CompletionAuto);
0223 
0224     completion.setOrder(KCompletion::Weighted);
0225     completion.setItems(wstrings);
0226     QVERIFY(completion.items().count() == 4);
0227 
0228     QStringList matches = completion.substringCompletion(QStringLiteral("c"));
0229     QVERIFY(matches.count() == 4);
0230     QCOMPARE(matches[0], carpet);
0231     QCOMPARE(matches[1], clampet);
0232     QCOMPARE(matches[2], coolcat);
0233     QCOMPARE(matches[3], carp);
0234 
0235     matches = completion.substringCompletion(QStringLiteral("ca"));
0236     QVERIFY(matches.count() == 3);
0237     QCOMPARE(matches[0], carpet);
0238     QCOMPARE(matches[1], coolcat);
0239     QCOMPARE(matches[2], carp);
0240 
0241     matches = completion.substringCompletion(QStringLiteral("car"));
0242     QVERIFY(matches.count() == 2);
0243     QCOMPARE(matches[0], carpet);
0244     QCOMPARE(matches[1], carp);
0245 
0246     matches = completion.substringCompletion(QStringLiteral("pet"));
0247     QVERIFY(matches.count() == 2);
0248     QCOMPARE(matches[0], carpet);
0249     QCOMPARE(matches[1], clampet);
0250 }
0251 
0252 void Test_KCompletion::allMatches_Insertion()
0253 {
0254     KCompletion completion;
0255     completion.setSoundsEnabled(false);
0256     completion.setCompletionMode(KCompletion::CompletionAuto);
0257 
0258     completion.setOrder(KCompletion::Insertion);
0259     completion.setItems(strings);
0260     QVERIFY(completion.items().count() == 4);
0261 
0262     QStringList matches = completion.allMatches(QStringLiteral("c"));
0263     QVERIFY(matches.count() == 4);
0264     QCOMPARE(matches[0], clampet);
0265     QCOMPARE(matches[1], coolcat);
0266     QCOMPARE(matches[2], carpet);
0267     QCOMPARE(matches[3], carp);
0268 
0269     matches = completion.allMatches(QStringLiteral("ca"));
0270     QVERIFY(matches.count() == 2);
0271     QCOMPARE(matches[0], carpet);
0272     QCOMPARE(matches[1], carp);
0273 
0274     matches = completion.allMatches(QStringLiteral("pet"));
0275     QVERIFY(matches.count() == 0);
0276 }
0277 
0278 void Test_KCompletion::allMatches_Sorted()
0279 {
0280     KCompletion completion;
0281     completion.setSoundsEnabled(false);
0282     completion.setCompletionMode(KCompletion::CompletionAuto);
0283 
0284     completion.setOrder(KCompletion::Sorted);
0285     completion.setItems(strings);
0286     QVERIFY(completion.items().count() == 4);
0287 
0288     QStringList matches = completion.allMatches(QStringLiteral("c"));
0289     QVERIFY(matches.count() == 4);
0290     QCOMPARE(matches[0], carp);
0291     QCOMPARE(matches[1], carpet);
0292     QCOMPARE(matches[2], clampet);
0293     QCOMPARE(matches[3], coolcat);
0294 
0295     matches = completion.allMatches(QStringLiteral("ca"));
0296     QVERIFY(matches.count() == 2);
0297     QCOMPARE(matches[0], carp);
0298     QCOMPARE(matches[1], carpet);
0299 
0300     matches = completion.allMatches(QStringLiteral("pet"));
0301     QVERIFY(matches.count() == 0);
0302 }
0303 
0304 void Test_KCompletion::allMatches_Weighted()
0305 {
0306     KCompletion completion;
0307     completion.setSoundsEnabled(false);
0308     completion.setCompletionMode(KCompletion::CompletionAuto);
0309 
0310     completion.setOrder(KCompletion::Weighted);
0311     completion.setItems(wstrings);
0312     QVERIFY(completion.items().count() == 4);
0313 
0314     QStringList matches = completion.allMatches(QStringLiteral("c"));
0315     QVERIFY(matches.count() == 4);
0316     QCOMPARE(matches[0], carpet);
0317     QCOMPARE(matches[1], clampet);
0318     QCOMPARE(matches[2], coolcat);
0319     QCOMPARE(matches[3], carp);
0320 
0321     matches = completion.allMatches(QStringLiteral("ca"));
0322     QVERIFY(matches.count() == 2);
0323     QCOMPARE(matches[0], carpet);
0324     QCOMPARE(matches[1], carp);
0325 
0326     matches = completion.allMatches(QStringLiteral("pet"));
0327     QVERIFY(matches.count() == 0);
0328 }
0329 
0330 void Test_KCompletion::cycleMatches_Insertion()
0331 {
0332     KCompletion completion;
0333     completion.setSoundsEnabled(false);
0334     completion.setOrder(KCompletion::Insertion);
0335     completion.setItems(strings);
0336     completion.setCompletionMode(KCompletion::CompletionAuto);
0337 
0338     completion.makeCompletion(QStringLiteral("ca"));
0339     QCOMPARE(completion.nextMatch(), carpet);
0340     QCOMPARE(completion.nextMatch(), carp);
0341     QCOMPARE(completion.previousMatch(), carpet);
0342     QCOMPARE(completion.previousMatch(), carp);
0343 }
0344 
0345 void Test_KCompletion::cycleMatches_Sorted()
0346 {
0347     KCompletion completion;
0348     completion.setSoundsEnabled(false);
0349     completion.setOrder(KCompletion::Sorted);
0350     completion.setItems(strings);
0351     completion.setCompletionMode(KCompletion::CompletionAuto);
0352 
0353     completion.makeCompletion(QStringLiteral("ca"));
0354     QCOMPARE(completion.nextMatch(), carp);
0355     QCOMPARE(completion.nextMatch(), carpet);
0356     QCOMPARE(completion.previousMatch(), carp);
0357     QCOMPARE(completion.previousMatch(), carpet);
0358 }
0359 
0360 void Test_KCompletion::cycleMatches_Weighted()
0361 {
0362     KCompletion completion;
0363     completion.setSoundsEnabled(false);
0364     completion.setOrder(KCompletion::Weighted);
0365     completion.setItems(wstrings);
0366     completion.setCompletionMode(KCompletion::CompletionAuto);
0367 
0368     completion.makeCompletion(QStringLiteral("ca"));
0369     QCOMPARE(completion.nextMatch(), carpet);
0370     QCOMPARE(completion.nextMatch(), carp);
0371     QCOMPARE(completion.previousMatch(), carpet);
0372     QCOMPARE(completion.previousMatch(), carp);
0373 }
0374 
0375 QTEST_MAIN(Test_KCompletion)
0376 
0377 #include "moc_kcompletioncoretest.cpp"