From 4a9092eaad34ca4788247c1ecb216f7d3b3d1d08 Mon Sep 17 00:00:00 2001 From: Dmitry Ashkadov Date: Fri, 11 Oct 2013 14:05:01 +0400 Subject: 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 Reviewed-by: Thiago Macieira Reviewed-by: Jason McDonald --- src/testlib/qtestcase.cpp | 19 +++ src/testlib/qtestcase.h | 45 ++++++ .../expected_verifyexceptionthrown.lightxml | 49 +++++++ .../selftests/expected_verifyexceptionthrown.txt | 21 +++ .../selftests/expected_verifyexceptionthrown.xml | 52 +++++++ .../expected_verifyexceptionthrown.xunitxml | 31 ++++ tests/auto/testlib/selftests/selftests.pri | 1 + tests/auto/testlib/selftests/selftests.qrc | 4 + tests/auto/testlib/selftests/tst_selftests.cpp | 4 + .../tst_verifyexceptionthrown.cpp | 162 +++++++++++++++++++++ .../verifyexceptionthrown.pro | 8 + 11 files changed, 396 insertions(+) create mode 100644 tests/auto/testlib/selftests/expected_verifyexceptionthrown.lightxml create mode 100644 tests/auto/testlib/selftests/expected_verifyexceptionthrown.txt create mode 100644 tests/auto/testlib/selftests/expected_verifyexceptionthrown.xml create mode 100644 tests/auto/testlib/selftests/expected_verifyexceptionthrown.xunitxml create mode 100644 tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp create mode 100644 tests/auto/testlib/selftests/verifyexceptionthrown/verifyexceptionthrown.pro 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 +#ifndef QT_NO_EXCEPTIONS +# include +#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 { \ diff --git a/tests/auto/testlib/selftests/expected_verifyexceptionthrown.lightxml b/tests/auto/testlib/selftests/expected_verifyexceptionthrown.lightxml new file mode 100644 index 0000000000..6ef857c6d0 --- /dev/null +++ b/tests/auto/testlib/selftests/expected_verifyexceptionthrown.lightxml @@ -0,0 +1,49 @@ + + @INSERT_QT_VERSION_HERE@ + @INSERT_QT_VERSION_HERE@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/auto/testlib/selftests/expected_verifyexceptionthrown.txt b/tests/auto/testlib/selftests/expected_verifyexceptionthrown.txt new file mode 100644 index 0000000000..5c40f62dd2 --- /dev/null +++ b/tests/auto/testlib/selftests/expected_verifyexceptionthrown.txt @@ -0,0 +1,21 @@ +********* Start testing of tst_VerifyExceptionThrown ********* +Config: Using QtTest library @INSERT_QT_VERSION_HERE@, Qt @INSERT_QT_VERSION_HERE@ +PASS : tst_VerifyExceptionThrown::initTestCase() +PASS : tst_VerifyExceptionThrown::testCorrectStdTypes() +PASS : tst_VerifyExceptionThrown::testCorrectStdExceptions() +PASS : tst_VerifyExceptionThrown::testCorrectMyExceptions() +FAIL! : tst_VerifyExceptionThrown::testFailInt() Expected exception of type double to be thrown but unknown exception caught + Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(128)] +FAIL! : tst_VerifyExceptionThrown::testFailStdString() Expected exception of type char* to be thrown but unknown exception caught + Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(133)] +FAIL! : tst_VerifyExceptionThrown::testFailStdRuntimeError() Expected exception of type std::runtime_error to be thrown but std::exception caught with message: logic error + Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(138)] +FAIL! : tst_VerifyExceptionThrown::testFailMyException() Expected exception of type MyBaseException to be thrown but std::exception caught with message: logic error + Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(143)] +FAIL! : tst_VerifyExceptionThrown::testFailMyDerivedException() Expected exception of type std::runtime_error to be thrown but std::exception caught with message: MyDerivedException + Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(148)] +FAIL! : tst_VerifyExceptionThrown::testFailNoException() Expected exception of type std::exception to be thrown but no exception caught + Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(153)] +PASS : tst_VerifyExceptionThrown::cleanupTestCase() +Totals: 5 passed, 6 failed, 0 skipped +********* Finished testing of tst_VerifyExceptionThrown ********* diff --git a/tests/auto/testlib/selftests/expected_verifyexceptionthrown.xml b/tests/auto/testlib/selftests/expected_verifyexceptionthrown.xml new file mode 100644 index 0000000000..df261b72c4 --- /dev/null +++ b/tests/auto/testlib/selftests/expected_verifyexceptionthrown.xml @@ -0,0 +1,52 @@ + + + + @INSERT_QT_VERSION_HERE@ + @INSERT_QT_VERSION_HERE@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/auto/testlib/selftests/expected_verifyexceptionthrown.xunitxml b/tests/auto/testlib/selftests/expected_verifyexceptionthrown.xunitxml new file mode 100644 index 0000000000..f49746a9af --- /dev/null +++ b/tests/auto/testlib/selftests/expected_verifyexceptionthrown.xunitxml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/auto/testlib/selftests/selftests.pri b/tests/auto/testlib/selftests/selftests.pri index c9474419eb..7b706735a9 100644 --- a/tests/auto/testlib/selftests/selftests.pri +++ b/tests/auto/testlib/selftests/selftests.pri @@ -41,5 +41,6 @@ SUBPROGRAMS = \ subtest \ verbose1 \ verbose2 \ + verifyexceptionthrown \ warnings \ xunit diff --git a/tests/auto/testlib/selftests/selftests.qrc b/tests/auto/testlib/selftests/selftests.qrc index 03de05fb89..e7ca7138b2 100644 --- a/tests/auto/testlib/selftests/selftests.qrc +++ b/tests/auto/testlib/selftests/selftests.qrc @@ -134,6 +134,10 @@ expected_verbose2.txt expected_verbose2.xml expected_verbose2.xunitxml + expected_verifyexceptionthrown.lightxml + expected_verifyexceptionthrown.txt + expected_verifyexceptionthrown.xml + expected_verifyexceptionthrown.xunitxml expected_warnings.lightxml expected_warnings.txt expected_warnings.xml diff --git a/tests/auto/testlib/selftests/tst_selftests.cpp b/tests/auto/testlib/selftests/tst_selftests.cpp index b737f823c8..d77e372983 100644 --- a/tests/auto/testlib/selftests/tst_selftests.cpp +++ b/tests/auto/testlib/selftests/tst_selftests.cpp @@ -380,6 +380,10 @@ void tst_Selftests::runSubTest_data() << "subtest" << "verbose1" << "verbose2" +#ifndef QT_NO_EXCEPTIONS + // this test will test nothing if the exceptions are disabled + << "verifyexceptionthrown" +#endif //!QT_NO_EXCEPTIONS << "warnings" << "xunit" ; diff --git a/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp b/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp new file mode 100644 index 0000000000..fc57f19c4f --- /dev/null +++ b/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp @@ -0,0 +1,162 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include + + +#ifndef QT_NO_EXCEPTIONS +# include +#endif + + +#ifndef QT_NO_EXCEPTIONS + +class MyBaseException +{ + +}; + +class MyDerivedException: public MyBaseException, public std::domain_error +{ +public: + MyDerivedException(): std::domain_error("MyDerivedException") {} +}; + + +#endif // !QT_NO_EXCEPTIONS + + +class tst_VerifyExceptionThrown: public QObject +{ + Q_OBJECT +private: + void doSomething() const {} + +private slots: +// Remove all test cases if exceptions are not available +#ifndef QT_NO_EXCEPTIONS + void testCorrectStdTypes() const; + void testCorrectStdExceptions() const; + void testCorrectMyExceptions() const; + + void testFailInt() const; + void testFailStdString() const; + void testFailStdRuntimeError() const; + void testFailMyException() const; + void testFailMyDerivedException() const; + + void testFailNoException() const; +#endif // !QT_NO_EXCEPTIONS +}; + + + +#ifndef QT_NO_EXCEPTIONS + +void tst_VerifyExceptionThrown::testCorrectStdTypes() const +{ + QVERIFY_EXCEPTION_THROWN(throw int(5), int); + QVERIFY_EXCEPTION_THROWN(throw float(9.8), float); + QVERIFY_EXCEPTION_THROWN(throw bool(true), bool); + QVERIFY_EXCEPTION_THROWN(throw std::string("some string"), std::string); +} + +void tst_VerifyExceptionThrown::testCorrectStdExceptions() const +{ + // same type + QVERIFY_EXCEPTION_THROWN(throw std::exception(), std::exception); + QVERIFY_EXCEPTION_THROWN(throw std::runtime_error("runtime error"), std::runtime_error); + QVERIFY_EXCEPTION_THROWN(throw std::overflow_error("overflow error"), std::overflow_error); + + // inheritance + QVERIFY_EXCEPTION_THROWN(throw std::overflow_error("overflow error"), std::runtime_error); + QVERIFY_EXCEPTION_THROWN(throw std::overflow_error("overflow error"), std::exception); +} + +void tst_VerifyExceptionThrown::testCorrectMyExceptions() const +{ + // same type + QVERIFY_EXCEPTION_THROWN(throw MyBaseException(), MyBaseException); + QVERIFY_EXCEPTION_THROWN(throw MyDerivedException(), MyDerivedException); + + // inheritance + QVERIFY_EXCEPTION_THROWN(throw MyDerivedException(), MyBaseException); + QVERIFY_EXCEPTION_THROWN(throw MyDerivedException(), std::domain_error); +} + +void tst_VerifyExceptionThrown::testFailInt() const +{ + QVERIFY_EXCEPTION_THROWN(throw int(5), double); +} + +void tst_VerifyExceptionThrown::testFailStdString() const +{ + QVERIFY_EXCEPTION_THROWN(throw std::string("some string"), char*); +} + +void tst_VerifyExceptionThrown::testFailStdRuntimeError() const +{ + QVERIFY_EXCEPTION_THROWN(throw std::logic_error("logic error"), std::runtime_error); +} + +void tst_VerifyExceptionThrown::testFailMyException() const +{ + QVERIFY_EXCEPTION_THROWN(throw std::logic_error("logic error"), MyBaseException); +} + +void tst_VerifyExceptionThrown::testFailMyDerivedException() const +{ + QVERIFY_EXCEPTION_THROWN(throw MyDerivedException(), std::runtime_error); +} + +void tst_VerifyExceptionThrown::testFailNoException() const +{ + QVERIFY_EXCEPTION_THROWN(doSomething(), std::exception); +} + +#endif // !QT_NO_EXCEPTIONS + + + +QTEST_MAIN(tst_VerifyExceptionThrown) + +#include "tst_verifyexceptionthrown.moc" diff --git a/tests/auto/testlib/selftests/verifyexceptionthrown/verifyexceptionthrown.pro b/tests/auto/testlib/selftests/verifyexceptionthrown/verifyexceptionthrown.pro new file mode 100644 index 0000000000..51f108c9d7 --- /dev/null +++ b/tests/auto/testlib/selftests/verifyexceptionthrown/verifyexceptionthrown.pro @@ -0,0 +1,8 @@ +SOURCES += tst_verifyexceptionthrown.cpp +QT = core testlib + +mac:CONFIG -= app_bundle +CONFIG -= debug_and_release_target +CONFIG += exceptions + +TARGET = verifyexceptionthrown -- cgit v1.2.3