[libcalamares] Test for Variant methods

In advance of PR #1491, test loading and stringlist extraction.

- from code, extraction works "normally"
- for YAML data, the stringlist isn't actually a stringlist
This commit is contained in:
Adriaan de Groot 2020-08-18 16:20:34 +02:00
parent 117859082e
commit e55def87e9

View File

@ -25,6 +25,7 @@
#include "RAII.h" #include "RAII.h"
#include "Traits.h" #include "Traits.h"
#include "UMask.h" #include "UMask.h"
#include "Variant.h"
#include "Yaml.h" #include "Yaml.h"
#include "GlobalStorage.h" #include "GlobalStorage.h"
@ -68,6 +69,11 @@ private Q_SLOTS:
/** @brief Tests the Traits bits. */ /** @brief Tests the Traits bits. */
void testTraits(); void testTraits();
void testVariantStringListCode();
void testVariantStringListYAMLDashed();
void testVariantStringListYAMLBracketed();
private: private:
void recursiveCompareMap( const QVariantMap& a, const QVariantMap& b, int depth ); 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. * which should call do_the_thing() of its argument if it exists.
*/ */
struct c1 { int do_the_thing() { return 2; } }; struct c1
struct c2 { }; {
int do_the_thing() { return 2; }
};
struct c2
{
};
DECLARE_HAS_METHOD(do_the_thing) DECLARE_HAS_METHOD( do_the_thing )
struct Thinginator struct Thinginator
{ {
public: public:
/// When class T has function do_the_thing() /// 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(); 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; return -1;
} }
template< class T > int thingify( T& t ) template < class T >
int thingify( T& t )
{ {
return thingify(t, has_do_the_thing<T>{}); return thingify( t, has_do_the_thing< T > {} );
} }
} ; };
void void
LibCalamaresTests::testTraits() LibCalamaresTests::testTraits()
{ {
has_do_the_thing<c1> x{}; has_do_the_thing< c1 > x {};
has_do_the_thing<c2> y{}; has_do_the_thing< c2 > y {};
QVERIFY(x); QVERIFY( x );
QVERIFY(!y); QVERIFY( !y );
c1 c1{}; c1 c1 {};
c2 c2{}; c2 c2 {};
QCOMPARE(c1.do_the_thing(), 2); QCOMPARE( c1.do_the_thing(), 2 );
Thinginator t; Thinginator t;
QCOMPARE(t.thingify(c1), 2); // Calls c1::do_the_thing() QCOMPARE( t.thingify( c1 ), 2 ); // Calls c1::do_the_thing()
QCOMPARE(t.thingify(c2), -1); 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 ) QTEST_GUILESS_MAIN( LibCalamaresTests )