From 4c3c63d4cbb81b38e88e06b72749e7e01497b6f1 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Wed, 4 Dec 2019 09:49:34 +0100 Subject: 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() - 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 --- .../corelib/kernel/qmetatype/tst_qmetatype.cpp | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'tests') 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()).id(), ::qMetaTypeId()); + QCOMPARE(QMetaType::fromType().id(), ::qMetaTypeId()); } 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 >(); \ QCOMPARE(type, expectedType); \ + QCOMPARE((QMetaType::fromType>().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(), QMetaType(MetaTypeId)); \ + QVERIFY(QMetaType::fromType() == QMetaType(MetaTypeId)); \ + QVERIFY(!(QMetaType::fromType() != QMetaType(MetaTypeId))); \ + QCOMPARE(QMetaType::fromType().id(), MetaTypeId); + + FOR_EACH_CORE_METATYPE(FROMTYPE_CHECK) + + QVERIFY(QMetaType::fromType() != QMetaType()); + QCOMPARE(QMetaType(), QMetaType()); + QCOMPARE(QMetaType(QMetaType::UnknownType), QMetaType()); + + FROMTYPE_CHECK(_, ::qMetaTypeId>(), Whity) + #undef FROMTYPE_CHECK +} + + // Compile-time test, it should be possible to register function pointer types class Undefined; -- cgit v1.2.3