[libcalamares] Add tests to string truncation

- check that basic manipulations succeed
- trailing-lines selection fails, though
main
Adriaan de Groot 4 years ago
parent 8cc114bf2c
commit 3be360e433

@ -126,6 +126,13 @@ QString
truncateMultiLine( const QString& string, CalamaresUtils::LinesStartEnd lines, CalamaresUtils::CharCount chars )
{
const int maxLines = lines.atStart + lines.atEnd;
if ( maxLines < 1 )
{
QString shorter( string );
shorter.truncate( chars.total );
return shorter;
}
if ( ( string.length() <= chars.total ) && ( string.count( '\n' ) <= maxLines ) )
{
return string;

@ -62,15 +62,22 @@ DLLEXPORT QString removeDiacritics( const QString& string );
*/
DLLEXPORT QString obscure( const QString& string );
/** @brief Parameter for counting lines at beginning and end of string
*
* This is used by truncateMultiLine() to indicate how many lines from
* the beginning and how many from the end should be kept.
*/
struct LinesStartEnd
{
int atStart;
int atEnd;
int atStart = 0;
int atEnd = 0;
};
/** @brief Parameter for counting characters in truncateMultiLine()
*/
struct CharCount
{
int total;
int total = 0;
};
/** @brief Truncate a string to some reasonable length for display

@ -13,6 +13,7 @@
#include "Entropy.h"
#include "Logger.h"
#include "RAII.h"
#include "String.h"
#include "Traits.h"
#include "UMask.h"
#include "Variant.h"
@ -63,6 +64,8 @@ private Q_SLOTS:
void testVariantStringListYAMLDashed();
void testVariantStringListYAMLBracketed();
/** @brief Test smart string truncation. */
void testStringTruncation();
private:
void recursiveCompareMap( const QVariantMap& a, const QVariantMap& b, int depth );
@ -495,6 +498,67 @@ strings: [ aap, noot, mies ]
QVERIFY( !getStringList( m, key ).contains( "lam" ) );
}
void
LibCalamaresTests::testStringTruncation()
{
using namespace CalamaresUtils;
const QString longString( R"(---
--- src/libcalamares/utils/String.h
+++ src/libcalamares/utils/String.h
@@ -62,15 +62,22 @@ DLLEXPORT QString removeDiacritics( const QString& string );
*/
DLLEXPORT QString obscure( const QString& string );
+/** @brief Parameter for counting lines at beginning and end of string
+ *
+ * This is used by truncateMultiLine() to indicate how many lines from
+ * the beginning and how many from the end should be kept.
+ */
struct LinesStartEnd
{
- int atStart;
- int atEnd;
+ int atStart = 0;
+ int atEnd = 0;
)" );
const int sufficientLength = 812;
// There's 18 lines in all
QCOMPARE( longString.count( '\n' ), 18 );
QVERIFY( longString.length() < sufficientLength );
// If we ask for more, we get everything back
QCOMPARE( longString, truncateMultiLine( longString, LinesStartEnd { 20, 0 }, CharCount { sufficientLength } ) );
QCOMPARE( longString, truncateMultiLine( longString, LinesStartEnd { 0, 20 }, CharCount { sufficientLength } ) );
// If we ask for no lines, only characters, we get that
{
auto s = truncateMultiLine( longString, LinesStartEnd { 0, 0 }, CharCount { 4 } );
QCOMPARE( s.length(), 4 );
QCOMPARE( s, QString( "---\n" ) );
}
{
auto s = truncateMultiLine( longString, LinesStartEnd { 0, 0 }, CharCount { sufficientLength } );
QCOMPARE( s, longString );
}
// Lines at the start
{
auto s = truncateMultiLine( longString, LinesStartEnd { 4, 0 }, CharCount { sufficientLength } );
QVERIFY( longString.startsWith( s ) );
QCOMPARE( s.count( '\n' ), 4 );
}
// Lines at the end
{
auto s = truncateMultiLine( longString, LinesStartEnd { 0, 4 }, CharCount { sufficientLength } );
QVERIFY( longString.endsWith( s ) );
QCOMPARE( s.count( '\n' ), 4 );
}
}
QTEST_GUILESS_MAIN( LibCalamaresTests )
#include "utils/moc-warnings.h"

Loading…
Cancel
Save