From 2c01d402e19b6137cf24794cbc4533a2b8b2d816 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 18 Nov 2014 14:32:38 -0800 Subject: QDebug: pretty-print QStrings and QStringRefs [ChangeLog][QtCore][QDebug] Printing of QStrings and QStringRefs whenever "noquote" is not active now prints the strings in a format that can be copied back to C++ code. All characters that aren't printable in US-ASCII are escaped (this includes printable Unicode characters outside of US-ASCII). Pretty-printing will not respect QTextFormat padding or field widths. Change-Id: I169a8a0508e24693f5652f0129defe7f709e5d08 Reviewed-by: Oswald Buddenhagen Reviewed-by: Thiago Macieira --- tests/auto/corelib/io/qdebug/tst_qdebug.cpp | 67 +++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'tests/auto/corelib/io') diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 015a13775d..63de953437 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -52,6 +52,7 @@ private slots: void stateSaver() const; void veryLongWarningMessage() const; void qDebugQChar() const; + void qDebugQString() const; void qDebugQStringRef() const; void qDebugQLatin1String() const; void qDebugQByteArray() const; @@ -344,6 +345,54 @@ void tst_QDebug::qDebugQChar() const } +void tst_QDebug::qDebugQString() const +{ + /* Use a basic string. */ + { + QString file, function; + int line = 0; + const QString in(QLatin1String("input")); + const QStringRef inRef(&in); + + MessageHandlerSetter mhs(myMessageHandler); + { qDebug() << inRef; } +#ifndef QT_NO_MESSAGELOGCONTEXT + file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO; +#endif + QCOMPARE(s_msgType, QtDebugMsg); + QCOMPARE(s_msg, QString::fromLatin1("\"input\"")); + QCOMPARE(QString::fromLatin1(s_file), file); + QCOMPARE(s_line, line); + QCOMPARE(QString::fromLatin1(s_function), function); + } + + /* simpler tests from now on */ + MessageHandlerSetter mhs(myMessageHandler); + + QString string = "Hello"; + qDebug() << string; + QCOMPARE(s_msg, QString("\"Hello\"")); + + qDebug().noquote().nospace() << string; + QCOMPARE(s_msg, string); + + qDebug().noquote().nospace() << qSetFieldWidth(8) << string; + QCOMPARE(s_msg, " " + string); + + string = QLatin1String("\nSm\xF8rg\xE5sbord\\"); + qDebug().noquote().nospace() << string; + QCOMPARE(s_msg, string); + + qDebug() << string; + QCOMPARE(s_msg, QString("\"\\nSm\\u00F8rg\\u00E5sbord\\\\\"")); + + // surrogate pairs (including broken pairings) + ushort utf16[] = { 0xDC00, 0xD800, 0xDC00, 'x', 0xD800, 0xDC00, 0xD800, 0 }; + string = QString::fromUtf16(utf16); + qDebug() << string; + QCOMPARE(s_msg, QString("\"\\uDC00\\U00010000x\\U00010000\\uD800\"")); +} + void tst_QDebug::qDebugQStringRef() const { /* Use a basic string. */ @@ -403,6 +452,24 @@ void tst_QDebug::qDebugQLatin1String() const QCOMPARE(QString::fromLatin1(s_file), file); QCOMPARE(s_line, line); QCOMPARE(QString::fromLatin1(s_function), function); + + /* simpler tests from now on */ + QLatin1String string("\"Hello\""); + qDebug() << string; + QCOMPARE(s_msg, QString("\"\\\"Hello\\\"\"")); + + qDebug().noquote().nospace() << string; + QCOMPARE(s_msg, QString(string)); + + qDebug().noquote().nospace() << qSetFieldWidth(8) << string; + QCOMPARE(s_msg, " " + QString(string)); + + string = QLatin1String("\nSm\xF8rg\xE5sbord\\"); + qDebug().noquote().nospace() << string; + QCOMPARE(s_msg, QString(string)); + + qDebug() << string; + QCOMPARE(s_msg, QString("\"\\nSm\\u00F8rg\\u00E5sbord\\\\\"")); } void tst_QDebug::qDebugQByteArray() const -- cgit v1.2.3