QTest: de-inline QVERIFY_THROWS_EXCEPTION message formatting

Extract Method QTest::qCaught() to take the string handling out of the
header. This should help a bit in speeding up compilation of large
unit test files (provided they use QVERIFY_THROWS_EXCEPTION), although
I have no data to support that.

Since we changed the error message, update the selftest accordingly.

Change-Id: Id4a3c8c34d5df8d0c7a861106d269097f4a6de5c
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Marc Mutz 2021-11-23 08:58:58 +01:00
parent 405adf3348
commit 59600a514b
8 changed files with 68 additions and 35 deletions

View File

@ -2073,6 +2073,40 @@ bool QTest::qExpectFail(const char *dataIndex, const char *comment,
return QTestResult::expectFail(dataIndex, qstrdup(comment), mode, file, line);
}
/*!
\internal
Executes qFail() following a failed QVERIFY_THROWS_EXCEPTION or
QVERIFY_THROWS_NO_EXCEPTION, passing a suitable message created from \a expected,
\a what, along with \a file and \a line.
The \a expected parameter contains the type of the exception that is expected to
be thrown, or \nullptr, if no exception was expected.
The \a what parameter contains the result of \c{std::exception::what()}, or nullptr,
if a non-\c{std::exception}-derived exception was caught.
The \a file and \a line parameters hold expansions of the \c{__FILE__} and \c{__LINE__}
macros, respectively.
*/
void QTest::qCaught(const char *expected, const char *what, const char *file, int line)
{
auto message = [&] {
const auto exType = what ? "std::" : "unknown ";
const auto ofType = expected ? " of type " : "";
const auto no = expected ? "an" : "no";
const auto withMsg = what ? " with message " : "";
const auto protect = [](const char *s) { return s ? s : ""; };
return QString::asprintf("Expected %s exception%s%s to be thrown, "
"but caught %sexception%s%s",
no, ofType, protect(expected),
exType, withMsg, protect(what));
};
qFail(message().toUtf8().constData(), file, line);
}
#if QT_DEPRECATED_SINCE(6, 3)
/*!
\internal

View File

@ -115,13 +115,10 @@ inline void useVerifyThrowsException() {}
/* success */\
}\
} 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__);\
QTest::qCaught(#exceptiontype, e.what(), __FILE__, __LINE__);\
return;\
} QT_CATCH (...) {\
QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
" but unknown exception caught", __FILE__, __LINE__);\
QTest::qCaught(#exceptiontype, nullptr, __FILE__, __LINE__);\
QT_RETHROW;\
}\
} while (false)
@ -323,6 +320,8 @@ namespace QTest
Q_TESTLIB_EXPORT void qSkip(const char *message, const char *file, int line);
Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode,
const char *file, int line);
Q_DECL_COLD_FUNCTION
Q_TESTLIB_EXPORT void qCaught(const char *expected, const char *what, const char *file, int line);
#if QT_DEPRECATED_SINCE(6, 3)
QT_DEPRECATED_VERSION_X_6_3("Use qWarning() instead")
Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = nullptr, int line = 0);

View File

@ -10,19 +10,19 @@
<testcase name="testCorrectStdExceptions" classname="tst_VerifyExceptionThrown" time="@TEST_DURATION@"/>
<testcase name="testCorrectMyExceptions" classname="tst_VerifyExceptionThrown" time="@TEST_DURATION@"/>
<testcase name="testFailInt" classname="tst_VerifyExceptionThrown" time="@TEST_DURATION@">
<failure type="fail" message="Expected exception of type double to be thrown but unknown exception caught"/>
<failure type="fail" message="Expected an exception of type double to be thrown, but caught unknown exception"/>
</testcase>
<testcase name="testFailStdString" classname="tst_VerifyExceptionThrown" time="@TEST_DURATION@">
<failure type="fail" message="Expected exception of type char* to be thrown but unknown exception caught"/>
<failure type="fail" message="Expected an exception of type char* to be thrown, but caught unknown exception"/>
</testcase>
<testcase name="testFailStdRuntimeError" classname="tst_VerifyExceptionThrown" time="@TEST_DURATION@">
<failure type="fail" message="Expected exception of type std::runtime_error to be thrown but std::exception caught with message: logic error"/>
<failure type="fail" message="Expected an exception of type std::runtime_error to be thrown, but caught std::exception with message logic error"/>
</testcase>
<testcase name="testFailMyException" classname="tst_VerifyExceptionThrown" time="@TEST_DURATION@">
<failure type="fail" message="Expected exception of type MyBaseException to be thrown but std::exception caught with message: logic error"/>
<failure type="fail" message="Expected an exception of type MyBaseException to be thrown, but caught std::exception with message logic error"/>
</testcase>
<testcase name="testFailMyDerivedException" classname="tst_VerifyExceptionThrown" time="@TEST_DURATION@">
<failure type="fail" message="Expected exception of type std::runtime_error to be thrown but std::exception caught with message: MyDerivedException"/>
<failure type="fail" message="Expected an exception of type std::runtime_error to be thrown, but caught std::exception with message MyDerivedException"/>
</testcase>
<testcase name="testFailNoException" classname="tst_VerifyExceptionThrown" time="@TEST_DURATION@">
<failure type="fail" message="Expected exception of type std::exception to be thrown but no exception caught"/>

View File

@ -21,31 +21,31 @@
</TestFunction>
<TestFunction name="testFailInt">
<Incident type="fail" file="qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp" line="0">
<Description><![CDATA[Expected exception of type double to be thrown but unknown exception caught]]></Description>
<Description><![CDATA[Expected an exception of type double to be thrown, but caught unknown exception]]></Description>
</Incident>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="testFailStdString">
<Incident type="fail" file="qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp" line="0">
<Description><![CDATA[Expected exception of type char* to be thrown but unknown exception caught]]></Description>
<Description><![CDATA[Expected an exception of type char* to be thrown, but caught unknown exception]]></Description>
</Incident>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="testFailStdRuntimeError">
<Incident type="fail" file="qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp" line="0">
<Description><![CDATA[Expected exception of type std::runtime_error to be thrown but std::exception caught with message: logic error]]></Description>
<Description><![CDATA[Expected an exception of type std::runtime_error to be thrown, but caught std::exception with message logic error]]></Description>
</Incident>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="testFailMyException">
<Incident type="fail" file="qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp" line="0">
<Description><![CDATA[Expected exception of type MyBaseException to be thrown but std::exception caught with message: logic error]]></Description>
<Description><![CDATA[Expected an exception of type MyBaseException to be thrown, but caught std::exception with message logic error]]></Description>
</Incident>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="testFailMyDerivedException">
<Incident type="fail" file="qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp" line="0">
<Description><![CDATA[Expected exception of type std::runtime_error to be thrown but std::exception caught with message: MyDerivedException]]></Description>
<Description><![CDATA[Expected an exception of type std::runtime_error to be thrown, but caught std::exception with message MyDerivedException]]></Description>
</Incident>
<Duration msecs="0"/>
</TestFunction>

View File

@ -6,35 +6,35 @@ ok 3 - testCorrectStdExceptions()
ok 4 - testCorrectMyExceptions()
not ok 5 - testFailInt()
---
# Expected exception of type double to be thrown but unknown exception caught
# Expected an exception of type double to be thrown, but caught unknown exception
at: tst_VerifyExceptionThrown::testFailInt() (qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp:0)
file: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp
line: 0
...
not ok 6 - testFailStdString()
---
# Expected exception of type char* to be thrown but unknown exception caught
# Expected an exception of type char* to be thrown, but caught unknown exception
at: tst_VerifyExceptionThrown::testFailStdString() (qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp:0)
file: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp
line: 0
...
not ok 7 - testFailStdRuntimeError()
---
# Expected exception of type std::runtime_error to be thrown but std::exception caught with message: logic error
# Expected an exception of type std::runtime_error to be thrown, but caught std::exception with message logic error
at: tst_VerifyExceptionThrown::testFailStdRuntimeError() (qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp:0)
file: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp
line: 0
...
not ok 8 - testFailMyException()
---
# Expected exception of type MyBaseException to be thrown but std::exception caught with message: logic error
# Expected an exception of type MyBaseException to be thrown, but caught std::exception with message logic error
at: tst_VerifyExceptionThrown::testFailMyException() (qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp:0)
file: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp
line: 0
...
not ok 9 - testFailMyDerivedException()
---
# Expected exception of type std::runtime_error to be thrown but std::exception caught with message: MyDerivedException
# Expected an exception of type std::runtime_error to be thrown, but caught std::exception with message MyDerivedException
at: tst_VerifyExceptionThrown::testFailMyDerivedException() (qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp:0)
file: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp
line: 0

View File

@ -8,19 +8,19 @@
##teamcity[testStarted name='testCorrectMyExceptions()' flowId='tst_VerifyExceptionThrown']
##teamcity[testFinished name='testCorrectMyExceptions()' flowId='tst_VerifyExceptionThrown']
##teamcity[testStarted name='testFailInt()' flowId='tst_VerifyExceptionThrown']
##teamcity[testFailed name='testFailInt()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)|]' details='Expected exception of type double to be thrown but unknown exception caught' flowId='tst_VerifyExceptionThrown']
##teamcity[testFailed name='testFailInt()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)|]' details='Expected an exception of type double to be thrown, but caught unknown exception' flowId='tst_VerifyExceptionThrown']
##teamcity[testFinished name='testFailInt()' flowId='tst_VerifyExceptionThrown']
##teamcity[testStarted name='testFailStdString()' flowId='tst_VerifyExceptionThrown']
##teamcity[testFailed name='testFailStdString()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)|]' details='Expected exception of type char* to be thrown but unknown exception caught' flowId='tst_VerifyExceptionThrown']
##teamcity[testFailed name='testFailStdString()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)|]' details='Expected an exception of type char* to be thrown, but caught unknown exception' flowId='tst_VerifyExceptionThrown']
##teamcity[testFinished name='testFailStdString()' flowId='tst_VerifyExceptionThrown']
##teamcity[testStarted name='testFailStdRuntimeError()' flowId='tst_VerifyExceptionThrown']
##teamcity[testFailed name='testFailStdRuntimeError()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)|]' details='Expected exception of type std::runtime_error to be thrown but std::exception caught with message: logic error' flowId='tst_VerifyExceptionThrown']
##teamcity[testFailed name='testFailStdRuntimeError()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)|]' details='Expected an exception of type std::runtime_error to be thrown, but caught std::exception with message logic error' flowId='tst_VerifyExceptionThrown']
##teamcity[testFinished name='testFailStdRuntimeError()' flowId='tst_VerifyExceptionThrown']
##teamcity[testStarted name='testFailMyException()' flowId='tst_VerifyExceptionThrown']
##teamcity[testFailed name='testFailMyException()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)|]' details='Expected exception of type MyBaseException to be thrown but std::exception caught with message: logic error' flowId='tst_VerifyExceptionThrown']
##teamcity[testFailed name='testFailMyException()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)|]' details='Expected an exception of type MyBaseException to be thrown, but caught std::exception with message logic error' flowId='tst_VerifyExceptionThrown']
##teamcity[testFinished name='testFailMyException()' flowId='tst_VerifyExceptionThrown']
##teamcity[testStarted name='testFailMyDerivedException()' flowId='tst_VerifyExceptionThrown']
##teamcity[testFailed name='testFailMyDerivedException()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)|]' details='Expected exception of type std::runtime_error to be thrown but std::exception caught with message: MyDerivedException' flowId='tst_VerifyExceptionThrown']
##teamcity[testFailed name='testFailMyDerivedException()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)|]' details='Expected an exception of type std::runtime_error to be thrown, but caught std::exception with message MyDerivedException' flowId='tst_VerifyExceptionThrown']
##teamcity[testFinished name='testFailMyDerivedException()' flowId='tst_VerifyExceptionThrown']
##teamcity[testStarted name='testFailNoException()' flowId='tst_VerifyExceptionThrown']
##teamcity[testFailed name='testFailNoException()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)|]' details='Expected exception of type std::exception to be thrown but no exception caught' flowId='tst_VerifyExceptionThrown']

View File

@ -4,15 +4,15 @@ 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
FAIL! : tst_VerifyExceptionThrown::testFailInt() Expected an exception of type double to be thrown, but caught unknown exception
Loc: [qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)]
FAIL! : tst_VerifyExceptionThrown::testFailStdString() Expected exception of type char* to be thrown but unknown exception caught
FAIL! : tst_VerifyExceptionThrown::testFailStdString() Expected an exception of type char* to be thrown, but caught unknown exception
Loc: [qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)]
FAIL! : tst_VerifyExceptionThrown::testFailStdRuntimeError() Expected exception of type std::runtime_error to be thrown but std::exception caught with message: logic error
FAIL! : tst_VerifyExceptionThrown::testFailStdRuntimeError() Expected an exception of type std::runtime_error to be thrown, but caught std::exception with message logic error
Loc: [qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)]
FAIL! : tst_VerifyExceptionThrown::testFailMyException() Expected exception of type MyBaseException to be thrown but std::exception caught with message: logic error
FAIL! : tst_VerifyExceptionThrown::testFailMyException() Expected an exception of type MyBaseException to be thrown, but caught std::exception with message logic error
Loc: [qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)]
FAIL! : tst_VerifyExceptionThrown::testFailMyDerivedException() Expected exception of type std::runtime_error to be thrown but std::exception caught with message: MyDerivedException
FAIL! : tst_VerifyExceptionThrown::testFailMyDerivedException() Expected an exception of type std::runtime_error to be thrown, but caught std::exception with message MyDerivedException
Loc: [qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)]
FAIL! : tst_VerifyExceptionThrown::testFailNoException() Expected exception of type std::exception to be thrown but no exception caught
Loc: [qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp(0)]

View File

@ -23,31 +23,31 @@
</TestFunction>
<TestFunction name="testFailInt">
<Incident type="fail" file="qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp" line="0">
<Description><![CDATA[Expected exception of type double to be thrown but unknown exception caught]]></Description>
<Description><![CDATA[Expected an exception of type double to be thrown, but caught unknown exception]]></Description>
</Incident>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="testFailStdString">
<Incident type="fail" file="qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp" line="0">
<Description><![CDATA[Expected exception of type char* to be thrown but unknown exception caught]]></Description>
<Description><![CDATA[Expected an exception of type char* to be thrown, but caught unknown exception]]></Description>
</Incident>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="testFailStdRuntimeError">
<Incident type="fail" file="qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp" line="0">
<Description><![CDATA[Expected exception of type std::runtime_error to be thrown but std::exception caught with message: logic error]]></Description>
<Description><![CDATA[Expected an exception of type std::runtime_error to be thrown, but caught std::exception with message logic error]]></Description>
</Incident>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="testFailMyException">
<Incident type="fail" file="qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp" line="0">
<Description><![CDATA[Expected exception of type MyBaseException to be thrown but std::exception caught with message: logic error]]></Description>
<Description><![CDATA[Expected an exception of type MyBaseException to be thrown, but caught std::exception with message logic error]]></Description>
</Incident>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="testFailMyDerivedException">
<Incident type="fail" file="qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp" line="0">
<Description><![CDATA[Expected exception of type std::runtime_error to be thrown but std::exception caught with message: MyDerivedException]]></Description>
<Description><![CDATA[Expected an exception of type std::runtime_error to be thrown, but caught std::exception with message MyDerivedException]]></Description>
</Incident>
<Duration msecs="0"/>
</TestFunction>