Verified Commit 84f964d6 authored by Fushan Wen's avatar Fushan Wen
Browse files

runners/spellchecker: Add autotests

1. `testDefaultDictionary`: Test the runner can return a suggestion.
   (spell hello)
2. `testSpecifiedDictionary`: Test the runner can return a suggestion
   when the language is specified. (spell en_US hello)
3. `testAutomaticDictionary`: Test the runner can return a suggestion
   when the term is not in the default language. (spell мама)
4. `testSuggestions`: Test the runner can return a suggestion when the
   term is misspelled.

CCBUG: 439544
parent 10b7007c
Pipeline #144941 passed with stage
in 2 minutes and 50 seconds
add_definitions(-DTRANSLATION_DOMAIN="plasma_runner_spellcheckrunner")
kcoreaddons_add_plugin(krunner_spellcheck SOURCES spellcheck.cpp INSTALL_NAMESPACE "kf5/krunner")
target_link_libraries(krunner_spellcheck
add_library(krunner_spellcheck_static STATIC spellcheck.cpp)
target_link_libraries(krunner_spellcheck_static
KF5::Runner
KF5::KIOWidgets
KF5::I18n
KF5::SonnetCore
)
kcoreaddons_add_plugin(krunner_spellcheck SOURCES spellcheck.cpp INSTALL_NAMESPACE "kf5/krunner")
target_link_libraries(krunner_spellcheck
krunner_spellcheck_static
)
kcoreaddons_add_plugin(kcm_krunner_spellcheck INSTALL_NAMESPACE "kf5/krunner/kcms")
ki18n_wrap_ui(kcm_krunner_spellcheck spellcheck_config.ui)
target_sources(kcm_krunner_spellcheck PRIVATE spellcheck_config.cpp)
......@@ -17,3 +22,7 @@ target_link_libraries(kcm_krunner_spellcheck
KF5::KCMUtils
KF5::I18n
)
if(BUILD_TESTING)
add_subdirectory(autotests)
endif()
# SPDX-License-Identifier: BSD-3-Clause
# SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
remove_definitions(-DQT_NO_CAST_FROM_ASCII)
include(ECMAddTests)
ecm_add_test(spellcheckrunnertest.cpp TEST_NAME spellcheckrunnertest
LINK_LIBRARIES
Qt::Test
krunner_spellcheck_static
)
/*
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include <QObject>
#include <QTest>
#include "../spellcheck.h"
class SpellCheckRunnerTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void testDefaultDictionary();
void testSpecifiedDictionary();
void testAutomaticDictionary();
void testSuggestions();
private:
bool m_isDefaultDictonaryUS;
bool m_hasUSDictonary;
bool m_hasRUDictionary;
};
void SpellCheckRunnerTest::initTestCase()
{
Sonnet::Speller defaultDictonary = Sonnet::Speller(QString());
m_isDefaultDictonaryUS = defaultDictonary.language() == QStringLiteral("en_US");
m_hasUSDictonary = defaultDictonary.availableLanguages().contains(QStringLiteral("en_US"));
m_hasRUDictionary = defaultDictonary.availableLanguages().contains(QStringLiteral("ru_RU"));
}
void SpellCheckRunnerTest::cleanupTestCase()
{
}
void SpellCheckRunnerTest::testDefaultDictionary()
{
if (!m_isDefaultDictonaryUS) {
QSKIP("The default dictionary is not en_US.");
}
SpellCheckRunner runner(this, KPluginMetaData(), QVariantList());
runner.loadData();
Plasma::RunnerContext context;
context.setQuery(QStringLiteral("hello"));
runner.match(context);
const auto matches = context.matches();
QCOMPARE(matches.count(), 1);
QCOMPARE(matches.constFirst().text(), QStringLiteral("hello"));
QCOMPARE(matches.constFirst().iconName(), QStringLiteral("checkbox"));
}
void SpellCheckRunnerTest::testSpecifiedDictionary()
{
if (!m_hasUSDictonary) {
QSKIP("en_US dictionary is not available.");
}
SpellCheckRunner runner(this, KPluginMetaData(), QVariantList());
runner.loadData();
Plasma::RunnerContext context;
// Test exact match
context.setQuery(QStringLiteral("en_US hello"));
runner.match(context);
auto matches = context.matches();
QCOMPARE(matches.count(), 1);
QCOMPARE(matches.constFirst().text(), QStringLiteral("hello"));
QCOMPARE(matches.constFirst().iconName(), QStringLiteral("checkbox"));
// Test case-insensitive match
context.setQuery(QStringLiteral("en_us hello"));
runner.match(context);
matches = context.matches();
QCOMPARE(matches.count(), 1);
QCOMPARE(matches.constFirst().text(), QStringLiteral("hello"));
QCOMPARE(matches.constFirst().iconName(), QStringLiteral("checkbox"));
// Test startsWith
context.setQuery(QStringLiteral("en hello"));
runner.match(context);
matches = context.matches();
QCOMPARE(matches.count(), 1);
QCOMPARE(matches.constFirst().text(), QStringLiteral("hello"));
QCOMPARE(matches.constFirst().iconName(), QStringLiteral("checkbox"));
if (m_hasRUDictionary) {
context.setQuery(QStringLiteral("ru_RU мама"));
runner.match(context);
auto matches = context.matches();
QCOMPARE(matches.count(), 1);
QCOMPARE(matches.constFirst().text(), QStringLiteral("мама"));
QCOMPARE(matches.constFirst().iconName(), QStringLiteral("checkbox"));
}
}
void SpellCheckRunnerTest::testAutomaticDictionary()
{
if (!m_isDefaultDictonaryUS) {
QSKIP("The default dictionary is not en_US.");
}
if (!m_hasRUDictionary) {
QSKIP("ru_RU dictionary is not available.");
}
SpellCheckRunner runner(this, KPluginMetaData(), QVariantList());
runner.loadData();
Plasma::RunnerContext context;
context.setQuery(QStringLiteral("мама"));
runner.match(context);
auto matches = context.matches();
QCOMPARE(matches.count(), 1);
QCOMPARE(matches.constFirst().text(), QStringLiteral("мама"));
QCOMPARE(matches.constFirst().iconName(), QStringLiteral("checkbox"));
// When a language code is specified but the search term is not in the language.
context.setQuery(QStringLiteral("en_US мама"));
runner.match(context);
matches = context.matches();
QCOMPARE(matches.count(), 0);
}
void SpellCheckRunnerTest::testSuggestions()
{
if (!m_hasUSDictonary) {
QSKIP("en_US dictionary is not available.");
}
SpellCheckRunner runner(this, KPluginMetaData(), QVariantList());
runner.loadData();
Plasma::RunnerContext context;
context.setQuery(QStringLiteral("hallo"));
runner.match(context);
const auto matches = context.matches();
QVERIFY(std::any_of(matches.cbegin(), matches.cend(), [](const Plasma::QueryMatch &match) {
return match.text() == QStringLiteral("hello") && match.iconName() == QStringLiteral("edit-rename");
}));
}
QTEST_MAIN(SpellCheckRunnerTest)
#include "spellcheckrunnertest.moc"
......@@ -47,6 +47,8 @@ private:
bool m_requireTriggerWord;
QMap<QString, QSharedPointer<Sonnet::Speller>> m_spellers; // spellers
QMutex m_spellLock; // Lock held when constructing a new speller
friend class SpellCheckRunnerTest;
};
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment