[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:
parent
8ea4091c7b
commit
18e2f2ae52
@ -19,7 +19,10 @@
|
|||||||
#include "Tests.h"
|
#include "Tests.h"
|
||||||
|
|
||||||
#include "locale/LabelModel.h"
|
#include "locale/LabelModel.h"
|
||||||
|
#include "locale/TranslatableConfiguration.h"
|
||||||
|
|
||||||
#include "utils/Logger.h"
|
#include "utils/Logger.h"
|
||||||
|
#include "CalamaresVersion.h"
|
||||||
|
|
||||||
#include <QtTest/QtTest>
|
#include <QtTest/QtTest>
|
||||||
|
|
||||||
@ -81,3 +84,47 @@ LocaleTests::testEsperanto()
|
|||||||
|
|
||||||
QCOMPARE( QLocale( "eo" ).language(), QLocale::C );
|
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 );
|
||||||
|
}
|
||||||
|
@ -33,6 +33,9 @@ private Q_SLOTS:
|
|||||||
|
|
||||||
void testLanguageModelCount();
|
void testLanguageModelCount();
|
||||||
void testEsperanto();
|
void testEsperanto();
|
||||||
|
void testTranslatableLanguages();
|
||||||
|
void testTranslatableConfig1();
|
||||||
|
void testTranslatableConfig2();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -44,7 +44,7 @@ TranslatedString::TranslatedString(const QVariantMap& map, const QString& key)
|
|||||||
}
|
}
|
||||||
m_strings[QString()] = value;
|
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;
|
QString subkey = (*it).first;
|
||||||
if ( subkey == key )
|
if ( subkey == key )
|
||||||
@ -53,11 +53,12 @@ TranslatedString::TranslatedString(const QVariantMap& map, const QString& key)
|
|||||||
}
|
}
|
||||||
else if ( subkey.startsWith( key ) )
|
else if ( subkey.startsWith( key ) )
|
||||||
{
|
{
|
||||||
|
cDebug() << "Checking" << subkey;
|
||||||
QRegularExpressionMatch match;
|
QRegularExpressionMatch match;
|
||||||
if ( subkey.indexOf( QRegularExpression("\\[([a-zA-Z_@]*)\\]"), 0, &match ) > 0 )
|
if ( subkey.indexOf( QRegularExpression("\\[([a-zA-Z_@]*)\\]"), 0, &match ) > 0 )
|
||||||
{
|
{
|
||||||
QString language = match.captured(1);
|
QString language = match.captured(1);
|
||||||
cDebug() << "Found translation" << key << '[' << language << ']';
|
m_strings[language] = (*it).second.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,9 @@ namespace Locale
|
|||||||
/** @brief Not-actually-translated string.
|
/** @brief Not-actually-translated string.
|
||||||
*/
|
*/
|
||||||
TranslatedString( const QString& string );
|
TranslatedString( const QString& string );
|
||||||
|
|
||||||
|
int count() const { return m_strings.count(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Maps locale name to human-readable string, "" is English
|
// Maps locale name to human-readable string, "" is English
|
||||||
QMap< QString, QString > m_strings;
|
QMap< QString, QString > m_strings;
|
||||||
|
Loading…
Reference in New Issue
Block a user