diff options
author | David Faure <faure@kde.org> | 2012-07-27 14:17:38 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-01-14 23:44:15 +0100 |
commit | 327b2ba3b77e7a738ccfbe84c6ca5e9525010630 (patch) | |
tree | f89c8299d94d4f407ddb91bc4dfff3464680a85e /tests/auto/corelib/io/qdebug/tst_qdebug.cpp | |
parent | ad265c55beb596e28d6b28c16700795fa8555ee6 (diff) |
Add class QDebugStateSaver for writing QDebug operators correctly
Had to move QTextStreamPrivate to a private header, to be able to use
its new internal Params struct from qdebug.cpp
Change-Id: If28e25f27bbd04b1825a5eb3e2ef83ecad72e7b2
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/io/qdebug/tst_qdebug.cpp')
-rw-r--r-- | tests/auto/corelib/io/qdebug/tst_qdebug.cpp | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 407b5d00f3..c7a41657ac 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -52,7 +52,8 @@ private slots: void warningWithoutDebug() const; void criticalWithoutDebug() const; void debugWithBool() const; - void debugNoSpaces() const; + void debugSpaceHandling() const; + void stateSaver() const; void veryLongWarningMessage() const; void qDebugQStringRef() const; void qDebugQLatin1String() const; @@ -150,7 +151,36 @@ void tst_QDebug::debugWithBool() const QCOMPARE(QString::fromLatin1(s_function), function); } -void tst_QDebug::debugNoSpaces() const +class MyPoint +{ +public: + MyPoint(int val1, int val2) + : v1(val1), v2(val2) {} + int v1; + int v2; +}; +QDebug operator<< (QDebug s, const MyPoint& point) +{ + const QDebugStateSaver saver(s); + return s.nospace() << "MyPoint(" << point.v1 << ", " << point.v2 << ")"; +} + +class MyLine +{ +public: + MyLine(const MyPoint& point1, const MyPoint& point2) + : p1(point1), p2(point2) {} + MyPoint p1; + MyPoint p2; +}; +QDebug operator<< (QDebug s, const MyLine& line) +{ + const QDebugStateSaver saver(s); + s.nospace() << "MyLine(" << line.p1 << ", " << line.p2 << ")"; + return s; +} + +void tst_QDebug::debugSpaceHandling() const { MessageHandlerSetter mhs(myMessageHandler); { @@ -166,8 +196,26 @@ void tst_QDebug::debugNoSpaces() const d << "key=" << "value"; d.space(); d << 1 << 2; + MyLine line(MyPoint(10, 11), MyPoint (12, 13)); + d << line; + // With the old implementation of MyPoint doing dbg.nospace() << ...; dbg.space() we ended up with + // MyLine(MyPoint(10, 11) , MyPoint(12, 13) ) + } + QCOMPARE(s_msg, QString::fromLatin1(" foo key=value 1 2 MyLine(MyPoint(10, 11), MyPoint(12, 13))")); +} + +void tst_QDebug::stateSaver() const +{ + MessageHandlerSetter mhs(myMessageHandler); + { + QDebug d = qDebug(); + { + QDebugStateSaver saver(d); + d.nospace() << hex << right << qSetFieldWidth(3) << qSetPadChar('0') << 42; + } + d.space() << 42; } - QCOMPARE(s_msg, QString::fromLatin1(" foo key=value 1 2 ")); + QCOMPARE(s_msg, QString::fromLatin1("02a 42 ")); } void tst_QDebug::veryLongWarningMessage() const |