Skip to content

Commit bda67ad

Browse files
committedFeb 17, 2018
add test for QgsSettings::enumSettingValue
1 parent 1198dba commit bda67ad

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed
 

‎src/core/qgssettings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,9 @@ class CORE_EXPORT QgsSettings : public QObject
221221
#ifndef SIP_RUN
222222

223223
/**
224-
* Return the setting value for a setting defined on an enum.
224+
* Return the setting value for a setting based on an enum.
225225
* This forces the output to be a valid and existing entry of the enum.
226+
* Hence if the setting value is incorrect, the given default value is returned.
226227
* \note The enum needs to be declared with Q_ENUM
227228
*/
228229
template <class T>

‎tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ SET(TESTS
165165
testqgsrectangle.cpp
166166
testqgsrenderers.cpp
167167
testqgsrulebasedrenderer.cpp
168+
testqgssettings.cpp
168169
testqgsshapeburst.cpp
169170
testqgssimplemarker.cpp
170171
testqgssnappingutils.cpp

‎tests/src/core/testqgssettings.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/***************************************************************************
2+
testqgssettings.cpp
3+
--------------------------------------
4+
Date : 17.02.2018
5+
Copyright : (C) 2018 by Denis Rouzaud
6+
Email : denis.rouzaud@gmail.com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#include <QObject>
16+
17+
18+
#include "qgssettings.h"
19+
#include "qgsunittypes.h"
20+
#include "qgstest.h"
21+
22+
23+
/**
24+
* \ingroup UnitTests
25+
* This is a unit test for the operations on curve geometries
26+
*/
27+
class TestQgsSettings : public QObject
28+
{
29+
Q_OBJECT
30+
31+
private slots:
32+
void enumSettingValue();
33+
};
34+
35+
36+
void TestQgsSettings::enumSettingValue()
37+
{
38+
QgsSettings settings;
39+
40+
// assign to inexisting value
41+
settings.setValue( QStringLiteral( "qgis/testing/my_value_for_units" ), -1 );
42+
// just to be sure it really doesn't exist
43+
QVERIFY( static_cast<int>( QgsUnitTypes::LayoutMeters ) != -1 );
44+
45+
// standard method returns invalid value
46+
int v1 = settings.value( QStringLiteral( "qgis/testing/my_value_for_units" ), QgsUnitTypes::LayoutMeters ).toInt();
47+
QCOMPARE( v1, -1 );
48+
49+
// enum method returns default value if current setting is incorrect
50+
QgsUnitTypes::LayoutUnit v2 = settings.enumSettingValue( QStringLiteral( "qgis/testing/my_value_for_units" ), QgsUnitTypes::LayoutMeters );
51+
QCOMPARE( v2, QgsUnitTypes::LayoutMeters );
52+
}
53+
54+
55+
QGSTEST_MAIN( TestQgsSettings )
56+
#include "testqgssettings.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.