chore: Add tests for I18N

This commit is contained in:
sithlord48 2025-12-08 19:49:26 -05:00 committed by Nick Bolton
parent 13750f4175
commit b1bac23dcb
3 changed files with 124 additions and 0 deletions

View file

@ -7,3 +7,10 @@ create_test(
SOURCE SettingsTests.cpp
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/common"
)
create_test(
NAME I18NTests
DEPENDS common
SOURCE I18NTests.cpp
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/common"
)

View file

@ -0,0 +1,86 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include "I18NTests.h"
#include "common/I18N.h"
#include "common/Settings.h"
#include <QDir>
#include <QFile>
#include <QSignalSpy>
void I18NTests::initTestCase()
{
QFile oldSettings(m_settingsFile);
if (oldSettings.exists())
oldSettings.remove();
Settings::setSettingsFile(m_settingsFile);
Settings::setStateFile(m_stateFile);
m_myTDir = QStringLiteral("%1/translations").arg(QCoreApplication::applicationDirPath());
const auto srcTDir = QStringLiteral("%1/../../../translations").arg(QCoreApplication::applicationDirPath());
QDir dir;
if (dir.exists(m_myTDir)) {
dir.setPath(m_myTDir);
dir.removeRecursively();
}
dir.mkdir(m_myTDir);
dir.setPath(srcTDir);
for (const auto &file : dir.entryList({"deskflow_*.qm"}, QDir::Files, QDir::Name)) {
QFile::copy(QStringLiteral("%1/%2").arg(srcTDir, file), QStringLiteral("%1/%2").arg(m_myTDir, file));
QVERIFY(QFile::exists(QStringLiteral("%1/%2").arg(m_myTDir, file)));
}
}
void I18NTests::creationTest()
{
QVERIFY(I18N::instance());
}
void I18NTests::detectedLangTest()
{
for (const auto &lang : I18N::detectedLanguages())
QVERIFY(m_langMap.contains(lang));
}
void I18NTests::check639NameTest()
{
for (const auto &lang : m_langMap.keys())
QCOMPARE(I18N::nativeTo639Name(lang), m_langMap.value(lang));
}
void I18NTests::toNativeNameTest()
{
for (const auto &lang : m_langMap.values())
QCOMPARE(I18N::toNativeName(lang), m_langMap.key(lang));
}
void I18NTests::setLangTest()
{
QSignalSpy spy(I18N::instance(), &I18N::languageChanged);
for (const auto &lang : m_langMap.values()) {
I18N::setLanguage(lang);
QCOMPARE(I18N::currentLanguage(), lang);
}
QCOMPARE(spy.count(), m_langMap.count());
}
void I18NTests::reDetectTest()
{
QSignalSpy spy(I18N::instance(), &I18N::langaugesChanged);
I18N::reDetectLanguages();
QCOMPARE(spy.count(), 0);
QFile::remove(QStringLiteral("%1/deskflow_en.qm").arg(m_myTDir));
I18N::reDetectLanguages();
QCOMPARE(spy.count(), 1);
}
QTEST_MAIN(I18NTests)

View file

@ -0,0 +1,31 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include <QTest>
class I18NTests : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void creationTest();
void detectedLangTest();
void check639NameTest();
void toNativeNameTest();
void setLangTest();
void reDetectTest();
private:
QString m_myTDir;
inline static const QString m_settingsPathTemp = QStringLiteral("tmp/test");
inline static const QString m_settingsFile = QStringLiteral("%1/Deskflow.conf").arg(m_settingsPathTemp);
inline static const QString m_stateFile = QStringLiteral("%1/Deskflow.state").arg(m_settingsPathTemp);
inline static const QMap<QString, QString> m_langMap = {
{QStringLiteral("English"), QStringLiteral("en")}, {QStringLiteral("Español"), QStringLiteral("es")},
{QStringLiteral("Italiano"), QStringLiteral("it")}, {QStringLiteral("日本語"), QStringLiteral("ja")},
{QStringLiteral("Русский"), QStringLiteral("ru")}, {QStringLiteral("简体中文"), QStringLiteral("zh_CN")}
};
};