From 3623e9aefc0864ca8be7ba384340845aa06558c3 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 28 Jan 2021 14:47:03 +0100 Subject: [PATCH] [libcalamares] Extend tests of string-truncation --- src/libcalamares/utils/Tests.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/libcalamares/utils/Tests.cpp b/src/libcalamares/utils/Tests.cpp index 3493f9579..539113d91 100644 --- a/src/libcalamares/utils/Tests.cpp +++ b/src/libcalamares/utils/Tests.cpp @@ -66,6 +66,7 @@ private Q_SLOTS: /** @brief Test smart string truncation. */ void testStringTruncation(); + void testStringTruncationShorter(); private: void recursiveCompareMap( const QVariantMap& a, const QVariantMap& b, int depth ); @@ -573,11 +574,41 @@ LibCalamaresTests::testStringTruncation() auto firsttwo = truncateMultiLine( s, LinesStartEnd { 2, 0 }, CharCount { sufficientLength } ); auto lasttwo = truncateMultiLine( s, LinesStartEnd { 0, 2 }, CharCount { sufficientLength } ); QCOMPARE( firsttwo + lasttwo, s ); + QCOMPARE( firsttwo.count( '\n' ), 2 ); QVERIFY( longString.startsWith( firsttwo ) ); QVERIFY( longString.endsWith( lasttwo ) ); } } +void +LibCalamaresTests::testStringTruncationShorter() +{ + Logger::setupLogLevel( Logger::LOGDEBUG ); + + using namespace CalamaresUtils; + + const QString longString( R"(Some strange string artifacts appeared, leading to `{1?}` being +displayed in various user-facing messages. These have been removed +and the translations updated.)" ); + const char NEWLINE = '\n'; + + const int insufficientLength = 42; + // There's 2 newlines in all, no trailing newline + QVERIFY( !longString.endsWith( NEWLINE ) ); + QCOMPARE( longString.count( NEWLINE ), 2 ); + QVERIFY( longString.length() > insufficientLength ); + + // Grab first line, untruncated + { + + auto s = truncateMultiLine( longString, LinesStartEnd { 1, 0 } ); + QVERIFY( s.length() > 1 ); + QVERIFY( longString.startsWith( s ) ); + QVERIFY( s.endsWith( NEWLINE ) ); + QVERIFY( s.endsWith( "being\n" ) ); + } +} + QTEST_GUILESS_MAIN( LibCalamaresTests )