From afe3902a30030280b48bfeed403db5edf56336a1 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Thu, 16 Jan 2014 13:45:37 +0100 Subject: [PATCH] Testlib: Use QString for messages in QAbstractTestLogger::addMessage() Task-number: QTBUG-35743 [ChangeLog][QtTest][Windows] Use correct UTF-8 encoding for XML test results on platforms with different console encoding. Change-Id: Ice9d03192098f931e5dac358928e0c4421ab715e Reviewed-by: Frederik Gladhorn --- src/testlib/qabstracttestlogger_p.h | 2 +- src/testlib/qplaintestlogger.cpp | 4 +-- src/testlib/qplaintestlogger_p.h | 2 +- src/testlib/qtestlog.cpp | 26 +++++++++---------- src/testlib/qxmltestlogger.cpp | 6 ++--- src/testlib/qxmltestlogger_p.h | 2 +- src/testlib/qxunittestlogger.cpp | 8 +++--- src/testlib/qxunittestlogger_p.h | 2 +- .../testlib/selftests/badxml/tst_badxml.cpp | 21 +++++++++++++++ .../selftests/expected_badxml.lightxml | 25 +++++++++++------- .../testlib/selftests/expected_badxml.txt | 12 +++++---- .../testlib/selftests/expected_badxml.xml | 25 +++++++++++------- .../selftests/expected_badxml.xunitxml | 6 ++++- .../auto/testlib/selftests/tst_selftests.cpp | 2 ++ 14 files changed, 92 insertions(+), 51 deletions(-) diff --git a/src/testlib/qabstracttestlogger_p.h b/src/testlib/qabstracttestlogger_p.h index c471717655..c549233ddb 100644 --- a/src/testlib/qabstracttestlogger_p.h +++ b/src/testlib/qabstracttestlogger_p.h @@ -94,7 +94,7 @@ public: const char *file = 0, int line = 0) = 0; virtual void addBenchmarkResult(const QBenchmarkResult &result) = 0; - virtual void addMessage(MessageTypes type, const char *message, + virtual void addMessage(MessageTypes type, const QString &message, const char *file = 0, int line = 0) = 0; void outputString(const char *msg); diff --git a/src/testlib/qplaintestlogger.cpp b/src/testlib/qplaintestlogger.cpp index 77be0e423e..480956d34a 100644 --- a/src/testlib/qplaintestlogger.cpp +++ b/src/testlib/qplaintestlogger.cpp @@ -381,14 +381,14 @@ void QPlainTestLogger::addBenchmarkResult(const QBenchmarkResult &result) printBenchmarkResult(result); } -void QPlainTestLogger::addMessage(MessageTypes type, const char *message, +void QPlainTestLogger::addMessage(MessageTypes type, const QString &message, const char *file, int line) { // suppress non-fatal messages in silent mode if (type != QAbstractTestLogger::QFatal && QTestLog::verboseLevel() < 0) return; - printMessage(QTest::messageType2String(type), message, file, line); + printMessage(QTest::messageType2String(type), qPrintable(message), file, line); } QT_END_NAMESPACE diff --git a/src/testlib/qplaintestlogger_p.h b/src/testlib/qplaintestlogger_p.h index 8aa0d745e9..69dbc89649 100644 --- a/src/testlib/qplaintestlogger_p.h +++ b/src/testlib/qplaintestlogger_p.h @@ -73,7 +73,7 @@ public: const char *file = 0, int line = 0); void addBenchmarkResult(const QBenchmarkResult &result); - void addMessage(MessageTypes type, const char *message, + void addMessage(MessageTypes type, const QString &message, const char *file = 0, int line = 0); private: diff --git a/src/testlib/qtestlog.cpp b/src/testlib/qtestlog.cpp index 037bed643d..74947b3f3a 100644 --- a/src/testlib/qtestlog.cpp +++ b/src/testlib/qtestlog.cpp @@ -209,7 +209,7 @@ namespace QTest { FOREACH_LOGGER(logger->addBenchmarkResult(result)); } - static void addMessage(QAbstractTestLogger::MessageTypes type, const char *message, + static void addMessage(QAbstractTestLogger::MessageTypes type, const QString &message, const char *file = 0, int line = 0) { FOREACH_LOGGER(logger->addMessage(type, message, file, line)); @@ -242,11 +242,10 @@ namespace QTest { static QtMessageHandler oldMessageHandler; - static bool handleIgnoredMessage(QtMsgType type, const char *msg) + static bool handleIgnoredMessage(QtMsgType type, const QString &message) { if (!ignoreResultList) return false; - const QString message = QString::fromLocal8Bit(msg); IgnoreResultList *last = 0; IgnoreResultList *list = ignoreResultList; while (list) { @@ -279,12 +278,11 @@ namespace QTest { QTEST_ASSERT(QTest::TestLoggers::loggerCount() != 0); } - QByteArray msg = message.toLocal8Bit(); - if (handleIgnoredMessage(type, msg)) + if (handleIgnoredMessage(type, message)) // the message is expected, so just swallow it. return; - msg = qMessageFormatString(type, context, message).toLocal8Bit(); + QString msg = qMessageFormatString(type, context, message); msg.chop(1); // remove trailing newline if (type != QtFatalMsg) { @@ -293,7 +291,7 @@ namespace QTest { if (!counter.deref()) { QTest::TestLoggers::addMessage(QAbstractTestLogger::QSystem, - "Maximum amount of warnings exceeded. Use -maxwarnings to override."); + QStringLiteral("Maximum amount of warnings exceeded. Use -maxwarnings to override.")); return; } } @@ -354,15 +352,15 @@ void QTestLog::leaveTestFunction() void QTestLog::printUnhandledIgnoreMessages() { - char msg[1024]; + QString message; QTest::IgnoreResultList *list = QTest::ignoreResultList; while (list) { if (list->pattern.type() == QVariant::String) { - qsnprintf(msg, 1024, "Did not receive message: \"%s\"", qPrintable(list->pattern.toString())); + message = QStringLiteral("Did not receive message: \"") + list->pattern.toString() + QLatin1Char('"'); } else { - qsnprintf(msg, 1024, "Did not receive any message matching: \"%s\"", qPrintable(list->pattern.toRegularExpression().pattern())); + message = QStringLiteral("Did not receive any message matching: \"") + list->pattern.toRegularExpression().pattern() + QLatin1Char('"'); } - QTest::TestLoggers::addMessage(QAbstractTestLogger::Info, msg); + QTest::TestLoggers::addMessage(QAbstractTestLogger::Info, message); list = list->next; } @@ -419,7 +417,7 @@ void QTestLog::addSkip(const char *msg, const char *file, int line) ++QTest::skips; - QTest::TestLoggers::addMessage(QAbstractTestLogger::Skip, msg, file, line); + QTest::TestLoggers::addMessage(QAbstractTestLogger::Skip, QString::fromUtf8(msg), file, line); } void QTestLog::addBenchmarkResult(const QBenchmarkResult &result) @@ -483,14 +481,14 @@ void QTestLog::warn(const char *msg, const char *file, int line) QTEST_ASSERT(msg); if (QTest::TestLoggers::loggerCount() > 0) - QTest::TestLoggers::addMessage(QAbstractTestLogger::Warn, msg, file, line); + QTest::TestLoggers::addMessage(QAbstractTestLogger::Warn, QString::fromUtf8(msg), file, line); } void QTestLog::info(const char *msg, const char *file, int line) { QTEST_ASSERT(msg); - QTest::TestLoggers::addMessage(QAbstractTestLogger::Info, msg, file, line); + QTest::TestLoggers::addMessage(QAbstractTestLogger::Info, QString::fromUtf8(msg), file, line); } void QTestLog::setVerboseLevel(int level) diff --git a/src/testlib/qxmltestlogger.cpp b/src/testlib/qxmltestlogger.cpp index b4035301d3..3fff753c5c 100644 --- a/src/testlib/qxmltestlogger.cpp +++ b/src/testlib/qxmltestlogger.cpp @@ -271,7 +271,7 @@ void QXmlTestLogger::addBenchmarkResult(const QBenchmarkResult &result) outputString(buf.constData()); } -void QXmlTestLogger::addMessage(MessageTypes type, const char *message, +void QXmlTestLogger::addMessage(MessageTypes type, const QString &message, const char *file, int line) { QTestCharBuffer buf; @@ -288,10 +288,10 @@ void QXmlTestLogger::addMessage(MessageTypes type, const char *message, xmlQuote("edFile, file); xmlCdata(&cdataGtag, gtag); xmlCdata(&cdataTag, tag); - xmlCdata(&cdataDescription, message); + xmlCdata(&cdataDescription, message.toUtf8().constData()); QTest::qt_asprintf(&buf, - QTest::messageFormatString(QTest::isEmpty(message), notag), + QTest::messageFormatString(message.isEmpty(), notag), QTest::xmlMessageType2String(type), quotedFile.constData(), line, cdataGtag.constData(), diff --git a/src/testlib/qxmltestlogger_p.h b/src/testlib/qxmltestlogger_p.h index 65699af3ad..8ca15e47e2 100644 --- a/src/testlib/qxmltestlogger_p.h +++ b/src/testlib/qxmltestlogger_p.h @@ -77,7 +77,7 @@ public: const char *file = 0, int line = 0); void addBenchmarkResult(const QBenchmarkResult &result); - void addMessage(MessageTypes type, const char *message, + void addMessage(MessageTypes type, const QString &message, const char *file = 0, int line = 0); static int xmlCdata(QTestCharBuffer *dest, char const* src); diff --git a/src/testlib/qxunittestlogger.cpp b/src/testlib/qxunittestlogger.cpp index 0a1a5fb6f9..a47f77ae49 100644 --- a/src/testlib/qxunittestlogger.cpp +++ b/src/testlib/qxunittestlogger.cpp @@ -220,7 +220,7 @@ void QXunitTestLogger::addIncident(IncidentTypes type, const char *description, have some information about the expected failure. */ if (type == QAbstractTestLogger::XFail) { - QXunitTestLogger::addMessage(QAbstractTestLogger::Info, description, file, line); + QXunitTestLogger::addMessage(QAbstractTestLogger::Info, QString::fromUtf8(description), file, line); } } @@ -263,7 +263,7 @@ void QXunitTestLogger::addTag(QTestElement* element) element->addAttribute(QTest::AI_Tag, buf.constData()); } -void QXunitTestLogger::addMessage(MessageTypes type, const char *message, const char *file, int line) +void QXunitTestLogger::addMessage(MessageTypes type, const QString &message, const char *file, int line) { QTestElement *errorElement = new QTestElement(QTest::LET_Error); const char *typeBuf = 0; @@ -296,7 +296,7 @@ void QXunitTestLogger::addMessage(MessageTypes type, const char *message, const } errorElement->addAttribute(QTest::AI_Type, typeBuf); - errorElement->addAttribute(QTest::AI_Description, message); + errorElement->addAttribute(QTest::AI_Description, message.toUtf8().constData()); addTag(errorElement); if (file) @@ -314,7 +314,7 @@ void QXunitTestLogger::addMessage(MessageTypes type, const char *message, const // Also add the message to the system error log (i.e. stderr), if one exists if (errorLogElement) { QTestElement *systemErrorElement = new QTestElement(QTest::LET_Error); - systemErrorElement->addAttribute(QTest::AI_Description, message); + systemErrorElement->addAttribute(QTest::AI_Description, message.toUtf8().constData()); errorLogElement->addLogElement(systemErrorElement); } } diff --git a/src/testlib/qxunittestlogger_p.h b/src/testlib/qxunittestlogger_p.h index 2c39fa952c..754462473a 100644 --- a/src/testlib/qxunittestlogger_p.h +++ b/src/testlib/qxunittestlogger_p.h @@ -77,7 +77,7 @@ class QXunitTestLogger : public QAbstractTestLogger void addBenchmarkResult(const QBenchmarkResult &result); void addTag(QTestElement* element); - void addMessage(MessageTypes type, const char *message, + void addMessage(MessageTypes type, const QString &message, const char *file = 0, int line = 0); private: diff --git a/tests/auto/testlib/selftests/badxml/tst_badxml.cpp b/tests/auto/testlib/selftests/badxml/tst_badxml.cpp index 1ccbdd7899..3f2385bb30 100644 --- a/tests/auto/testlib/selftests/badxml/tst_badxml.cpp +++ b/tests/auto/testlib/selftests/badxml/tst_badxml.cpp @@ -41,6 +41,7 @@ #include +#include #include #include @@ -61,6 +62,8 @@ private slots: void failWithNoFile() const; + void encoding(); + public: static QList const& badStrings(); }; @@ -126,6 +129,24 @@ void tst_BadXml::failWithNoFile() const QTest::qFail("failure message", 0, 0); } +// QTBUG-35743, test whether XML is using correct UTF-8 encoding +// on platforms where the console encoding differs. +void tst_BadXml::encoding() +{ + QStringList arguments = QCoreApplication::arguments(); + arguments.pop_front(); // Prevent match on binary "badxml" + if (arguments.filter(QStringLiteral("xml")).isEmpty()) + QSKIP("Skipped for text due to unpredictable console encoding."); + QString string; + string += QChar(ushort(0xDC)); // German umlaut Ue + string += QStringLiteral("lrich "); + string += QChar(ushort(0xDC)); // German umlaut Ue + string += QStringLiteral("ml"); + string += QChar(ushort(0xE4)); // German umlaut ae + string += QStringLiteral("ut"); + qDebug() << string; +} + /* Outputs a message containing a bad string. */ diff --git a/tests/auto/testlib/selftests/expected_badxml.lightxml b/tests/auto/testlib/selftests/expected_badxml.lightxml index 15981b12be..e4c79e3bb0 100644 --- a/tests/auto/testlib/selftests/expected_badxml.lightxml +++ b/tests/auto/testlib/selftests/expected_badxml.lightxml @@ -4,14 +4,14 @@ - + text ]]]> more text]]> - + text ]]]> more text]]> @@ -27,7 +27,7 @@ - + @@ -43,7 +43,7 @@ open < tags < text]]> - + open < tags < text]]> @@ -59,7 +59,7 @@ " mixed ]]]> up > " in < the ]]]> hopes < of triggering "< ]]]> bugs]]> - + " mixed ]]]> up > " in < the ]]]> hopes < of triggering "< ]]]> bugs]]> @@ -71,7 +71,7 @@ " mixed ]]]> up > " in < the ]]]> hopes < of triggering "< ]]]> bugs]]> - + @@ -102,16 +102,23 @@ - + - + + + + + + + + - + diff --git a/tests/auto/testlib/selftests/expected_badxml.txt b/tests/auto/testlib/selftests/expected_badxml.txt index 3c65231529..a42013df0b 100644 --- a/tests/auto/testlib/selftests/expected_badxml.txt +++ b/tests/auto/testlib/selftests/expected_badxml.txt @@ -3,28 +3,28 @@ Config: Using QtTest library @INSERT_QT_VERSION_HERE@, Qt @INSERT_QT_VERSION_HER PASS : tst_BadXml::initTestCase() QDEBUG : tst_BadXml::badDataTag(fail end cdata ]]> text ]]> more text) a message FAIL! : tst_BadXml::badDataTag(fail end cdata ]]> text ]]> more text) a failure - Loc: [tst_badxml.cpp(111)] + Loc: [tst_badxml.cpp(114)] QDEBUG : tst_BadXml::badDataTag(pass end cdata ]]> text ]]> more text) a message PASS : tst_BadXml::badDataTag(pass end cdata ]]> text ]]> more text) RESULT : tst_BadXml::badDataTag():"pass end cdata ]]> text ]]> more text": 0 events per iteration (total: 0, iterations: 1) QDEBUG : tst_BadXml::badDataTag(fail quotes " text" more text) a message FAIL! : tst_BadXml::badDataTag(fail quotes " text" more text) a failure - Loc: [tst_badxml.cpp(111)] + Loc: [tst_badxml.cpp(114)] QDEBUG : tst_BadXml::badDataTag(pass quotes " text" more text) a message PASS : tst_BadXml::badDataTag(pass quotes " text" more text) RESULT : tst_BadXml::badDataTag():"pass quotes " text" more text": 0 events per iteration (total: 0, iterations: 1) QDEBUG : tst_BadXml::badDataTag(fail xml close > open < tags < text) a message FAIL! : tst_BadXml::badDataTag(fail xml close > open < tags < text) a failure - Loc: [tst_badxml.cpp(111)] + Loc: [tst_badxml.cpp(114)] QDEBUG : tst_BadXml::badDataTag(pass xml close > open < tags < text) a message PASS : tst_BadXml::badDataTag(pass xml close > open < tags < text) RESULT : tst_BadXml::badDataTag():"pass xml close > open < tags < text": 0 events per iteration (total: 0, iterations: 1) QDEBUG : tst_BadXml::badDataTag(fail all > " mixed ]]> up > " in < the ]]> hopes < of triggering "< ]]> bugs) a message FAIL! : tst_BadXml::badDataTag(fail all > " mixed ]]> up > " in < the ]]> hopes < of triggering "< ]]> bugs) a failure - Loc: [tst_badxml.cpp(111)] + Loc: [tst_badxml.cpp(114)] QDEBUG : tst_BadXml::badDataTag(pass all > " mixed ]]> up > " in < the ]]> hopes < of triggering "< ]]> bugs) a message PASS : tst_BadXml::badDataTag(pass all > " mixed ]]> up > " in < the ]]> hopes < of triggering "< ]]> bugs) RESULT : tst_BadXml::badDataTag():"pass all > " mixed ]]> up > " in < the ]]> hopes < of triggering "< ]]> bugs": @@ -38,6 +38,8 @@ PASS : tst_BadXml::badMessage(string 2) QDEBUG : tst_BadXml::badMessage(string 3) all > " mixed ]]> up > " in < the ]]> hopes < of triggering "< ]]> bugs PASS : tst_BadXml::badMessage(string 3) FAIL! : tst_BadXml::failWithNoFile() failure message +SKIP : tst_BadXml::encoding() Skipped for text due to unpredictable console encoding. + Loc: [tst_badxml.cpp(139)] PASS : tst_BadXml::cleanupTestCase() -Totals: 10 passed, 5 failed, 0 skipped +Totals: 10 passed, 5 failed, 1 skipped ********* Finished testing of tst_BadXml ********* diff --git a/tests/auto/testlib/selftests/expected_badxml.xml b/tests/auto/testlib/selftests/expected_badxml.xml index c3330a6b97..0811db0f3a 100644 --- a/tests/auto/testlib/selftests/expected_badxml.xml +++ b/tests/auto/testlib/selftests/expected_badxml.xml @@ -6,14 +6,14 @@ - + text ]]]> more text]]> - + text ]]]> more text]]> @@ -29,7 +29,7 @@ - + @@ -45,7 +45,7 @@ open < tags < text]]> - + open < tags < text]]> @@ -61,7 +61,7 @@ " mixed ]]]> up > " in < the ]]]> hopes < of triggering "< ]]]> bugs]]> - + " mixed ]]]> up > " in < the ]]]> hopes < of triggering "< ]]]> bugs]]> @@ -73,7 +73,7 @@ " mixed ]]]> up > " in < the ]]]> hopes < of triggering "< ]]]> bugs]]> - + @@ -104,17 +104,24 @@ - + - + + + + + + + + - + diff --git a/tests/auto/testlib/selftests/expected_badxml.xunitxml b/tests/auto/testlib/selftests/expected_badxml.xunitxml index 939e887a88..a696da58c9 100644 --- a/tests/auto/testlib/selftests/expected_badxml.xunitxml +++ b/tests/auto/testlib/selftests/expected_badxml.xunitxml @@ -1,5 +1,5 @@ - + @@ -28,6 +28,9 @@ + + + @@ -42,5 +45,6 @@ open < tags < text]]> " mixed ]]]> up > " in < the ]]]> hopes < of triggering "< ]]]> bugs]]> + diff --git a/tests/auto/testlib/selftests/tst_selftests.cpp b/tests/auto/testlib/selftests/tst_selftests.cpp index 97083d8d61..c2265ad198 100644 --- a/tests/auto/testlib/selftests/tst_selftests.cpp +++ b/tests/auto/testlib/selftests/tst_selftests.cpp @@ -486,6 +486,8 @@ void tst_Selftests::runSubTest_data() continue; } } + if (subtest == "badxml" && (loggerSet.name == "all loggers" || loggerSet.name.contains("txt"))) + continue; // XML only, do not mix txt and XML for encoding test. const bool crashes = subtest == QLatin1String("assert") || subtest == QLatin1String("exceptionthrow") || subtest == QLatin1String("fetchbogus") || subtest == QLatin1String("crashedterminate") || subtest == QLatin1String("crashes") || subtest == QLatin1String("silent");