diff options
author | Olivier Goffart <ogoffart@woboq.com> | 2019-12-04 09:49:34 +0100 |
---|---|---|
committer | Olivier Goffart <ogoffart@woboq.com> | 2019-12-07 10:32:29 +0100 |
commit | 4c3c63d4cbb81b38e88e06b72749e7e01497b6f1 (patch) | |
tree | c1daef3225c90791dd642abe44ffe6242652cb08 /tests | |
parent | ece0c0a5e7e0b18beb58ccd868bde54c7be64f78 (diff) |
QMetaType: add more static-less API
In prevision to Qt6 which is going to discourrage the use of the integer id,
add some missing API to the staticless QMetaType API:
- Add a way to construct a QMetaType from a type without calling qMetaTypeId:
QMetaType::fromType<T>()
- Add equality operators
- Add a QMetaType::name() function
- Add a default constructor (by adding a default parameter to the existing ctor)
Change-Id: I95487c1c31bdf0d773717daa9d5452cbced30673
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index 12c29a6e13..19b3289390 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -125,6 +125,7 @@ private slots: void compareCustomEqualOnlyType(); void customDebugStream(); void unknownType(); + void fromType(); }; struct BaseGenericType @@ -482,6 +483,7 @@ void tst_QMetaType::id() { QCOMPARE(QMetaType(QMetaType::QString).id(), QMetaType::QString); QCOMPARE(QMetaType(::qMetaTypeId<TestSpace::Foo>()).id(), ::qMetaTypeId<TestSpace::Foo>()); + QCOMPARE(QMetaType::fromType<TestSpace::Foo>().id(), ::qMetaTypeId<TestSpace::Foo>()); } void tst_QMetaType::qMetaTypeId() @@ -600,6 +602,12 @@ void tst_QMetaType::typeName() QCOMPARE(name, aTypeName); QCOMPARE(name.toLatin1(), QMetaObject::normalizedType(name.toLatin1().constData())); QCOMPARE(rawname == nullptr, aTypeName.isNull()); + + QMetaType mt(aType); + if (mt.isValid()) { // Gui type are not valid + QCOMPARE(QString::fromLatin1(QMetaType(aType).name()), aTypeName); + } + } void tst_QMetaType::type_data() @@ -1728,6 +1736,7 @@ void tst_QMetaType::automaticTemplateRegistration() const int type = QMetaType::type(tn); \ const int expectedType = ::qMetaTypeId<CONTAINER< __VA_ARGS__ > >(); \ QCOMPARE(type, expectedType); \ + QCOMPARE((QMetaType::fromType<CONTAINER< __VA_ARGS__ >>().id()), expectedType); \ } #define FOR_EACH_1ARG_TEMPLATE_TYPE(F, TYPE) \ @@ -2572,6 +2581,26 @@ void tst_QMetaType::unknownType() invalid.construct(&buffer); QCOMPARE(buffer, 0xBAD); } + +void tst_QMetaType::fromType() +{ + #define FROMTYPE_CHECK(MetaTypeName, MetaTypeId, RealType) \ + QCOMPARE(QMetaType::fromType<RealType>(), QMetaType(MetaTypeId)); \ + QVERIFY(QMetaType::fromType<RealType>() == QMetaType(MetaTypeId)); \ + QVERIFY(!(QMetaType::fromType<RealType>() != QMetaType(MetaTypeId))); \ + QCOMPARE(QMetaType::fromType<RealType>().id(), MetaTypeId); + + FOR_EACH_CORE_METATYPE(FROMTYPE_CHECK) + + QVERIFY(QMetaType::fromType<QString>() != QMetaType()); + QCOMPARE(QMetaType(), QMetaType()); + QCOMPARE(QMetaType(QMetaType::UnknownType), QMetaType()); + + FROMTYPE_CHECK(_, ::qMetaTypeId<Whity<int>>(), Whity<int>) + #undef FROMTYPE_CHECK +} + + // Compile-time test, it should be possible to register function pointer types class Undefined; |