File indexing completed on 2024-04-21 03:53:20

0001 /*
0002     SPDX-FileCopyrightText: 2018 Harald Sitter <sitter@kde.org>
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 <QStandardPaths>
0009 #include <QTest>
0010 
0011 #include "klanguagename.h"
0012 
0013 #ifdef Q_OS_WIN
0014 #include <filesystem>
0015 #endif
0016 
0017 class KLanguageNameTest : public QObject
0018 {
0019     Q_OBJECT
0020 
0021 public:
0022     static void initMain()
0023     {
0024         qputenv("LANG", "C.UTF-8");
0025         qputenv("LANGUAGE", "en");
0026         QStandardPaths::setTestModeEnabled(true);
0027     }
0028 
0029 private Q_SLOTS:
0030     void initTestCase()
0031     {
0032         // looking for the test data as deployed for this test, needs QApp instance created
0033 #ifdef Q_OS_WIN
0034         const std::string source = QFINDTESTDATA("kf6_entry_data").toStdString();
0035         const std::string dest = QString(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)).toStdString();
0036 
0037         std::filesystem::remove_all(dest);
0038         std::filesystem::copy(source, dest, std::filesystem::copy_options::recursive);
0039 #else
0040         qputenv("XDG_DATA_DIRS", qUtf8Printable(QFINDTESTDATA("kf6_entry_data")));
0041 #endif
0042 
0043         // NOTE
0044         // - fr has no translations
0045         // - es has no kf6_entry at all
0046         // - other languages under testing are complete
0047     }
0048 
0049 private Q_SLOTS:
0050     void testListFound()
0051     {
0052         QVERIFY(KLanguageName::allLanguageCodes().count() > 0);
0053     }
0054     void testNameForCode()
0055     {
0056         // This is somewhat wrong, it should not say US.
0057         QCOMPARE(KLanguageName::nameForCode("en"), "US English");
0058 
0059         QCOMPARE(KLanguageName::nameForCode("de"), "German");
0060         QCOMPARE(KLanguageName::nameForCode("pt"), "Portuguese");
0061         QCOMPARE(KLanguageName::nameForCode("ca"), "Catalan");
0062     }
0063 
0064     void testNameForCodeInLocale()
0065     {
0066         // This is somewhat wrong, it should not say US.
0067         QCOMPARE(KLanguageName::nameForCodeInLocale("en", "de"), "US-Englisch");
0068 
0069         QCOMPARE(KLanguageName::nameForCodeInLocale("de", "de"), "Deutsch");
0070         QCOMPARE(KLanguageName::nameForCodeInLocale("pt", "de"), "Portugiesisch");
0071         QCOMPARE(KLanguageName::nameForCodeInLocale("ca", "de"), "Katalanisch");
0072     }
0073 
0074     void testNoTranslation()
0075     {
0076         // This has an entry file but no translation => QLocale.
0077         QCOMPARE(KLanguageName::nameForCode("fr"), "French");
0078         QCOMPARE(KLanguageName::nameForCodeInLocale("fr", "de"), "French");
0079         // When in the same language, use the native name.
0080         QCOMPARE(KLanguageName::nameForCodeInLocale("fr", "fr"), "français");
0081     }
0082 
0083     void testNoEntry()
0084     {
0085         // This has no entry file => QLocale.
0086         QCOMPARE(KLanguageName::nameForCode("es"), "Spanish");
0087         QCOMPARE(KLanguageName::nameForCodeInLocale("es", "de"), "Spanish");
0088         // When in the same language, use the native name.
0089         QCOMPARE(KLanguageName::nameForCodeInLocale("es", "es"), "español de España");
0090     }
0091 
0092     void testNoString()
0093     {
0094         // Qt doesn't have za support, we have no test fixture, so no string.
0095         QCOMPARE(KLanguageName::nameForCode("za"), QString());
0096     }
0097 };
0098 
0099 QTEST_GUILESS_MAIN(KLanguageNameTest)
0100 
0101 #include "klanguagenametest.moc"