[libcalamares] Add tests for new TranslatedString

- Test that construction works as expected
 - Add count() method to TranslatedString for testing purposes.
This commit is contained in:
Adriaan de Groot 2019-08-05 17:57:32 +02:00
parent 8ea4091c7b
commit 18e2f2ae52
4 changed files with 56 additions and 2 deletions

View File

@ -19,7 +19,10 @@
#include "Tests.h"
#include "locale/LabelModel.h"
#include "locale/TranslatableConfiguration.h"
#include "utils/Logger.h"
#include "CalamaresVersion.h"
#include <QtTest/QtTest>
@ -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 );
}

View File

@ -33,6 +33,9 @@ private Q_SLOTS:
void testLanguageModelCount();
void testEsperanto();
void testTranslatableLanguages();
void testTranslatableConfig1();
void testTranslatableConfig2();
};
#endif

View File

@ -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();
}
}
}

View File

@ -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;