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 <frederik.gladhorn@digia.com>
This commit is contained in:
Friedemann Kleint 2014-01-16 13:45:37 +01:00 committed by The Qt Project
parent 5c19fad8c1
commit afe3902a30
14 changed files with 92 additions and 51 deletions

View File

@ -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);

View File

@ -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

View File

@ -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:

View File

@ -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)

View File

@ -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(&quotedFile, 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(),

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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:

View File

@ -41,6 +41,7 @@
#include <QtCore/QCoreApplication>
#include <QtCore/QStringList>
#include <QtTest/QtTest>
#include <private/qmetaobjectbuilder_p.h>
@ -61,6 +62,8 @@ private slots:
void failWithNoFile() const;
void encoding();
public:
static QList<QByteArray> 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.
*/

View File

@ -4,14 +4,14 @@
</Environment>
<TestFunction name="initTestCase">
<Incident type="pass" file="" line="0" />
<Duration msecs="0"/>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="badDataTag">
<Message type="qdebug" file="" line="0">
<DataTag><![CDATA[fail end cdata ]]]><![CDATA[]> text ]]]><![CDATA[]> more text]]></DataTag>
<Description><![CDATA[a message]]></Description>
</Message>
<Incident type="fail" file="tst_badxml.cpp" line="111">
<Incident type="fail" file="tst_badxml.cpp" line="114">
<DataTag><![CDATA[fail end cdata ]]]><![CDATA[]> text ]]]><![CDATA[]> more text]]></DataTag>
<Description><![CDATA[a failure]]></Description>
</Incident>
@ -27,7 +27,7 @@
<DataTag><![CDATA[fail quotes " text" more text]]></DataTag>
<Description><![CDATA[a message]]></Description>
</Message>
<Incident type="fail" file="tst_badxml.cpp" line="111">
<Incident type="fail" file="tst_badxml.cpp" line="114">
<DataTag><![CDATA[fail quotes " text" more text]]></DataTag>
<Description><![CDATA[a failure]]></Description>
</Incident>
@ -43,7 +43,7 @@
<DataTag><![CDATA[fail xml close > open < tags < text]]></DataTag>
<Description><![CDATA[a message]]></Description>
</Message>
<Incident type="fail" file="tst_badxml.cpp" line="111">
<Incident type="fail" file="tst_badxml.cpp" line="114">
<DataTag><![CDATA[fail xml close > open < tags < text]]></DataTag>
<Description><![CDATA[a failure]]></Description>
</Incident>
@ -59,7 +59,7 @@
<DataTag><![CDATA[fail all > " mixed ]]]><![CDATA[]> up > " in < the ]]]><![CDATA[]> hopes < of triggering "< ]]]><![CDATA[]> bugs]]></DataTag>
<Description><![CDATA[a message]]></Description>
</Message>
<Incident type="fail" file="tst_badxml.cpp" line="111">
<Incident type="fail" file="tst_badxml.cpp" line="114">
<DataTag><![CDATA[fail all > " mixed ]]]><![CDATA[]> up > " in < the ]]]><![CDATA[]> hopes < of triggering "< ]]]><![CDATA[]> bugs]]></DataTag>
<Description><![CDATA[a failure]]></Description>
</Incident>
@ -71,7 +71,7 @@
<DataTag><![CDATA[pass all > " mixed ]]]><![CDATA[]> up > " in < the ]]]><![CDATA[]> hopes < of triggering "< ]]]><![CDATA[]> bugs]]></DataTag>
</Incident>
<BenchmarkResult metric="Events" tag="pass all &gt; &quot; mixed ]]&gt; up &gt; &quot; in &lt; the ]]&gt; hopes &lt; of triggering &quot;&lt; ]]&gt; bugs" value="0" iterations="1" />
<Duration msecs="0"/>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="badMessage">
<Message type="qdebug" file="" line="0">
@ -102,16 +102,23 @@
<Incident type="pass" file="" line="0">
<DataTag><![CDATA[string 3]]></DataTag>
</Incident>
<Duration msecs="0"/>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="failWithNoFile">
<Incident type="fail" file="" line="0">
<Description><![CDATA[failure message]]></Description>
</Incident>
<Duration msecs="0"/>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="encoding">
<Message type="qdebug" file="" line="0">
<Description><![CDATA["Ülrich Ümläut"]]></Description>
</Message>
<Incident type="pass" file="" line="0" />
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="cleanupTestCase">
<Incident type="pass" file="" line="0" />
<Duration msecs="0"/>
<Duration msecs="0"/>
</TestFunction>
<Duration msecs="0"/>

View File

@ -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 *********

View File

@ -6,14 +6,14 @@
</Environment>
<TestFunction name="initTestCase">
<Incident type="pass" file="" line="0" />
<Duration msecs="0"/>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="badDataTag">
<Message type="qdebug" file="" line="0">
<DataTag><![CDATA[fail end cdata ]]]><![CDATA[]> text ]]]><![CDATA[]> more text]]></DataTag>
<Description><![CDATA[a message]]></Description>
</Message>
<Incident type="fail" file="tst_badxml.cpp" line="111">
<Incident type="fail" file="tst_badxml.cpp" line="114">
<DataTag><![CDATA[fail end cdata ]]]><![CDATA[]> text ]]]><![CDATA[]> more text]]></DataTag>
<Description><![CDATA[a failure]]></Description>
</Incident>
@ -29,7 +29,7 @@
<DataTag><![CDATA[fail quotes " text" more text]]></DataTag>
<Description><![CDATA[a message]]></Description>
</Message>
<Incident type="fail" file="tst_badxml.cpp" line="111">
<Incident type="fail" file="tst_badxml.cpp" line="114">
<DataTag><![CDATA[fail quotes " text" more text]]></DataTag>
<Description><![CDATA[a failure]]></Description>
</Incident>
@ -45,7 +45,7 @@
<DataTag><![CDATA[fail xml close > open < tags < text]]></DataTag>
<Description><![CDATA[a message]]></Description>
</Message>
<Incident type="fail" file="tst_badxml.cpp" line="111">
<Incident type="fail" file="tst_badxml.cpp" line="114">
<DataTag><![CDATA[fail xml close > open < tags < text]]></DataTag>
<Description><![CDATA[a failure]]></Description>
</Incident>
@ -61,7 +61,7 @@
<DataTag><![CDATA[fail all > " mixed ]]]><![CDATA[]> up > " in < the ]]]><![CDATA[]> hopes < of triggering "< ]]]><![CDATA[]> bugs]]></DataTag>
<Description><![CDATA[a message]]></Description>
</Message>
<Incident type="fail" file="tst_badxml.cpp" line="111">
<Incident type="fail" file="tst_badxml.cpp" line="114">
<DataTag><![CDATA[fail all > " mixed ]]]><![CDATA[]> up > " in < the ]]]><![CDATA[]> hopes < of triggering "< ]]]><![CDATA[]> bugs]]></DataTag>
<Description><![CDATA[a failure]]></Description>
</Incident>
@ -73,7 +73,7 @@
<DataTag><![CDATA[pass all > " mixed ]]]><![CDATA[]> up > " in < the ]]]><![CDATA[]> hopes < of triggering "< ]]]><![CDATA[]> bugs]]></DataTag>
</Incident>
<BenchmarkResult metric="Events" tag="pass all &gt; &quot; mixed ]]&gt; up &gt; &quot; in &lt; the ]]&gt; hopes &lt; of triggering &quot;&lt; ]]&gt; bugs" value="0" iterations="1" />
<Duration msecs="0"/>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="badMessage">
<Message type="qdebug" file="" line="0">
@ -104,17 +104,24 @@
<Incident type="pass" file="" line="0">
<DataTag><![CDATA[string 3]]></DataTag>
</Incident>
<Duration msecs="0"/>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="failWithNoFile">
<Incident type="fail" file="" line="0">
<Description><![CDATA[failure message]]></Description>
</Incident>
<Duration msecs="0"/>
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="encoding">
<Message type="qdebug" file="" line="0">
<Description><![CDATA["Ülrich Ümläut"]]></Description>
</Message>
<Incident type="pass" file="" line="0" />
<Duration msecs="0"/>
</TestFunction>
<TestFunction name="cleanupTestCase">
<Incident type="pass" file="" line="0" />
<Duration msecs="0"/>
<Duration msecs="0"/>
</TestFunction>
<Duration msecs="0"/>
</TestCase>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite errors="12" failures="5" tests="5" name="tst_BadXml">
<testsuite errors="13" failures="5" tests="6" name="tst_BadXml">
<properties>
<property value="@INSERT_QT_VERSION_HERE@" name="QTestVersion"/>
<property value="@INSERT_QT_VERSION_HERE@" name="QtVersion"/>
@ -28,6 +28,9 @@
<testcase result="fail" name="failWithNoFile">
<failure message="failure message" result="fail"/>
</testcase>
<testcase result="pass" name="encoding">
<!-- message="&quot;Ülrich Ümläut&quot;" type="qdebug" -->
</testcase>
<testcase result="pass" name="cleanupTestCase"/>
<system-err>
<![CDATA[a message]]>
@ -42,5 +45,6 @@
<![CDATA[quotes " text" more text]]>
<![CDATA[xml close > open < tags < text]]>
<![CDATA[all > " mixed ]]]><![CDATA[]> up > " in < the ]]]><![CDATA[]> hopes < of triggering "< ]]]><![CDATA[]> bugs]]>
<![CDATA["Ülrich Ümläut"]]>
</system-err>
</testsuite>

View File

@ -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");