From bca316299e2210fafe05e9acd8267ea05d0196c1 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Sun, 22 Mar 2020 21:08:32 +0100 Subject: [PATCH] [netinstall] Add tests - Just some simple tests for the Items - Test creation of package group from variant - This needs Qt5::Gui to link because QStandardItem is a GUI class, although we can run the tests without a GUI. --- src/modules/netinstall/CMakeLists.txt | 11 +++ src/modules/netinstall/Tests.cpp | 118 ++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 src/modules/netinstall/Tests.cpp diff --git a/src/modules/netinstall/CMakeLists.txt b/src/modules/netinstall/CMakeLists.txt index c5eddd32b..9d0167670 100644 --- a/src/modules/netinstall/CMakeLists.txt +++ b/src/modules/netinstall/CMakeLists.txt @@ -16,3 +16,14 @@ calamares_add_plugin( netinstall yamlcpp SHARED_LIB ) + +calamares_add_test( + netinstalltest + SOURCES + Tests.cpp + PackageTreeItem.cpp + PackageModel.cpp + LIBRARIES + Qt5::Gui +) + diff --git a/src/modules/netinstall/Tests.cpp b/src/modules/netinstall/Tests.cpp new file mode 100644 index 000000000..d1d73d7fe --- /dev/null +++ b/src/modules/netinstall/Tests.cpp @@ -0,0 +1,118 @@ +/* === This file is part of Calamares - === + * + * Copyright 2020, 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 "PackageTreeItem.h" + +#include "utils/Logger.h" +#include "utils/Variant.h" +#include "utils/Yaml.h" + +#include + +class ItemTests : public QObject +{ + Q_OBJECT +public: + ItemTests(); + virtual ~ItemTests() {} + +private Q_SLOTS: + void initTestCase(); + + void testRoot(); + void testPackage(); + void testGroup(); +}; + +ItemTests::ItemTests() {} + +void +ItemTests::initTestCase() +{ + Logger::setupLogLevel( Logger::LOGDEBUG ); +} + +void +ItemTests::testRoot() +{ + PackageTreeItem r; + + QCOMPARE( r.isSelected(), Qt::Checked ); + QCOMPARE( r.name(), QStringLiteral( "" ) ); + QCOMPARE( r.parentItem(), nullptr ); +} + +void +ItemTests::testPackage() +{ + PackageTreeItem p( "bash", nullptr ); + + QCOMPARE( p.isSelected(), Qt::Unchecked ); + QCOMPARE( p.packageName(), QStringLiteral( "bash" ) ); + QVERIFY( p.name().isEmpty() ); // not a group + QCOMPARE( p.parentItem(), nullptr ); + QCOMPARE( p.childCount(), 0 ); + QVERIFY( !p.isHidden() ); + QVERIFY( !p.isCritical() ); + + // This doesn't happen in normal constructions, + // because a package can't have children. + PackageTreeItem c( "zsh", &p ); + QCOMPARE( c.isSelected(), Qt::Unchecked ); + QCOMPARE( c.packageName(), QStringLiteral( "zsh" ) ); + QVERIFY( c.name().isEmpty() ); // not a group + QCOMPARE( c.parentItem(), &p ); + + QCOMPARE( p.childCount(), 0 ); // not noticed it has a child +} + +// *INDENT-OFF* +// clang-format off +static const char doc[] = +"- name: \"CCR\"\n" +" description: \"Tools for the Chakra Community Repository\"\n" +" packages:\n" +" - ccr\n" +" - base-devel\n"; +// *INDENT-ON* +// clang-format on + +void +ItemTests::testGroup() +{ + YAML::Node yamldoc = YAML::Load( doc ); + QVariantList yamlContents = CalamaresUtils::yamlSequenceToVariant( yamldoc ); + + QCOMPARE( yamlContents.length(), 1 ); + + PackageTreeItem p( yamlContents[ 0 ].toMap(), nullptr ); + QCOMPARE( p.name(), QStringLiteral( "CCR" ) ); + QVERIFY( p.packageName().isEmpty() ); + QVERIFY( p.description().startsWith( QStringLiteral( "Tools " ) ) ); + QCOMPARE( p.parentItem(), nullptr ); + QVERIFY( !p.isHidden() ); + QVERIFY( !p.isCritical() ); + // The item-constructor doesn't consider the packages: list + QCOMPARE( p.childCount(), 0 ); +} + +QTEST_GUILESS_MAIN( ItemTests ) + +#include "utils/moc-warnings.h" + +#include "Tests.moc"