diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2020-02-19 11:29:37 +0100 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2020-02-22 14:16:59 +0100 |
commit | 658b9697f9d85d4ed294810b4f60bafdbdd8e247 (patch) | |
tree | d836620dc3d444b0ae6877a278bb78af814d64c6 /tests/auto/corelib/io | |
parent | 8471a422e3b4514904ce904aa5aadfab679b4190 (diff) |
Add QDebug::toString()
This template function streams the given object into a QDebug instance
that operates on a string, and then returns that string.
This function is useful for cases where you need the textual
representation of an object for debugging, but cannot use operator<<.
A good example of this is when writing tests where you want to provide
a useful failure message involving details about an object, but must
provide it in a string to e.g. QVERIFY2.
[ChangeLog][QtCore][QDebug] Added static template toString() function,
which streams the given object into a QDebug instance that operates on
a string, and then returns that string.
Fixes: QTBUG-82309
Change-Id: I8411394e899dedad19cec788d779a4515d52ba11
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'tests/auto/corelib/io')
-rw-r--r-- | tests/auto/corelib/io/qdebug/tst_qdebug.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 584e66a7db..7b9e614cf7 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -66,6 +66,7 @@ private slots: void resetFormat() const; void defaultMessagehandler() const; void threadSafety() const; + void toString() const; }; void tst_QDebug::assignment() const @@ -740,6 +741,28 @@ void tst_QDebug::threadSafety() const } } +void tst_QDebug::toString() const +{ + // By reference. + { + MyPoint point(3, 4); + QString expectedString; + QDebug stream(&expectedString); + stream << point; + QCOMPARE(QDebug::toString(point), expectedString); + } + + // By pointer. + { + QObject qobject; + qobject.setObjectName("test"); + QString expectedString; + QDebug stream(&expectedString); + stream << &qobject; + QCOMPARE(QDebug::toString(&qobject), expectedString); + } +} + // Should compile: instentiation of unrelated operator<< should not cause cause compilation // error in QDebug operators (QTBUG-47375) class TestClassA {}; |