File indexing completed on 2024-05-05 12:11:25
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 static void setEnvironment() 0018 { 0019 qputenv("LANG", "C.UTF-8"); 0020 qputenv("LANGUAGE", "en"); 0021 QStandardPaths::setTestModeEnabled(true); 0022 // There is a distinct chance of the envionrment setup being to late and 0023 // causing flakey results based on the execution env. 0024 // Make sure we definitely default to english. 0025 QLocale::setDefault(QLocale::English); 0026 0027 #ifdef Q_OS_WIN 0028 const std::string source = QFINDTESTDATA("kf5_entry_data").toStdString(); 0029 const std::string dest = QString(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)).toStdString(); 0030 0031 std::filesystem::remove_all(dest); 0032 std::filesystem::copy(source, dest, std::filesystem::copy_options::recursive); 0033 #else 0034 qputenv("XDG_DATA_DIRS", qUtf8Printable(QFINDTESTDATA("kf5_entry_data"))); 0035 #endif 0036 0037 // NOTE 0038 // - fr has no translations 0039 // - es has no kf5_entry at all 0040 // - other languages under testing are complete 0041 } 0042 0043 class KLanguageNameTest : public QObject 0044 { 0045 Q_OBJECT 0046 0047 private Q_SLOTS: 0048 void testListFound() 0049 { 0050 QVERIFY(KLanguageName::allLanguageCodes().count() > 0); 0051 } 0052 void testNameForCode() 0053 { 0054 // This is somewhat wrong, it should not say US. 0055 QCOMPARE(KLanguageName::nameForCode("en"), "US English"); 0056 0057 QCOMPARE(KLanguageName::nameForCode("de"), "German"); 0058 QCOMPARE(KLanguageName::nameForCode("pt"), "Portuguese"); 0059 QCOMPARE(KLanguageName::nameForCode("ca"), "Catalan"); 0060 } 0061 0062 void testNameForCodeInLocale() 0063 { 0064 // This is somewhat wrong, it should not say US. 0065 QCOMPARE(KLanguageName::nameForCodeInLocale("en", "de"), "US-Englisch"); 0066 0067 QCOMPARE(KLanguageName::nameForCodeInLocale("de", "de"), "Deutsch"); 0068 QCOMPARE(KLanguageName::nameForCodeInLocale("pt", "de"), "Portugiesisch"); 0069 QCOMPARE(KLanguageName::nameForCodeInLocale("ca", "de"), "Katalanisch"); 0070 } 0071 0072 void testNoTranslation() 0073 { 0074 // This has an entry file but no translation => QLocale. 0075 QCOMPARE(KLanguageName::nameForCode("fr"), "French"); 0076 QCOMPARE(KLanguageName::nameForCodeInLocale("fr", "de"), "French"); 0077 // When in the same language, use the native name. 0078 QCOMPARE(KLanguageName::nameForCodeInLocale("fr", "fr"), "français"); 0079 } 0080 0081 void testNoEntry() 0082 { 0083 // This has no entry file => QLocale. 0084 QCOMPARE(KLanguageName::nameForCode("es"), "Spanish"); 0085 QCOMPARE(KLanguageName::nameForCodeInLocale("es", "de"), "Spanish"); 0086 // When in the same language, use the native name. 0087 QCOMPARE(KLanguageName::nameForCodeInLocale("es", "es"), "español de España"); 0088 } 0089 0090 void testNoString() 0091 { 0092 // Qt doesn't have za support, we have no test fixture, so no string. 0093 QCOMPARE(KLanguageName::nameForCode("za"), QString()); 0094 } 0095 }; 0096 0097 Q_COREAPP_STARTUP_FUNCTION(setEnvironment) 0098 0099 QTEST_GUILESS_MAIN(KLanguageNameTest) 0100 0101 #include "klanguagenametest.moc"