tst_qmetaobjectbuilder: more descriptive error messages
Change-Id: I7e430f5033d18cf3dc804294bbaac16b0d3f348a Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
parent
465701bb98
commit
71200ae11d
@ -70,10 +70,18 @@ private:
|
||||
static bool checkForSideEffects
|
||||
(const QMetaObjectBuilder& builder,
|
||||
QMetaObjectBuilder::AddMembers members);
|
||||
static bool sameMetaObject
|
||||
(const QMetaObject *meta1, const QMetaObject *meta2);
|
||||
};
|
||||
|
||||
struct MetaObjectComparison {
|
||||
bool isSame = false;
|
||||
QString details;
|
||||
operator bool() {return isSame;}
|
||||
|
||||
static inline auto Ok() {return MetaObjectComparison{true, QString()}; }
|
||||
static inline auto Failed(QStringView message) {return MetaObjectComparison{false, message.toString()}; }
|
||||
};
|
||||
MetaObjectComparison sameMetaObject(const QMetaObject *meta1, const QMetaObject *meta2);
|
||||
|
||||
// Dummy class that has something of every type of thing moc can generate.
|
||||
class SomethingOfEverything : public QObject
|
||||
{
|
||||
@ -999,17 +1007,20 @@ void tst_QMetaObjectBuilder::copyMetaObject()
|
||||
{
|
||||
QMetaObjectBuilder builder(&QObject::staticMetaObject);
|
||||
QMetaObject *meta = builder.toMetaObject();
|
||||
QVERIFY(sameMetaObject(meta, &QObject::staticMetaObject));
|
||||
auto compared = sameMetaObject(meta, &QObject::staticMetaObject);
|
||||
QVERIFY2(compared, qPrintable(compared.details));
|
||||
free(meta);
|
||||
|
||||
QMetaObjectBuilder builder2(&staticMetaObject);
|
||||
meta = builder2.toMetaObject();
|
||||
QVERIFY(sameMetaObject(meta, &staticMetaObject));
|
||||
compared = sameMetaObject(meta, &staticMetaObject);
|
||||
QVERIFY2(compared, qPrintable(compared.details));
|
||||
free(meta);
|
||||
|
||||
QMetaObjectBuilder builder3(&SomethingOfEverything::staticMetaObject);
|
||||
meta = builder3.toMetaObject();
|
||||
QVERIFY(sameMetaObject(meta, &SomethingOfEverything::staticMetaObject));
|
||||
compared = sameMetaObject(meta, &SomethingOfEverything::staticMetaObject);
|
||||
QVERIFY2(compared, qPrintable(compared.details));
|
||||
free(meta);
|
||||
}
|
||||
|
||||
@ -1034,7 +1045,8 @@ void tst_QMetaObjectBuilder::serialize()
|
||||
builder2.setStaticMetacallFunction(builder.staticMetacallFunction());
|
||||
QMetaObject *meta2 = builder2.toMetaObject();
|
||||
|
||||
QVERIFY(sameMetaObject(meta, meta2));
|
||||
auto compared = sameMetaObject(meta, meta2);
|
||||
QVERIFY2(compared, qPrintable(compared.details));
|
||||
free(meta);
|
||||
free(meta2);
|
||||
}
|
||||
@ -1089,7 +1101,8 @@ void tst_QMetaObjectBuilder::relocatableData()
|
||||
|
||||
QMetaObject *meta = builder.toMetaObject();
|
||||
|
||||
QVERIFY(sameMetaObject(meta, &meta2));
|
||||
auto compared = sameMetaObject(meta, &meta2);
|
||||
QVERIFY2(compared, qPrintable(compared.details));
|
||||
|
||||
QVERIFY(!meta2.d.extradata);
|
||||
QVERIFY(!meta2.d.relatedMetaObjects);
|
||||
@ -1209,13 +1222,13 @@ static bool sameMethod(const QMetaMethod& method1, const QMetaMethod& method2)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool sameProperty(const QMetaProperty& prop1, const QMetaProperty& prop2)
|
||||
static MetaObjectComparison sameProperty(const QMetaProperty& prop1, const QMetaProperty& prop2)
|
||||
{
|
||||
if (QByteArray(prop1.name()) != QByteArray(prop2.name()))
|
||||
return false;
|
||||
return MetaObjectComparison::Failed(QStringLiteral("Property names differ: %1 vs %2").arg(prop1.name(), prop2.name()));
|
||||
|
||||
if (QByteArray(prop1.typeName()) != QByteArray(prop2.typeName()))
|
||||
return false;
|
||||
return MetaObjectComparison::Failed(QStringLiteral("Property type names differ: %1 vs %2").arg(prop1.typeName(), prop2.typeName()));
|
||||
|
||||
if (prop1.isReadable() != prop2.isReadable() ||
|
||||
prop1.isWritable() != prop2.isWritable() ||
|
||||
@ -1228,17 +1241,21 @@ static bool sameProperty(const QMetaProperty& prop1, const QMetaProperty& prop2)
|
||||
prop1.isEnumType() != prop2.isEnumType() ||
|
||||
prop1.hasNotifySignal() != prop2.hasNotifySignal() ||
|
||||
prop1.hasStdCppSet() != prop2.hasStdCppSet())
|
||||
return false;
|
||||
return MetaObjectComparison::Failed(u"Flags differ");
|
||||
|
||||
if (prop1.hasNotifySignal()) {
|
||||
if (prop1.notifySignalIndex() != prop2.notifySignalIndex())
|
||||
return false;
|
||||
return MetaObjectComparison::Failed(QStringLiteral("Notify signal index differ: %1 vs %2").arg(
|
||||
QString::number(prop1.notifySignalIndex()),
|
||||
QString::number(prop2.notifySignalIndex())));
|
||||
}
|
||||
|
||||
if (prop1.revision() != prop2.revision())
|
||||
return false;
|
||||
const int revision1 = prop1.revision();
|
||||
const int revision2 = prop2.revision();
|
||||
if (revision1 != revision2)
|
||||
return MetaObjectComparison::Failed(QStringLiteral("Revisions differ: %1 vs %2").arg(QString::number(revision1), QString::number(revision2)));
|
||||
|
||||
return true;
|
||||
return MetaObjectComparison::Ok();
|
||||
}
|
||||
|
||||
static bool sameEnumerator(const QMetaEnum& enum1, const QMetaEnum& enum2)
|
||||
@ -1266,69 +1283,89 @@ static bool sameEnumerator(const QMetaEnum& enum1, const QMetaEnum& enum2)
|
||||
}
|
||||
|
||||
// Determine if two meta objects are identical.
|
||||
bool tst_QMetaObjectBuilder::sameMetaObject
|
||||
(const QMetaObject *meta1, const QMetaObject *meta2)
|
||||
MetaObjectComparison sameMetaObject(const QMetaObject *meta1, const QMetaObject *meta2)
|
||||
{
|
||||
int index;
|
||||
|
||||
if (strcmp(meta1->className(), meta2->className()) != 0)
|
||||
return false;
|
||||
if (strcmp(meta1->className(), meta2->className()) != 0) {
|
||||
QString message = QLatin1String("Class names differ: %1 %2").arg(meta1->className(), meta2->className());
|
||||
return MetaObjectComparison::Failed(message);
|
||||
}
|
||||
|
||||
if (meta1->superClass() != meta2->superClass())
|
||||
return false;
|
||||
return MetaObjectComparison::Failed(QStringLiteral("Super classes differ"));
|
||||
|
||||
if (meta1->constructorCount() != meta2->constructorCount() ||
|
||||
meta1->methodCount() != meta2->methodCount() ||
|
||||
meta1->enumeratorCount() != meta2->enumeratorCount() ||
|
||||
meta1->propertyCount() != meta2->propertyCount() ||
|
||||
meta1->classInfoCount() != meta2->classInfoCount())
|
||||
return false;
|
||||
auto numCompare = [](int num1, int num2, const QString &message) {
|
||||
if (num1 != num2)
|
||||
return MetaObjectComparison::Failed(message.arg(QString::number(num1), QString::number(num2)));
|
||||
return MetaObjectComparison::Ok();
|
||||
};
|
||||
|
||||
auto compared = numCompare(meta1->constructorCount(), meta2->constructorCount(), QStringLiteral("Construct counts differ: %1 %2"));
|
||||
if (!compared.isSame)
|
||||
return compared;
|
||||
compared = numCompare(meta1->methodCount(), meta2->methodCount(), QStringLiteral("Method counts differ: %1 %2"));
|
||||
if (!compared.isSame)
|
||||
return compared;
|
||||
compared = numCompare(meta1->enumeratorCount(), meta2->enumeratorCount(), QStringLiteral("Enumerator counts differ: %1 %2"));
|
||||
if (!compared.isSame)
|
||||
return compared;
|
||||
compared = numCompare(meta1->propertyCount(), meta2->propertyCount(), QStringLiteral("Property counts differ: %1 %2"));
|
||||
if (!compared.isSame)
|
||||
return compared;
|
||||
compared = numCompare(meta1->classInfoCount(), meta2->classInfoCount(), QStringLiteral("ClassInfo counts differ: %1 %2"));
|
||||
if (!compared.isSame)
|
||||
return compared;
|
||||
|
||||
for (index = 0; index < meta1->constructorCount(); ++index) {
|
||||
if (!sameMethod(meta1->constructor(index), meta2->constructor(index)))
|
||||
return false;
|
||||
return MetaObjectComparison::Failed(QStringLiteral("Constructors difer at index %1").arg(index));
|
||||
}
|
||||
|
||||
for (index = 0; index < meta1->methodCount(); ++index) {
|
||||
if (!sameMethod(meta1->method(index), meta2->method(index)))
|
||||
return false;
|
||||
return MetaObjectComparison::Failed(QStringLiteral("Methods difer at index %1").arg(index));
|
||||
}
|
||||
|
||||
for (index = 0; index < meta1->propertyCount(); ++index) {
|
||||
if (!sameProperty(meta1->property(index), meta2->property(index)))
|
||||
return false;
|
||||
if (auto compared = sameProperty(meta1->property(index), meta2->property(index)); !compared) {
|
||||
compared.details += QStringLiteral(" at index %1").arg(index);
|
||||
return compared;
|
||||
}
|
||||
}
|
||||
|
||||
for (index = 0; index < meta1->enumeratorCount(); ++index) {
|
||||
if (!sameEnumerator(meta1->enumerator(index), meta2->enumerator(index)))
|
||||
return false;
|
||||
return MetaObjectComparison::Failed(QStringLiteral("Enumerators difer at index %1").arg(index));
|
||||
}
|
||||
|
||||
for (index = 0; index < meta1->classInfoCount(); ++index) {
|
||||
if (QByteArray(meta1->classInfo(index).name()) !=
|
||||
QByteArray(meta2->classInfo(index).name()))
|
||||
return false;
|
||||
if (QByteArray(meta1->classInfo(index).value()) !=
|
||||
QByteArray(meta2->classInfo(index).value()))
|
||||
return false;
|
||||
const auto name1= QByteArray(meta1->classInfo(index).name());
|
||||
const auto name2= QByteArray(meta1->classInfo(index).name());
|
||||
if (name1 != name2)
|
||||
return MetaObjectComparison::Failed(QStringLiteral("Class infos difer at index %1: %2 vs %3").arg(QString::number(index), name1, name2));
|
||||
const auto value1 = QByteArray(meta1->classInfo(index).value());
|
||||
const auto value2= QByteArray(meta2->classInfo(index).value());
|
||||
if ( value1 != value2)
|
||||
return MetaObjectComparison::Failed(QStringLiteral("Class infos difer at index %1: %2 vs %3").arg(QString::number(index), value1, value2));
|
||||
}
|
||||
|
||||
const auto *objects1 = meta1->d.relatedMetaObjects;
|
||||
const auto *objects2 = meta2->d.relatedMetaObjects;
|
||||
if (objects1 && !objects2)
|
||||
return false;
|
||||
return MetaObjectComparison::Failed(u"Metaobject 1 had related metaobjects, but Metaoject 2 did not");
|
||||
if (objects2 && !objects1)
|
||||
return false;
|
||||
return MetaObjectComparison::Failed(u"Metaobject 2 had related metaobjects, but Metaoject 1 did not");
|
||||
if (objects1 && objects2) {
|
||||
while (*objects1 != 0 && *objects2 != 0) {
|
||||
if (*objects1 != *objects2)
|
||||
return false;
|
||||
return MetaObjectComparison::Failed(QStringLiteral("Related metaobjects differ at index %1").arg(index));
|
||||
++objects1;
|
||||
++objects2;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return MetaObjectComparison::Ok();
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user