From 94ad841ab547e98da111ac41f235cb94cf2d0a45 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Sat, 5 Oct 2013 20:24:14 +0200 Subject: Test that QMetaObject::invokeMethod is exception safe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ie4662b7e475dc3d1ce9f36e8219361d9507622b4 Reviewed-by: Thiago Macieira Reviewed-by: Marc Mutz Reviewed-by: Jędrzej Nowacki --- .../corelib/kernel/qmetaobject/tst_qmetaobject.cpp | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp b/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp index 870e65f0cc..3afc2bc574 100644 --- a/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp +++ b/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp @@ -155,6 +155,7 @@ private slots: void invokeCustomTypes(); void invokeMetaConstructor(); void invokeTypedefTypes(); + void invokeException(); void qtMetaObjectInheritance(); void normalizedSignature_data(); void normalizedSignature(); @@ -301,6 +302,19 @@ void tst_QMetaObject::connectSlotsByName() struct MyUnregisteredType { }; +static int countedStructObjectsCount = 0; +struct CountedStruct +{ + CountedStruct() { ++countedStructObjectsCount; } + CountedStruct(const CountedStruct &) { ++countedStructObjectsCount; } + CountedStruct &operator=(const CountedStruct &) { return *this; } + ~CountedStruct() { --countedStructObjectsCount; } +}; + +#ifndef QT_NO_EXCEPTIONS +class ObjectException : public std::exception { }; +#endif + class QtTestObject: public QObject { friend class tst_QMetaObject; @@ -340,6 +354,13 @@ public slots: void slotWithUnregisteredParameterType(MyUnregisteredType); + CountedStruct throwingSlot(const CountedStruct &, CountedStruct s2) { +#ifndef QT_NO_EXCEPTIONS + throw ObjectException(); +#endif + return s2; + } + signals: void sig0(); QString sig1(QString s1); @@ -847,6 +868,23 @@ void tst_QMetaObject::invokeTypedefTypes() QCOMPARE(spy.at(0).at(0), QVariant(arg)); } +void tst_QMetaObject::invokeException() +{ +#ifndef QT_NO_EXCEPTIONS + QtTestObject obj; + QCOMPARE(countedStructObjectsCount, 0); + try { + CountedStruct s; + QVERIFY(QMetaObject::invokeMethod(&obj, "throwingSlot", Q_RETURN_ARG(CountedStruct, s), + Q_ARG(CountedStruct, s), Q_ARG(CountedStruct, s))); + QFAIL("Did not throw"); + } catch(ObjectException &) {} + QCOMPARE(countedStructObjectsCount, 0); +#else + QSKIP("Needs exceptions"); +#endif +} + void tst_QMetaObject::normalizedSignature_data() { QTest::addColumn("signature"); -- cgit v1.2.3