File indexing completed on 2024-05-12 16:59:40

0001 /*
0002     SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <QObject>
0008 #include <QTest>
0009 
0010 #include "../spellcheck.h"
0011 
0012 class SpellCheckRunnerTest : public QObject
0013 {
0014     Q_OBJECT
0015 private Q_SLOTS:
0016     void initTestCase();
0017     void cleanupTestCase();
0018 
0019     void testDefaultDictionary();
0020     void testSpecifiedDictionary();
0021     void testAutomaticDictionary();
0022     void testSuggestions();
0023 
0024 private:
0025     bool m_isDefaultDictonaryUS;
0026     bool m_hasUSDictonary;
0027     bool m_hasRUDictionary;
0028 };
0029 
0030 void SpellCheckRunnerTest::initTestCase()
0031 {
0032     Sonnet::Speller defaultDictonary = Sonnet::Speller(QString());
0033 
0034     m_isDefaultDictonaryUS = defaultDictonary.language() == QStringLiteral("en_US");
0035     m_hasUSDictonary = defaultDictonary.availableLanguages().contains(QStringLiteral("en_US"));
0036     m_hasRUDictionary = defaultDictonary.availableLanguages().contains(QStringLiteral("ru_RU"));
0037 }
0038 
0039 void SpellCheckRunnerTest::cleanupTestCase()
0040 {
0041 }
0042 
0043 void SpellCheckRunnerTest::testDefaultDictionary()
0044 {
0045     if (!m_isDefaultDictonaryUS) {
0046         QSKIP("The default dictionary is not en_US.");
0047     }
0048 
0049     SpellCheckRunner runner(this, KPluginMetaData(), QVariantList());
0050     runner.loadData();
0051 
0052     Plasma::RunnerContext context;
0053     context.setQuery(QStringLiteral("hello"));
0054     runner.match(context);
0055     const auto matches = context.matches();
0056     QCOMPARE(matches.count(), 1);
0057     QCOMPARE(matches.constFirst().text(), QStringLiteral("hello"));
0058     QCOMPARE(matches.constFirst().iconName(), QStringLiteral("checkbox"));
0059 }
0060 
0061 void SpellCheckRunnerTest::testSpecifiedDictionary()
0062 {
0063     if (!m_hasUSDictonary) {
0064         QSKIP("en_US dictionary is not available.");
0065     }
0066 
0067     SpellCheckRunner runner(this, KPluginMetaData(), QVariantList());
0068     runner.loadData();
0069 
0070     Plasma::RunnerContext context;
0071 
0072     // Test exact match
0073     context.setQuery(QStringLiteral("en_US hello"));
0074     runner.match(context);
0075     auto matches = context.matches();
0076     QCOMPARE(matches.count(), 1);
0077     QCOMPARE(matches.constFirst().text(), QStringLiteral("hello"));
0078     QCOMPARE(matches.constFirst().iconName(), QStringLiteral("checkbox"));
0079 
0080     // Test case-insensitive match
0081     context.setQuery(QStringLiteral("en_us hello"));
0082     runner.match(context);
0083     matches = context.matches();
0084     QCOMPARE(matches.count(), 1);
0085     QCOMPARE(matches.constFirst().text(), QStringLiteral("hello"));
0086     QCOMPARE(matches.constFirst().iconName(), QStringLiteral("checkbox"));
0087 
0088     // Test startsWith
0089     context.setQuery(QStringLiteral("en hello"));
0090     runner.match(context);
0091     matches = context.matches();
0092     QCOMPARE(matches.count(), 1);
0093     QCOMPARE(matches.constFirst().text(), QStringLiteral("hello"));
0094     QCOMPARE(matches.constFirst().iconName(), QStringLiteral("checkbox"));
0095 
0096     if (m_hasRUDictionary) {
0097         context.setQuery(QStringLiteral("ru_RU мама"));
0098         runner.match(context);
0099         auto matches = context.matches();
0100         QCOMPARE(matches.count(), 1);
0101         QCOMPARE(matches.constFirst().text(), QStringLiteral("мама"));
0102         QCOMPARE(matches.constFirst().iconName(), QStringLiteral("checkbox"));
0103     }
0104 }
0105 
0106 void SpellCheckRunnerTest::testAutomaticDictionary()
0107 {
0108     if (!m_isDefaultDictonaryUS) {
0109         QSKIP("The default dictionary is not en_US.");
0110     }
0111 
0112     if (!m_hasRUDictionary) {
0113         QSKIP("ru_RU dictionary is not available.");
0114     }
0115 
0116     SpellCheckRunner runner(this, KPluginMetaData(), QVariantList());
0117     runner.loadData();
0118 
0119     Plasma::RunnerContext context;
0120 
0121     context.setQuery(QStringLiteral("мама"));
0122     runner.match(context);
0123     auto matches = context.matches();
0124     QCOMPARE(matches.count(), 1);
0125     QCOMPARE(matches.constFirst().text(), QStringLiteral("мама"));
0126     QCOMPARE(matches.constFirst().iconName(), QStringLiteral("checkbox"));
0127 
0128     // When a language code is specified but the search term is not in the language.
0129     context.setQuery(QStringLiteral("en_US мама"));
0130     runner.match(context);
0131     matches = context.matches();
0132     QCOMPARE(matches.count(), 0);
0133 }
0134 
0135 void SpellCheckRunnerTest::testSuggestions()
0136 {
0137     if (!m_hasUSDictonary) {
0138         QSKIP("en_US dictionary is not available.");
0139     }
0140 
0141     SpellCheckRunner runner(this, KPluginMetaData(), QVariantList());
0142     runner.loadData();
0143 
0144     Plasma::RunnerContext context;
0145     context.setQuery(QStringLiteral("hallo"));
0146 
0147     runner.match(context);
0148     const auto matches = context.matches();
0149     QVERIFY(std::any_of(matches.cbegin(), matches.cend(), [](const Plasma::QueryMatch &match) {
0150         return match.text() == QStringLiteral("hello") && match.iconName() == QStringLiteral("edit-rename");
0151     }));
0152 }
0153 
0154 QTEST_MAIN(SpellCheckRunnerTest)
0155 
0156 #include "spellcheckrunnertest.moc"