diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index 87338ae6c..f942dbde1 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -207,6 +207,16 @@ if ( ECM_FOUND AND BUILD_TESTING ) Qt5::Test ) calamares_automoc( libcalamaresnetworktest ) + + ecm_add_test( + modulesystem/Tests.cpp + TEST_NAME + libcalamaresmodulesystemtest + LINK_LIBRARIES + calamares + Qt5::Test + ) + calamares_automoc( libcalamaresmodulesystemtest ) endif() if( BUILD_TESTING ) diff --git a/src/libcalamares/modulesystem/InstanceKey.h b/src/libcalamares/modulesystem/InstanceKey.h index 8e04dbaef..80be23941 100644 --- a/src/libcalamares/modulesystem/InstanceKey.h +++ b/src/libcalamares/modulesystem/InstanceKey.h @@ -52,12 +52,14 @@ public: { second = first; } + validate(); } /// @brief Create "usual" instances keys `module@module` explicit InstanceKey( const QString& module ) : QPair( module, module ) { + validate(); } /// @brief Create unusual, invalid instance key @@ -89,6 +91,16 @@ public: return InstanceKey( moduleEntrySplit.first(), moduleEntrySplit.last() ); } +private: + /** @brief Check validity and reset module and id if needed. */ + void validate() + { + if ( first.contains( '@' ) || second.contains( '@' ) ) + { + first = QString(); + second = QString(); + } + } }; } diff --git a/src/libcalamares/modulesystem/Tests.cpp b/src/libcalamares/modulesystem/Tests.cpp new file mode 100644 index 000000000..b54d020c5 --- /dev/null +++ b/src/libcalamares/modulesystem/Tests.cpp @@ -0,0 +1,129 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "modulesystem/InstanceKey.h" + +#include + +using Calamares::ModuleSystem::InstanceKey; + +class ModuleSystemTests : public QObject +{ + Q_OBJECT +public: + ModuleSystemTests() {} + virtual ~ModuleSystemTests() {} + +private Q_SLOTS: + void initTestCase(); + + void testEmptyInstanceKey(); + void testSimpleInstanceKey(); + void testCustomInstanceKey(); + void testFromStringInstanceKey(); + + void testBadSimpleCases(); + void testBadFromStringCases(); +}; + +void ModuleSystemTests::initTestCase() +{ +} + +void assert_is_invalid( const InstanceKey& k ) +{ + QVERIFY( !k.isValid() ); + QVERIFY( !k.isCustom() ); + QVERIFY( k.module().isEmpty() ); + QVERIFY( k.id().isEmpty() ); +} + +void ModuleSystemTests::testEmptyInstanceKey() +{ + InstanceKey k0; + assert_is_invalid( k0 ); +} + +void ModuleSystemTests::testSimpleInstanceKey() +{ + InstanceKey k1( "derp" ); + QVERIFY( k1.isValid() ); + QVERIFY( !k1.isCustom() ); + QCOMPARE( k1.module(), QStringLiteral( "derp" ) ); + QCOMPARE( k1.id(), QStringLiteral( "derp" ) ); +} + +void ModuleSystemTests::testCustomInstanceKey() +{ + InstanceKey k0("derp", "derp"); + QVERIFY( k0.isValid() ); + QVERIFY( !k0.isCustom() ); + QCOMPARE( k0.module(), QStringLiteral( "derp" ) ); + QCOMPARE( k0.id(), QStringLiteral( "derp" ) ); + + InstanceKey k1("derp", "horse"); + QVERIFY( k1.isValid() ); + QVERIFY( k1.isCustom() ); + QCOMPARE( k1.module(), QStringLiteral( "derp" ) ); + QCOMPARE( k1.id(), QStringLiteral( "horse" ) ); +} + +void ModuleSystemTests::testFromStringInstanceKey() +{ + InstanceKey k0 = InstanceKey::fromString( "derp@derp" ); + QVERIFY( k0.isValid() ); + QVERIFY( !k0.isCustom() ); + QCOMPARE( k0.module(), QStringLiteral( "derp" ) ); + QCOMPARE( k0.id(), QStringLiteral( "derp" ) ); + + InstanceKey k1 = InstanceKey::fromString( "derp@horse" ); + QVERIFY( k1.isValid() ); + QVERIFY( k1.isCustom() ); + QCOMPARE( k1.module(), QStringLiteral( "derp" ) ); + QCOMPARE( k1.id(), QStringLiteral( "horse" ) ); + + InstanceKey k2 = InstanceKey::fromString( "derp" ); + QVERIFY( k2.isValid() ); + QVERIFY( !k2.isCustom() ); + QCOMPARE( k2.module(), QStringLiteral( "derp" ) ); + QCOMPARE( k2.id(), QStringLiteral( "derp" ) ); +} + +/// @brief These are expected to fail since they show bugs in the code +void ModuleSystemTests::testBadSimpleCases() +{ + InstanceKey k2( "derp@derp" ); + assert_is_invalid( k2 ); + + InstanceKey k3( "derp@horse" ); + assert_is_invalid( k3 ); +} + +void ModuleSystemTests::testBadFromStringCases() +{ + InstanceKey k0 = InstanceKey::fromString( QString() ); + assert_is_invalid( k0 ); + + k0 = InstanceKey::fromString( "derp@derp@derp" ); + assert_is_invalid( k0 ); +} + + +QTEST_GUILESS_MAIN( ModuleSystemTests ) + +#include "Tests.moc"