diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2022-07-09 17:01:38 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-07-13 13:14:43 +0000 |
commit | d84ee92ecf18f305e02cf58fcc835d27aa2f6579 (patch) | |
tree | f6396a1871e1892404b30a7ed3932596f9b115c9 /src/testlib | |
parent | e8298aea013064a2db1681bb2c02ae99a7d48e40 (diff) | |
download | qtbase-d84ee92ecf18f305e02cf58fcc835d27aa2f6579.tar.gz |
Use debug stream in QTest::toString's default fallback if possible
For built-in types, this is a compile-time assert - we should not have
any types in Qt for which we have neither debug streaming nor a
QTest::toString specialization implemented. A build of most of Qt
submodules passes with this change, after minor modifications to some
tests. We cannot declare QSizeHint::Policy as a metatype after the
QMetaType has already been instantiated for it, and the QDebug stream
operator for QElaspedTimer needs to be correctly declared within the
namespace.
Add a self-test function for a custom type, and update reference files
of the self-test.
Task-number: QTBUG-104867
Change-Id: I2936db5933f4589fce45f47cf2f3224ed614d8c9
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 92e696b4bad52c40c6cdc1c5bf7c0c0ea8a2b283)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/testlib')
-rw-r--r-- | src/testlib/qtestcase.h | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 0412f872f7..d8ad48d2c0 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -307,9 +307,18 @@ namespace QTest return qstrdup(QByteArray::number(static_cast<std::underlying_type_t<T>>(e)).constData()); } - template <typename T> // Fallback - inline typename std::enable_if<!QtPrivate::IsQEnumHelper<T>::Value && !std::is_enum_v<T>, char*>::type toString(const T &) + template <typename T> // Fallback; for built-in types debug streaming must be possible + inline typename std::enable_if<!QtPrivate::IsQEnumHelper<T>::Value && !std::is_enum_v<T>, char *>::type toString(const T &t) { +#ifndef QT_NO_DEBUG_STREAM + if constexpr (QTypeTraits::has_ostream_operator_v<QDebug, T>) { + return qstrdup(QDebug::toString(t).toUtf8().constData()); + } else { + static_assert(!QMetaTypeId2<T>::IsBuiltIn, + "Built-in type must implement debug streaming operator " + "or provide QTest::toString specialization"); + } +#endif return nullptr; } |