summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorChristian Ehrlicher <ch.ehrlicher@gmx.de>2021-01-03 21:40:52 +0100
committerChristian Ehrlicher <ch.ehrlicher@gmx.de>2021-01-15 18:51:15 +0100
commitc23e8cb582219a8e03c97c55a530625a41c68832 (patch)
tree0ac484877841ac7c2c283a8af177756cd52065d0 /tests/auto
parent539553a57216c4ece292ff54fb623807c334cdb6 (diff)
QSettings: Add support for QMetaType::Float
When writing out a float value, the output string is encoded as QVariant for no reason. Looks like an oversight when QMetaType::Float was added a long time ago. Fixes: QTBUG-21156 Change-Id: I7f5d31e15892d700c1b1e5e731b7733ce3a15730 Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/corelib/io/qsettings/CMakeLists.txt1
-rw-r--r--tests/auto/corelib/io/qsettings/float.ini3
-rw-r--r--tests/auto/corelib/io/qsettings/qsettings.qrc1
-rw-r--r--tests/auto/corelib/io/qsettings/tst_qsettings.cpp23
4 files changed, 28 insertions, 0 deletions
diff --git a/tests/auto/corelib/io/qsettings/CMakeLists.txt b/tests/auto/corelib/io/qsettings/CMakeLists.txt
index c12f94cf36..55a360166b 100644
--- a/tests/auto/corelib/io/qsettings/CMakeLists.txt
+++ b/tests/auto/corelib/io/qsettings/CMakeLists.txt
@@ -26,6 +26,7 @@ set(qsettings_resource_files
"resourcefile5.ini"
"resourcefile6.plist"
"withcomments.ini"
+ "float.ini"
)
qt_internal_add_resource(tst_qsettings "qsettings"
diff --git a/tests/auto/corelib/io/qsettings/float.ini b/tests/auto/corelib/io/qsettings/float.ini
new file mode 100644
index 0000000000..bf9312c14c
--- /dev/null
+++ b/tests/auto/corelib/io/qsettings/float.ini
@@ -0,0 +1,3 @@
+[test]
+float=0.5
+float_qvariant=@Variant(\0\0\0\x87?\0\0\0)
diff --git a/tests/auto/corelib/io/qsettings/qsettings.qrc b/tests/auto/corelib/io/qsettings/qsettings.qrc
index db1d8c663f..0501206c05 100644
--- a/tests/auto/corelib/io/qsettings/qsettings.qrc
+++ b/tests/auto/corelib/io/qsettings/qsettings.qrc
@@ -8,5 +8,6 @@
<file>resourcefile6.plist</file>
<file>bom.ini</file>
<file>withcomments.ini</file>
+ <file>float.ini</file>
</qresource>
</RCC>
diff --git a/tests/auto/corelib/io/qsettings/tst_qsettings.cpp b/tests/auto/corelib/io/qsettings/tst_qsettings.cpp
index eef0a7ca2a..9fd7b255d7 100644
--- a/tests/auto/corelib/io/qsettings/tst_qsettings.cpp
+++ b/tests/auto/corelib/io/qsettings/tst_qsettings.cpp
@@ -189,6 +189,7 @@ private slots:
void embeddedZeroByte_data();
void embeddedZeroByte();
void spaceAfterComment();
+ void floatAsQVariant();
void testXdg();
private:
@@ -765,6 +766,20 @@ void tst_QSettings::spaceAfterComment()
settings.endGroup();
}
+// test if a qvariant-encoded float can be read
+void tst_QSettings::floatAsQVariant()
+{
+ QVERIFY(QFile::exists(":/float.ini"));
+ QSettings s(":/float.ini", QSettings::IniFormat);
+
+ s.beginGroup("test");
+ QCOMPARE(s.value("float").toDouble(), 0.5);
+ QCOMPARE(s.value("float_qvariant").toDouble(), 0.5);
+
+ QCOMPARE(s.value("float").toFloat(), 0.5);
+ QCOMPARE(s.value("float_qvariant").toFloat(), 0.5);
+}
+
void tst_QSettings::testErrorHandling_data()
{
QTest::addColumn<int>("filePerms"); // -1 means file should not exist
@@ -1090,6 +1105,14 @@ void tst_QSettings::setValue()
settings.setValue("key 2", QString("false"));
QCOMPARE(settings.value("key 2", true).toBool(), false);
+ settings.setValue("key 2", double(1234.56));
+ QCOMPARE(settings.value("key 2").toDouble(), double(1234.56));
+ QCOMPARE(settings.value("key 2").toString().left(7), QString::number(double(1234.56)));
+
+ settings.setValue("key 2", float(1234.56));
+ QCOMPARE(settings.value("key 2").toFloat(), float(1234.56));
+ QCOMPARE(settings.value("key 2").toString().left(7), QString::number(float(1234.56)));
+
// The following block should not compile.
/*
settings.setValue("key 2", "true");