summaryrefslogtreecommitdiffstats
path: root/src/testlib
diff options
context:
space:
mode:
authorDmitry Ashkadov <dmitry.ashkadov@gmail.com>2013-10-11 14:05:01 +0400
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-11-26 07:37:06 +0100
commit4a9092eaad34ca4788247c1ecb216f7d3b3d1d08 (patch)
treec77bead318449a4fa257ff0cd860555df7550db3 /src/testlib
parent5d8c05baf478d8eb8cb7ce827caa2c1103f5fa3f (diff)
Add QVERIFY_EXCEPTION_THROWN macro for testing exceptions using QtTest
New macro QVERIFY_EXCEPTION_THROWN may be used to check that some code really throws an exception of specified type. Change-Id: I7cf499c7c37a35407862bc604c6eb862c5f329d3 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Jason McDonald <macadder1@gmail.com>
Diffstat (limited to 'src/testlib')
-rw-r--r--src/testlib/qtestcase.cpp19
-rw-r--r--src/testlib/qtestcase.h45
2 files changed, 64 insertions, 0 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index b09bd6701c..4abbb34986 100644
--- a/src/testlib/qtestcase.cpp
+++ b/src/testlib/qtestcase.cpp
@@ -165,6 +165,25 @@ QT_BEGIN_NAMESPACE
\sa QVERIFY(), QTRY_COMPARE(), QTest::toString()
*/
+/*! \macro QVERIFY_EXCEPTION_THROWN(expression, exceptiontype)
+ \since 5.3
+
+ \relates QTest
+
+ The QVERIFY_EXCEPTION_THROWN macro executes an \a expression and tries
+ to catch an exception thrown from the \a expression. If the \a expression
+ throws an exception and its type is the same as \a exceptiontype
+ or \a exceptiontype is substitutable with the type of thrown exception
+ (i.e. usually the type of thrown exception is publically derived
+ from \a exceptiontype) then execution will be continued. If not-substitutable
+ type of exception is thrown or the \a expression doesn't throw an exception
+ at all, then a failure will be recorded in the test log and
+ the test won't be executed further.
+
+ \note This macro can only be used in a test function that is invoked
+ by the test framework.
+*/
+
/*! \macro QTRY_VERIFY_WITH_TIMEOUT(condition, timeout)
\since 5.0
diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h
index 2a5d7d353b..f95a155ed9 100644
--- a/src/testlib/qtestcase.h
+++ b/src/testlib/qtestcase.h
@@ -51,6 +51,11 @@
#include <string.h>
+#ifndef QT_NO_EXCEPTIONS
+# include <exception>
+#endif // QT_NO_EXCEPTIONS
+
+
QT_BEGIN_NAMESPACE
class QRegularExpression;
@@ -84,6 +89,46 @@ do {\
return;\
} while (0)
+
+#ifndef QT_NO_EXCEPTIONS
+
+# define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \
+ do {\
+ QT_TRY {\
+ QT_TRY {\
+ expression;\
+ QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
+ " but no exception caught", __FILE__, __LINE__);\
+ return;\
+ } QT_CATCH (const exceptiontype &) {\
+ }\
+ } QT_CATCH (const std::exception &e) {\
+ QByteArray msg = QByteArray() + "Expected exception of type " #exceptiontype \
+ " to be thrown but std::exception caught with message: " + e.what(); \
+ QTest::qFail(msg.constData(), __FILE__, __LINE__);\
+ return;\
+ } QT_CATCH (...) {\
+ QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
+ " but unknown exception caught", __FILE__, __LINE__);\
+ return;\
+ }\
+ } while (0)
+
+#else // QT_NO_EXCEPTIONS
+
+/*
+ * The expression passed to the macro should throw an exception and we can't
+ * catch it because Qt has been compiled without exception support. We can't
+ * skip the expression because it may have side effects and must be executed.
+ * So, users must use Qt with exception support enabled if they use exceptions
+ * in their code.
+ */
+# define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \
+ Q_STATIC_ASSERT_X(false, "Support of exceptions is disabled")
+
+#endif // !QT_NO_EXCEPTIONS
+
+
// Will try to wait for the expression to become true while allowing event processing
#define QTRY_VERIFY_WITH_TIMEOUT(__expr, __timeout) \
do { \