[libcalamares] Add isEmpty() to TranslatedString

- Massage the implementation a bit, don't insert a meaningless
   copy of the key as the untranslated message.
 - Add isEmpty() to check for presence of the untranslated message.
 - Document API.
 - Update tests.
This commit is contained in:
Adriaan de Groot 2019-08-06 11:53:35 +02:00
parent 8d3546f0b3
commit 4febe477cf
3 changed files with 47 additions and 6 deletions

View File

@ -96,6 +96,10 @@ someLanguages()
}
/** @brief Check consistency of test data
* Check that all the languages used in testing, are actually enabled
* in Calamares translations.
*/
void
LocaleTests::testTranslatableLanguages()
{
@ -108,12 +112,19 @@ LocaleTests::testTranslatableLanguages()
}
}
/** @brief Test strings with no translations
*/
void
LocaleTests::testTranslatableConfig1()
{
CalamaresUtils::Locale::TranslatedString ts0;
QVERIFY( ts0.isEmpty() );
QCOMPARE( ts0.count(), 1 ); // the empty string
QCOMPARE( QLocale().name(), "C" ); // Otherwise plain get() is dubious
CalamaresUtils::Locale::TranslatedString ts1( "Hello" );
QCOMPARE( ts1.count(), 1 );
QVERIFY( !ts1.isEmpty() );
QCOMPARE( ts1.get(), "Hello" );
QCOMPARE( ts1.get( QLocale( "nl" ) ), "Hello" );
@ -122,11 +133,14 @@ LocaleTests::testTranslatableConfig1()
map.insert( "description", "description (no language)" );
CalamaresUtils::Locale::TranslatedString ts2( map, "description" );
QCOMPARE( ts2.count(), 1 );
QVERIFY( !ts2.isEmpty() );
QCOMPARE( ts2.get(), "description (no language)" );
QCOMPARE( ts2.get( QLocale( "nl" ) ), "description (no language)" );
}
/** @bref Test strings with translations.
*/
void
LocaleTests::testTranslatableConfig2()
{
@ -143,11 +157,22 @@ LocaleTests::testTranslatableConfig2()
}
}
// If there's no untranslated string in the map, it is considered empty
CalamaresUtils::Locale::TranslatedString ts0( map, "description" );
QVERIFY( ts0.isEmpty() ); // Because no untranslated string
QCOMPARE( ts0.count(),
someLanguages().count() + 1 ); // But there are entries for the translations, plus an empty string
// expand the map with untranslated entries
map.insert( QString( "description" ), "description (no language)" );
map.insert( QString( "name" ), "name (no language)" );
CalamaresUtils::Locale::TranslatedString ts1( map, "description" );
// The +1 is because "" is always also inserted
QCOMPARE( ts1.count(), someLanguages().count() + 1 );
QVERIFY( !ts1.isEmpty() );
QCOMPARE( ts1.get(), "description" ); // it wasn't set
QCOMPARE( ts1.get(), "description (no language)" ); // it wasn't set
QCOMPARE( ts1.get( QLocale( "nl" ) ), "description (language nl)" );
for ( const auto& language : someLanguages() )
{
@ -167,4 +192,10 @@ LocaleTests::testTranslatableConfig2()
CalamaresUtils::Locale::TranslatedString ts2( map, "name" );
// We skipped dutch this time
QCOMPARE( ts2.count(), someLanguages().count() );
QVERIFY( !ts2.isEmpty() );
// This key doesn't exist
CalamaresUtils::Locale::TranslatedString ts3( map, "front" );
QVERIFY( ts3.isEmpty() );
QCOMPARE( ts3.count(), 1 ); // The empty string
}

View File

@ -38,10 +38,6 @@ TranslatedString::TranslatedString( const QVariantMap& map, const QString& key )
{
// Get the un-decorated value for the key
QString value = CalamaresUtils::getString( map, key );
if ( value.isEmpty() )
{
value = key;
}
m_strings[ QString() ] = value;
for ( auto it = map.constKeyValueBegin(); it != map.constKeyValueEnd(); ++it )

View File

@ -46,9 +46,23 @@ public:
TranslatedString( const QString& string );
/// @brief Empty string
TranslatedString()
: TranslatedString( QString() ) {}
: TranslatedString( QString() )
{
}
/** @brief How many strings (translations) are there?
*
* This is always at least 1 (for the untranslated string),
* but may be more than 1 even when isEmpty() is true --
* if there is no untranslated version, for instance.
*/
int count() const { return m_strings.count(); }
/** @brief Consider this string empty?
*
* Only the state of the untranslated string is considered,
* so count() may be more than 1 even while the string is empty.
*/
bool isEmpty() const { return m_strings[ QString() ].isEmpty(); }
/// @brief Gets the string in the current locale
QString get() const;