QtTest: move QTRY_X error message formatting out-of-line

Clazy warns about the fromUtf8() call with a constant argument, for
every use of the macro, so hide the stuff behind a compiler firewall.

Also fix the format injection error by using QLatin1String::arg()
instead of QString::arg() chaining.

Change-Id: I4bb4d4af56443540efc0c38c75819aa152a441fc
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2021-06-14 15:34:40 +02:00
parent fb3f903980
commit 66d080af31
2 changed files with 11 additions and 3 deletions

View File

@ -285,6 +285,14 @@ namespace QTestPrivate
namespace QTest
{
QString Internal::formatTryTimeoutDebugMessage(q_no_char8_t::QUtf8StringView expr, int timeout, int actual)
{
return QLatin1String("QTestLib: This test case check (\"%1\") failed because the requested timeout (%2 ms) was too short, %3 ms would have been sufficient this time.")
// ### Qt 7: remove the toString() (or earlier, when arg() can handle QUtf8StringView), passing the view directly
.arg(expr.toString(), QString::number(timeout), QString::number(actual));
}
extern Q_TESTLIB_EXPORT int lastMouseTimestamp;
class WatchDog;

View File

@ -143,9 +143,7 @@ do {\
if (!(expr)) { \
QTRY_LOOP_IMPL((expr), (2 * timeoutValue), step);\
if (expr) { \
QString msg = QString::fromUtf8("QTestLib: This test case check (\"%1\") failed because the requested timeout (%2 ms) was too short, %3 ms would have been sufficient this time."); \
msg = msg.arg(QString::fromUtf8(#expr)).arg(timeoutValue).arg(timeoutValue + qt_test_i); \
QFAIL(qPrintable(msg)); \
QFAIL(qPrintable(QTest::Internal::formatTryTimeoutDebugMessage(u8"" #expr, timeoutValue, timeoutValue + qt_test_i))); \
} \
}
@ -239,6 +237,8 @@ namespace QTest
{
namespace Internal {
Q_TESTLIB_EXPORT QString formatTryTimeoutDebugMessage(q_no_char8_t::QUtf8StringView expr, int timeout, int actual);
template<typename T> // Output registered enums
inline typename std::enable_if<QtPrivate::IsQEnumHelper<T>::Value, char*>::type toString(T e)
{