diff options
author | Stephen Kelly <stephen.kelly@kdab.com> | 2012-02-29 15:07:12 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-03-02 13:22:54 +0100 |
commit | fb8c95bac09910c4dfa476ab97c6206b2e94ee53 (patch) | |
tree | 0b98b1f18f2c2dc7fd68fe3e2b30c8086f209bb3 /tests | |
parent | 012f799254eedc610e2e33842e62d2a184b14fcc (diff) |
Automatic metatype registration of two-template-argument types.
This commit is complimentary to the commit which introduced a similar
partial specialization for single template argument types:
6b4f8a68c8da1af7c5be7dc6075b688c9d6ca55f
If T and U are available as metatypes, then QHash<T, U> is too.
Change-Id: I09097b954666418b424c8c23577032beb814343a
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index 06919c6f58..f8403f11a1 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -1053,6 +1053,61 @@ void tst_QMetaType::registerStreamBuiltin() Q_DECLARE_METATYPE(QSharedPointer<QObject>) +typedef QHash<int, uint> IntUIntHash; +Q_DECLARE_METATYPE(IntUIntHash) +typedef QMap<int, uint> IntUIntMap; +Q_DECLARE_METATYPE(IntUIntMap) +typedef QPair<int, uint> IntUIntPair; +Q_DECLARE_METATYPE(IntUIntPair) + +struct CustomComparable +{ + CustomComparable(int i_ = 0) :i(i_) { } + bool operator==(const CustomComparable &other) const + { + return i == other.i; + } + int i; +}; + +struct UnregisteredType {}; + +typedef QHash<int, CustomComparable> IntComparableHash; +Q_DECLARE_METATYPE(IntComparableHash) +typedef QMap<int, CustomComparable> IntComparableMap; +Q_DECLARE_METATYPE(IntComparableMap) +typedef QPair<int, CustomComparable> IntComparablePair; +Q_DECLARE_METATYPE(IntComparablePair) + +typedef QHash<int, int> IntIntHash; +typedef int NaturalNumber; +class AutoMetaTypeObject : public QObject +{ + Q_OBJECT + Q_PROPERTY(IntIntHash someHash READ someHash CONSTANT) + Q_PROPERTY(NaturalNumber someInt READ someInt CONSTANT) +public: + AutoMetaTypeObject(QObject *parent = 0) + : QObject(parent), m_int(42) + { + m_hash.insert(4, 2); + } + + QHash<int,int> someHash() const + { + return m_hash; + } + + int someInt() const + { + return m_int; + } + +private: + QHash<int,int> m_hash; + int m_int; +}; + void tst_QMetaType::automaticTemplateRegistration() { { @@ -1094,6 +1149,84 @@ void tst_QMetaType::automaticTemplateRegistration() vectorList << sharedPointerList; QVERIFY(QVariant::fromValue(vectorList).value<QVector<QList<QSharedPointer<QObject> > > >().first().first() == testObject); } + { + IntIntHash intIntHash; + intIntHash.insert(4, 2); + QCOMPARE(QVariant::fromValue(intIntHash).value<IntIntHash>().value(4), 2); + + AutoMetaTypeObject amto; + + qRegisterMetaType<QHash<int, int> >("IntIntHash"); + QVariant hashVariant = amto.property("someHash"); + QCOMPARE(hashVariant.value<IntIntHash>().value(4), 2); + + qRegisterMetaType<int>("NaturalNumber"); + QVariant intVariant = amto.property("someInt"); + QCOMPARE(intVariant.value<NaturalNumber>(), 42); + } + { + IntUIntHash intUIntHash; + intUIntHash.insert(4, 2); + QCOMPARE(QVariant::fromValue(intUIntHash).value<IntUIntHash>().value(4), (uint)2); + } + { + IntComparableHash intComparableHash; + CustomComparable m; + intComparableHash.insert(4, m); + QCOMPARE(QVariant::fromValue(intComparableHash).value<IntComparableHash>().value(4), m); + } + { + QVariantHash variantHash; + variantHash.insert(QStringLiteral("4"), 2); + QCOMPARE(QVariant::fromValue(variantHash).value<QVariantHash>().value(QStringLiteral("4")), QVariant(2)); + } + { + typedef QMap<int, int> IntIntMap; + IntIntMap intIntMap; + intIntMap.insert(4, 2); + QCOMPARE(QVariant::fromValue(intIntMap).value<IntIntMap>().value(4), 2); + } + { + IntUIntMap intUIntMap; + intUIntMap.insert(4, 2); + QCOMPARE(QVariant::fromValue(intUIntMap).value<IntUIntMap>().value(4), (uint)2); + } + { + IntComparableMap intComparableMap; + CustomComparable m; + intComparableMap.insert(4, m); + QCOMPARE(QVariant::fromValue(intComparableMap).value<IntComparableMap>().value(4), m); + } + { + QVariantMap variantMap; + variantMap.insert(QStringLiteral("4"), 2); + QCOMPARE(QVariant::fromValue(variantMap).value<QVariantMap>().value(QStringLiteral("4")), QVariant(2)); + } + { + typedef QPair<int, int> IntIntPair; + IntIntPair intIntPair = qMakePair(4, 2); + QCOMPARE(QVariant::fromValue(intIntPair).value<IntIntPair>().first, 4); + QCOMPARE(QVariant::fromValue(intIntPair).value<IntIntPair>().second, 2); + } + { + IntUIntPair intUIntPair = qMakePair<int, uint>(4, 2); + QCOMPARE(QVariant::fromValue(intUIntPair).value<IntUIntPair>().first, 4); + QCOMPARE(QVariant::fromValue(intUIntPair).value<IntUIntPair>().second, (uint)2); + } + { + CustomComparable m; + IntComparablePair intComparablePair = qMakePair(4, m); + QCOMPARE(QVariant::fromValue(intComparablePair).value<IntComparablePair>().first, 4); + QCOMPARE(QVariant::fromValue(intComparablePair).value<IntComparablePair>().second, m); + } + { + typedef QHash<int, UnregisteredType> IntUnregisteredTypeHash; + QVERIFY(qRegisterMetaType<IntUnregisteredTypeHash>("IntUnregisteredTypeHash") > 0); + } + { + typedef QList<UnregisteredType> UnregisteredTypeList; + QVERIFY(qRegisterMetaType<UnregisteredTypeList>("UnregisteredTypeList") > 0); + } } // Compile-time test, it should be possible to register function pointer types |