From 18e2f2ae527f6c83a1bb33d0e046bc0556e33a4f Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 5 Aug 2019 17:57:32 +0200 Subject: [PATCH] [libcalamares] Add tests for new TranslatedString - Test that construction works as expected - Add count() method to TranslatedString for testing purposes. --- src/libcalamares/locale/Tests.cpp | 47 +++++++++++++++++++ src/libcalamares/locale/Tests.h | 3 ++ .../locale/TranslatableConfiguration.cpp | 5 +- .../locale/TranslatableConfiguration.h | 3 ++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/libcalamares/locale/Tests.cpp b/src/libcalamares/locale/Tests.cpp index 9e2071a31..a87de8a98 100644 --- a/src/libcalamares/locale/Tests.cpp +++ b/src/libcalamares/locale/Tests.cpp @@ -19,7 +19,10 @@ #include "Tests.h" #include "locale/LabelModel.h" +#include "locale/TranslatableConfiguration.h" + #include "utils/Logger.h" +#include "CalamaresVersion.h" #include @@ -81,3 +84,47 @@ LocaleTests::testEsperanto() QCOMPARE( QLocale( "eo" ).language(), QLocale::C ); } + +static const QStringList& +someLanguages() +{ + static QStringList languages{ "nl", "de", "da", "nb", "sr@latin", "ar", "ru" }; + return languages; + } + + + void LocaleTests::testTranslatableLanguages() +{ + QStringList availableLanguages = QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' ); + cDebug() << "Translation languages:" << availableLanguages; + for ( const auto& language: someLanguages() ) + { + // Could be QVERIFY, but then we don't see what language code fails + QCOMPARE( availableLanguages.contains( language ) ? language : QString(), language ); + } +} + +void LocaleTests::testTranslatableConfig1() +{ + CalamaresUtils::Locale::TranslatedString ts1( "Hello" ); + QCOMPARE( ts1.count(), 1 ); + + QVariantMap map; + map.insert( "description", "description (no language)" ); + CalamaresUtils::Locale::TranslatedString ts2(map, "description"); + QCOMPARE( ts2.count(), 1 ); +} + +void LocaleTests::testTranslatableConfig2() +{ + QVariantMap map; + + for ( const auto& language: someLanguages() ) + { + map.insert( QString("description[%1]").arg(language), QString("description (language %1)").arg(language) ); + } + + CalamaresUtils::Locale::TranslatedString s(map, "description"); + // The +1 is because "" is always also inserted + QCOMPARE( s.count(), someLanguages().count()+1 ); +} diff --git a/src/libcalamares/locale/Tests.h b/src/libcalamares/locale/Tests.h index be712388f..c6949f3e4 100644 --- a/src/libcalamares/locale/Tests.h +++ b/src/libcalamares/locale/Tests.h @@ -33,6 +33,9 @@ private Q_SLOTS: void testLanguageModelCount(); void testEsperanto(); + void testTranslatableLanguages(); + void testTranslatableConfig1(); + void testTranslatableConfig2(); }; #endif diff --git a/src/libcalamares/locale/TranslatableConfiguration.cpp b/src/libcalamares/locale/TranslatableConfiguration.cpp index 6d4684121..d6c078303 100644 --- a/src/libcalamares/locale/TranslatableConfiguration.cpp +++ b/src/libcalamares/locale/TranslatableConfiguration.cpp @@ -44,7 +44,7 @@ TranslatedString::TranslatedString(const QVariantMap& map, const QString& key) } m_strings[QString()] = value; - for ( auto it = m_strings.constKeyValueBegin(); it != m_strings.constKeyValueEnd(); ++it ) + for ( auto it = map.constKeyValueBegin(); it != map.constKeyValueEnd(); ++it ) { QString subkey = (*it).first; if ( subkey == key ) @@ -53,11 +53,12 @@ TranslatedString::TranslatedString(const QVariantMap& map, const QString& key) } else if ( subkey.startsWith( key ) ) { + cDebug() << "Checking" << subkey; QRegularExpressionMatch match; if ( subkey.indexOf( QRegularExpression("\\[([a-zA-Z_@]*)\\]"), 0, &match ) > 0 ) { QString language = match.captured(1); - cDebug() << "Found translation" << key << '[' << language << ']'; + m_strings[language] = (*it).second.toString(); } } } diff --git a/src/libcalamares/locale/TranslatableConfiguration.h b/src/libcalamares/locale/TranslatableConfiguration.h index 8cc02fadd..01bdf8ed6 100644 --- a/src/libcalamares/locale/TranslatableConfiguration.h +++ b/src/libcalamares/locale/TranslatableConfiguration.h @@ -44,6 +44,9 @@ namespace Locale /** @brief Not-actually-translated string. */ TranslatedString( const QString& string ); + + int count() const { return m_strings.count(); } + private: // Maps locale name to human-readable string, "" is English QMap< QString, QString > m_strings;