File indexing completed on 2024-12-01 12:41:43

0001 // krazy:excludeall=spelling
0002 /**
0003  * test.cpp
0004  *
0005  * SPDX-FileCopyrightText: 2004 Zack Rusin <zack@kde.org>
0006  *
0007  * SPDX-License-Identifier: LGPL-2.1-or-later
0008  */
0009 #include "speller.h"
0010 
0011 #include <QDebug>
0012 #include <QObject>
0013 #include <QTest>
0014 
0015 using namespace Sonnet;
0016 
0017 class SuggestTest : public QObject
0018 {
0019     Q_OBJECT
0020 
0021 private Q_SLOTS:
0022     void aspell_english_data();
0023     void aspell_english();
0024     void performance();
0025 };
0026 
0027 void SuggestTest::aspell_english_data()
0028 {
0029     // Input
0030     QTest::addColumn<QString>("word");
0031     // Expected output
0032     QTest::addColumn<bool>("correct");
0033     QTest::addColumn<QStringList>("expectedSuggestions");
0034 
0035     QTest::newRow("hello") << QStringLiteral("hello") << true << QStringList();
0036     QTest::newRow("helo") << QStringLiteral("helo") << false << QStringList{QStringLiteral("hello"), QStringLiteral("hell"), QStringLiteral("help")};
0037     QTest::newRow("enviroment") << QStringLiteral("enviroment") << false << QStringList{QStringLiteral("environment")};
0038     QTest::newRow("guvernment") << QStringLiteral("guvernment") << false << QStringList{QStringLiteral("government")};
0039     QTest::newRow("dictionaies") << QStringLiteral("dictionaies") << false << QStringList{QStringLiteral("dictionaries")};
0040 }
0041 
0042 void SuggestTest::aspell_english()
0043 {
0044     QFETCH(QString, word);
0045     QFETCH(bool, correct);
0046     QFETCH(QStringList, expectedSuggestions);
0047 
0048     Speller dict(QStringLiteral("en_US"));
0049     if (!dict.availableBackends().contains(QLatin1String("ASpell"))) {
0050         QSKIP("ASpell not available");
0051     }
0052     if (!dict.availableLanguages().contains(QLatin1String("en"))) {
0053         QSKIP("'en' not available");
0054     }
0055     dict.setDefaultClient(QStringLiteral("ASpell"));
0056     dict.setDefaultLanguage(QStringLiteral("en"));
0057 
0058     QCOMPARE(dict.isCorrect(word), correct);
0059     if (!correct) {
0060         const QStringList suggestions = dict.suggest(word);
0061         qDebug() << suggestions;
0062         // Check that each expectedSuggestions was found, make list of those that were missing
0063         QStringList missingSuggestions;
0064         std::copy_if(expectedSuggestions.begin(), expectedSuggestions.end(), std::back_inserter(missingSuggestions), [&suggestions](const QString &sugg) {
0065             return !suggestions.contains(sugg);
0066         });
0067         QCOMPARE(missingSuggestions.join(QLatin1Char(',')), QString()); // not using QVERIFY, to see the list in the output
0068     }
0069 }
0070 
0071 void SuggestTest::performance()
0072 {
0073     Speller dict(QStringLiteral("en_US"));
0074 
0075     qDebug() << "Clients are " << dict.availableBackends();
0076     qDebug() << "Languages are " << dict.availableLanguages();
0077 
0078     QStringList words;
0079     for (int i = 0; i < 30; ++i) {
0080         words << QStringLiteral("hello") << QStringLiteral("helo") << QStringLiteral("enviroment") << QStringLiteral("guvernment") << QStringLiteral("farted");
0081     }
0082 
0083     QBENCHMARK {
0084         for (QStringList::Iterator itr = words.begin(); itr != words.end(); ++itr) {
0085             if (!dict.isCorrect(*itr)) {
0086                 // qDebug()<<"Word " << *itr <<" is misspelled";
0087                 QStringList sug = dict.suggest(*itr);
0088                 // qDebug()<<"Suggestions : "<<sug;
0089             }
0090         }
0091     }
0092 }
0093 
0094 QTEST_GUILESS_MAIN(SuggestTest)
0095 
0096 #include "test_suggest.moc"