diff --git a/src/libcalamares/utils/Tests.cpp b/src/libcalamares/utils/Tests.cpp index ec7f6889a..862b4d0d7 100644 --- a/src/libcalamares/utils/Tests.cpp +++ b/src/libcalamares/utils/Tests.cpp @@ -25,6 +25,7 @@ #include "RAII.h" #include "Traits.h" #include "UMask.h" +#include "Variant.h" #include "Yaml.h" #include "GlobalStorage.h" @@ -68,6 +69,11 @@ private Q_SLOTS: /** @brief Tests the Traits bits. */ void testTraits(); + void testVariantStringListCode(); + void testVariantStringListYAMLDashed(); + void testVariantStringListYAMLBracketed(); + + private: void recursiveCompareMap( const QVariantMap& a, const QVariantMap& b, int depth ); }; @@ -351,49 +357,136 @@ LibCalamaresTests::testBoolSetter() * which should call do_the_thing() of its argument if it exists. */ -struct c1 { int do_the_thing() { return 2; } }; -struct c2 { }; +struct c1 +{ + int do_the_thing() { return 2; } +}; +struct c2 +{ +}; -DECLARE_HAS_METHOD(do_the_thing) +DECLARE_HAS_METHOD( do_the_thing ) struct Thinginator { public: /// When class T has function do_the_thing() - template< class T > int thingify( T& t, const std::true_type& ) + template < class T > + int thingify( T& t, const std::true_type& ) { return t.do_the_thing(); } - template< class T > int thingify( T&, const std::false_type& ) + template < class T > + int thingify( T&, const std::false_type& ) { return -1; } - template< class T > int thingify( T& t ) + template < class T > + int thingify( T& t ) { - return thingify(t, has_do_the_thing{}); + return thingify( t, has_do_the_thing< T > {} ); } -} ; +}; void LibCalamaresTests::testTraits() { - has_do_the_thing x{}; - has_do_the_thing y{}; + has_do_the_thing< c1 > x {}; + has_do_the_thing< c2 > y {}; - QVERIFY(x); - QVERIFY(!y); + QVERIFY( x ); + QVERIFY( !y ); - c1 c1{}; - c2 c2{}; + c1 c1 {}; + c2 c2 {}; - QCOMPARE(c1.do_the_thing(), 2); + QCOMPARE( c1.do_the_thing(), 2 ); Thinginator t; - QCOMPARE(t.thingify(c1), 2); // Calls c1::do_the_thing() - QCOMPARE(t.thingify(c2), -1); + QCOMPARE( t.thingify( c1 ), 2 ); // Calls c1::do_the_thing() + QCOMPARE( t.thingify( c2 ), -1 ); +} + +void +LibCalamaresTests::testVariantStringListCode() +{ + using namespace CalamaresUtils; + const QString key( "strings" ); + { + // Things that are not stringlists + QVariantMap m; + QCOMPARE( getStringList( m, key ), QStringList {} ); + m.insert( key, 17 ); + QCOMPARE( getStringList( m, key ), QStringList {} ); + m.insert( key, QString( "more strings" ) ); + QCOMPARE( getStringList( m, key ), QStringList {} ); + m.insert( key, QVariant {} ); + QCOMPARE( getStringList( m, key ), QStringList {} ); + } + + { + // Things that are stringlists + QVariantMap m; + m.insert( key, QStringList { "aap", "noot" } ); + QVERIFY( getStringList( m, key ).contains( "aap" ) ); + QVERIFY( !getStringList( m, key ).contains( "mies" ) ); + } +} + +void +LibCalamaresTests::testVariantStringListYAMLDashed() +{ + using namespace CalamaresUtils; + const QString key( "strings" ); + + // Looks like a stringlist to me + QTemporaryFile f; + QVERIFY( f.open() ); + f.write( R"(--- +strings: + - aap + - noot + - mies +)" ); + f.close(); + bool ok = false; + QVariantMap m = loadYaml( f.fileName(), &ok ); + + QVERIFY( ok ); + QCOMPARE( m.count(), 1 ); + QVERIFY( m.contains( key ) ); + + QVERIFY( getStringList( m, key ).contains( "aap" ) ); + QVERIFY( getStringList( m, key ).contains( "mies" ) ); + QVERIFY( getStringList( m, key ).contains( "lam" ) ); +} + +void +LibCalamaresTests::testVariantStringListYAMLBracketed() +{ + using namespace CalamaresUtils; + const QString key( "strings" ); + + // Looks like a stringlist to me + QTemporaryFile f; + QVERIFY( f.open() ); + f.write( R"(--- +strings: [ aap, noot, mies ] +)" ); + f.close(); + bool ok = false; + QVariantMap m = loadYaml( f.fileName(), &ok ); + + QVERIFY( ok ); + QCOMPARE( m.count(), 1 ); + QVERIFY( m.contains( key ) ); + + QVERIFY( getStringList( m, key ).contains( "aap" ) ); + QVERIFY( getStringList( m, key ).contains( "mies" ) ); + QVERIFY( getStringList( m, key ).contains( "lam" ) ); } QTEST_GUILESS_MAIN( LibCalamaresTests )