Skip to content

Commit

Permalink
add test for QgsSettings::enumSettingValue
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Feb 17, 2018
1 parent 1198dba commit bda67ad
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/core/qgssettings.h
Expand Up @@ -221,8 +221,9 @@ class CORE_EXPORT QgsSettings : public QObject
#ifndef SIP_RUN

/**
* Return the setting value for a setting defined on an enum.
* Return the setting value for a setting based on an enum.
* This forces the output to be a valid and existing entry of the enum.
* Hence if the setting value is incorrect, the given default value is returned.
* \note The enum needs to be declared with Q_ENUM
*/
template <class T>
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -165,6 +165,7 @@ SET(TESTS
testqgsrectangle.cpp
testqgsrenderers.cpp
testqgsrulebasedrenderer.cpp
testqgssettings.cpp
testqgsshapeburst.cpp
testqgssimplemarker.cpp
testqgssnappingutils.cpp
Expand Down
56 changes: 56 additions & 0 deletions tests/src/core/testqgssettings.cpp
@@ -0,0 +1,56 @@
/***************************************************************************
testqgssettings.cpp
--------------------------------------
Date : 17.02.2018
Copyright : (C) 2018 by Denis Rouzaud
Email : denis.rouzaud@gmail.com
***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QObject>


#include "qgssettings.h"
#include "qgsunittypes.h"
#include "qgstest.h"


/**
* \ingroup UnitTests
* This is a unit test for the operations on curve geometries
*/
class TestQgsSettings : public QObject
{
Q_OBJECT

private slots:
void enumSettingValue();
};


void TestQgsSettings::enumSettingValue()
{
QgsSettings settings;

// assign to inexisting value
settings.setValue( QStringLiteral( "qgis/testing/my_value_for_units" ), -1 );
// just to be sure it really doesn't exist
QVERIFY( static_cast<int>( QgsUnitTypes::LayoutMeters ) != -1 );

// standard method returns invalid value
int v1 = settings.value( QStringLiteral( "qgis/testing/my_value_for_units" ), QgsUnitTypes::LayoutMeters ).toInt();
QCOMPARE( v1, -1 );

// enum method returns default value if current setting is incorrect
QgsUnitTypes::LayoutUnit v2 = settings.enumSettingValue( QStringLiteral( "qgis/testing/my_value_for_units" ), QgsUnitTypes::LayoutMeters );
QCOMPARE( v2, QgsUnitTypes::LayoutMeters );
}


QGSTEST_MAIN( TestQgsSettings )
#include "testqgssettings.moc"

0 comments on commit bda67ad

Please sign in to comment.