[libcalamares] Test GS

- test insert, remove, emitted signals
- test loading and saving of YAML and JSON

This shows up a big bug in the YAML saving code (which was never
used, it seems, anyway)
This commit is contained in:
Adriaan de Groot 2020-08-07 00:45:36 +02:00
parent a44e6802e5
commit dbc49f001e
2 changed files with 126 additions and 4 deletions

View File

@ -1,6 +1,7 @@
# === This file is part of Calamares - <https://github.com/calamares> ===
#
# SPDX-FileCopyrightText: 2020 Adriaan de Groot <groot@kde.org>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Calamares is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -15,12 +16,12 @@
# You should have received a copy of the GNU General Public License
# along with Calamares. If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
# License-Filename: LICENSE
#
#
# libcalamares is the non-GUI part of Calamares, which includes handling
# translations, configurations, logging, utilities, global storage, and (non-GUI) jobs.
# translations, configurations, logging, utilities, global storage, and
# (non-GUI) jobs.
#
add_definitions( -DDLLEXPORT_PRO )
include_directories( ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} )
@ -215,6 +216,12 @@ endforeach()
### TESTING
#
#
calamares_add_test(
libcalamarestest
SOURCES
Tests.cpp
)
calamares_add_test(
libcalamaresutilstest
SOURCES

115
src/libcalamares/Tests.cpp Normal file
View File

@ -0,0 +1,115 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* SPDX-FileCopyrightText: 2018-2020 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
#include "GlobalStorage.h"
#include <QObject>
#include <QSignalSpy>
#include <QtTest/QtTest>
class TestLibCalamares : public QObject
{
Q_OBJECT
public:
TestLibCalamares() {}
virtual ~TestLibCalamares() {}
private Q_SLOTS:
void testGSModify();
void testGSLoadSave();
};
void
TestLibCalamares::testGSModify()
{
Calamares::GlobalStorage gs;
QSignalSpy spy( &gs, &Calamares::GlobalStorage::changed );
const QString key( "derp" );
QCOMPARE( gs.count(), 0 );
QVERIFY( !gs.contains( key ) );
const int value = 17;
gs.insert( key, value );
QCOMPARE( gs.count(), 1 );
QVERIFY( gs.contains( key ) );
QCOMPARE( gs.value( key ).type(), QVariant::Int );
QCOMPARE( gs.value( key ).toString(), QString( "17" ) ); // It isn't a string, but does convert
QCOMPARE( gs.value( key ).toInt(), value );
gs.remove( key );
QCOMPARE( gs.count(), 0 );
QVERIFY( !gs.contains( key ) );
QCOMPARE( spy.count(), 2 ); // one insert, one remove
}
void
TestLibCalamares::testGSLoadSave()
{
Calamares::GlobalStorage gs;
const QString jsonfilename( "gs.test.json" );
const QString yamlfilename( "gs.test.yaml" );
gs.insert( "derp", 17 );
gs.insert( "cow", "moo" );
gs.insert( "dwarfs", QStringList { "dopey", "sneezy" } );
QCOMPARE( gs.count(), 3 );
QVERIFY( gs.saveJson( jsonfilename ) );
Calamares::GlobalStorage gs2;
QCOMPARE( gs2.count(), 0 );
QVERIFY( gs2.loadJson( jsonfilename ) );
QCOMPARE( gs2.count(), 3 );
QCOMPARE( gs2.data(), gs.data() );
QVERIFY( gs.saveYaml( yamlfilename ) );
Calamares::GlobalStorage gs3;
QCOMPARE( gs3.count(), 0 );
QVERIFY( gs3.loadYaml( jsonfilename ) );
QCOMPARE( gs3.count(), 3 );
QCOMPARE( gs3.data(), gs.data() );
// Failures in loading
QVERIFY( !gs3.loadYaml( jsonfilename ) );
QVERIFY( !gs3.loadJson( yamlfilename ) );
Calamares::GlobalStorage gs4;
gs4.insert( "derp", 32 );
gs4.insert( "dorp", "Varsseveld" );
QCOMPARE( gs4.count(), 2 );
QVERIFY( gs4.contains( "dorp" ) );
QCOMPARE( gs4.value( "derp" ).toInt(), 32 );
QVERIFY( gs4.loadJson( jsonfilename ) );
// 3 keys from the file, but one overwrite
QCOMPARE( gs4.count(), 4 );
QVERIFY( gs4.contains( "dorp" ) );
QCOMPARE( gs4.value( "derp" ).toInt(), 17 ); // This one was overwritten
}
QTEST_GUILESS_MAIN( TestLibCalamares )
#include "utils/moc-warnings.h"
#include "Tests.moc"