QVariant: retain duplicate keys when converting

Task-number: QTBUG-49520
Change-Id: I3e15a26e0e424169ac2bffff1417e3f4398c2277
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@theqtcompany.com>
This commit is contained in:
Thiago Macieira 2015-11-18 11:54:03 -08:00
parent 4ad8798de4
commit 7499e642e0
2 changed files with 45 additions and 4 deletions

View File

@ -772,7 +772,7 @@ namespace QtPrivate {
QVariantHash l; QVariantHash l;
l.reserve(iter.size()); l.reserve(iter.size());
for (QAssociativeIterable::const_iterator it = iter.begin(), end = iter.end(); it != end; ++it) for (QAssociativeIterable::const_iterator it = iter.begin(), end = iter.end(); it != end; ++it)
l.insert(it.key().toString(), it.value()); l.insertMulti(it.key().toString(), it.value());
return l; return l;
} }
return QVariantValueHelper<QVariantHash>::invoke(v); return QVariantValueHelper<QVariantHash>::invoke(v);
@ -788,7 +788,7 @@ namespace QtPrivate {
QAssociativeIterable iter = QVariantValueHelperInterface<QAssociativeIterable>::invoke(v); QAssociativeIterable iter = QVariantValueHelperInterface<QAssociativeIterable>::invoke(v);
QVariantMap l; QVariantMap l;
for (QAssociativeIterable::const_iterator it = iter.begin(), end = iter.end(); it != end; ++it) for (QAssociativeIterable::const_iterator it = iter.begin(), end = iter.end(); it != end; ++it)
l.insert(it.key().toString(), it.value()); l.insertMulti(it.key().toString(), it.value());
return l; return l;
} }
return QVariantValueHelper<QVariantMap>::invoke(v); return QVariantValueHelper<QVariantMap>::invoke(v);

View File

@ -2,6 +2,7 @@
** **
** Copyright (C) 2015 The Qt Company Ltd. ** Copyright (C) 2015 The Qt Company Ltd.
** Copyright (C) 2015 Olivier Goffart <ogoffart@woboq.com> ** Copyright (C) 2015 Olivier Goffart <ogoffart@woboq.com>
** Copyright (C) 2015 Intel Corporation.
** Contact: http://www.qt.io/licensing/ ** Contact: http://www.qt.io/licensing/
** **
** This file is part of the test suite of the Qt Toolkit. ** This file is part of the test suite of the Qt Toolkit.
@ -2491,14 +2492,26 @@ void tst_QVariant::variantMap()
QVariant v = map; QVariant v = map;
QVariantMap map2 = qvariant_cast<QVariantMap>(v); QVariantMap map2 = qvariant_cast<QVariantMap>(v);
QCOMPARE(map2.value("test").toInt(), 42); QCOMPARE(map2.value("test").toInt(), 42);
QCOMPARE(map2, map);
map2 = v.toMap();
QCOMPARE(map2.value("test").toInt(), 42);
QCOMPARE(map2, map);
QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map); QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42); QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map); QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42); QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
// multi-keys
map.insertMulti("test", 47);
v = map;
map2 = qvariant_cast<QVariantMap>(v);
QCOMPARE(map2, map);
map2 = v.toMap();
QCOMPARE(map2, map);
} }
void tst_QVariant::variantHash() void tst_QVariant::variantHash()
@ -2508,14 +2521,26 @@ void tst_QVariant::variantHash()
QVariant v = hash; QVariant v = hash;
QVariantHash hash2 = qvariant_cast<QVariantHash>(v); QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
QCOMPARE(hash2.value("test").toInt(), 42); QCOMPARE(hash2.value("test").toInt(), 42);
QCOMPARE(hash2, hash);
hash2 = v.toHash();
QCOMPARE(hash2.value("test").toInt(), 42);
QCOMPARE(hash2, hash);
QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash); QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42); QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash); QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42); QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
// multi-keys
hash.insertMulti("test", 47);
v = hash;
hash2 = qvariant_cast<QVariantHash>(v);
QCOMPARE(hash2, hash);
hash2 = v.toHash();
QCOMPARE(hash2, hash);
} }
class CustomQObject : public QObject { class CustomQObject : public QObject {
@ -3233,24 +3258,40 @@ void tst_QVariant::convertIterables() const
map.insert("3", 4); map.insert("3", 4);
QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().count(), map.count()); QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().count(), map.count());
QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().count(), map.count()); QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().count(), map.count());
map.insertMulti("3", 5);
QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().count(), map.count());
QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().count(), map.count());
} }
{ {
QVariantMap map; QVariantMap map;
map.insert("3", 4); map.insert("3", 4);
QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().count(), map.count()); QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().count(), map.count());
QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().count(), map.count()); QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().count(), map.count());
map.insertMulti("3", 5);
QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().count(), map.count());
QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().count(), map.count());
} }
{ {
QHash<QString, int> hash; QHash<QString, int> hash;
hash.insert("3", 4); hash.insert("3", 4);
QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().count(), hash.count()); QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().count(), hash.count());
QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().count(), hash.count()); QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().count(), hash.count());
hash.insertMulti("3", 5);
QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().count(), hash.count());
QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().count(), hash.count());
} }
{ {
QVariantHash hash; QVariantHash hash;
hash.insert("3", 4); hash.insert("3", 4);
QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().count(), hash.count()); QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().count(), hash.count());
QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().count(), hash.count()); QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().count(), hash.count());
hash.insertMulti("3", 5);
QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().count(), hash.count());
QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().count(), hash.count());
} }
} }