diff options
Diffstat (limited to 'tests/auto/corelib/global/qlogging/tst_qlogging.cpp')
-rw-r--r-- | tests/auto/corelib/global/qlogging/tst_qlogging.cpp | 204 |
1 files changed, 110 insertions, 94 deletions
diff --git a/tests/auto/corelib/global/qlogging/tst_qlogging.cpp b/tests/auto/corelib/global/qlogging/tst_qlogging.cpp index 21e07630e2..a8027a4157 100644 --- a/tests/auto/corelib/global/qlogging/tst_qlogging.cpp +++ b/tests/auto/corelib/global/qlogging/tst_qlogging.cpp @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Olivier Goffart <ogoffart@woboq.com> ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -63,8 +64,9 @@ private slots: void cleanupFuncinfo(); #endif + void qMessagePattern_data(); void qMessagePattern(); - void qMessagePatternIf(); + void setMessagePattern(); private: QString m_appDir; @@ -641,11 +643,95 @@ void tst_qmessagehandler::cleanupFuncinfo() } #endif +void tst_qmessagehandler::qMessagePattern_data() +{ + QTest::addColumn<QString>("pattern"); + QTest::addColumn<bool>("valid"); + QTest::addColumn<QList<QByteArray> >("expected"); + + // %{file} is tricky because of shadow builds + QTest::newRow("basic") << "%{type} %{appname} %{line} %{function} %{message}" << true << (QList<QByteArray>() + << "debug 52 T::T static constructor" + // we can't be sure whether the QT_MESSAGE_PATTERN is already destructed + << "static destructor" + << "debug tst_qlogging 73 MyClass::myFunction from_a_function 34" + << "debug tst_qlogging 83 main qDebug" + << "warning tst_qlogging 84 main qWarning" + << "critical tst_qlogging 85 main qCritical" + << "warning tst_qlogging 88 main qDebug with category" + << "debug tst_qlogging 92 main qDebug2"); + + + QTest::newRow("invalid") << "PREFIX: %{unknown} %{message}" << false << (QList<QByteArray>() + << "QT_MESSAGE_PATTERN: Unknown placeholder %{unknown}" + << "PREFIX: qDebug"); + + // test the if condition + QTest::newRow("ifs") << "[%{if-debug}D%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{if-category}%{category}: %{endif}%{message}" + << true << (QList<QByteArray>() + << "[D] static constructor" + // we can't be sure whether the QT_MESSAGE_PATTERN is already destructed + << "static destructor" + << "[D] qDebug" + << "[W] qWarning" + << "[C] qCritical" + << "[W] category: qDebug with category" + << "[D] qDebug2"); + + // test few errors cases + QTest::newRow("ifs-invalid1") << "PREFIX: %{unknown} %{endif} %{if-warning}" + << false << (QList<QByteArray>() + << "QT_MESSAGE_PATTERN: Unknown placeholder %{unknown}" + << "QT_MESSAGE_PATTERN: %{endif} without an %{if-*}" + << "QT_MESSAGE_PATTERN: missing %{endif}"); + + QTest::newRow("ifs-invalid2") << "A %{if-debug}DEBUG%{if-warning}WARNING%{endif} %{message} " + << false << (QList<QByteArray>() + << "QT_MESSAGE_PATTERN: %{if-*} cannot be nested" + << "A DEBUG qDebug " + << "A qWarning "); + + // This test won't work when midnight is too close... wait a bit + while (QTime::currentTime() > QTime(23, 59, 30)) + QTest::qWait(10000); + QTest::newRow("time") << "/%{time yyyy - MM - d}/%{message}" + << true << (QList<QByteArray>() + << ('/' + QDateTime::currentDateTime().toString("yyyy - MM - d").toUtf8() + "/qDebug")); + + // %{time} should have a padding of 6 so if it takes less than 10 seconds to show + // the first message, there should be 5 spaces + QTest::newRow("time") << "<%{time}>%{message}" << true << (QList<QByteArray>() + << "< "); + +#ifdef __GLIBC__ +#ifdef QT_NAMESPACE +#define QT_NAMESPACE_STR QT_STRINGIFY(QT_NAMESPACE::) +#else +#define QT_NAMESPACE_STR "" +#endif + +#ifndef QT_NO_DEBUG + QTest::newRow("backtrace") << "[%{backtrace}] %{message}" << true << (QList<QByteArray>() + // MyClass::qt_static_metacall is explicitly marked as hidden in the Q_OBJECT macro + << "[MyClass::myFunction|MyClass::mySlot1|?app?|" QT_NAMESPACE_STR "QMetaMethod::invoke|" QT_NAMESPACE_STR "QMetaObject::invokeMethod] from_a_function 34"); +#endif + + QTest::newRow("backtrace depth,separator") << "[%{backtrace depth=2 separator=\"\n\"}] %{message}" << true << (QList<QByteArray>() + << "[MyClass::myFunction\nMyClass::mySlot1] from_a_function 34"); +#endif + +} + + void tst_qmessagehandler::qMessagePattern() { #ifdef QT_NO_PROCESS QSKIP("This test requires QProcess support"); #else + QFETCH(QString, pattern); + QFETCH(bool, valid); + QFETCH(QList<QByteArray>, expected); + QProcess process; const QString appExe = m_appDir + "/app"; @@ -653,8 +739,7 @@ void tst_qmessagehandler::qMessagePattern() // test QT_MESSAGE_PATTERN // QStringList environment = m_baseEnvironment; - // %{file} is tricky because of shadow builds - environment.prepend("QT_MESSAGE_PATTERN=\"%{type} %{appname} %{line} %{function} %{message}\""); + environment.prepend("QT_MESSAGE_PATTERN=\"" + pattern + "\""); process.setEnvironment(environment); process.start(appExe); @@ -665,35 +750,33 @@ void tst_qmessagehandler::qMessagePattern() QByteArray output = process.readAllStandardError(); // qDebug() << output; QVERIFY(!output.isEmpty()); + QCOMPARE(!output.contains("QT_MESSAGE_PATTERN"), valid); - QVERIFY(output.contains("debug 46 T::T static constructor")); - // we can't be sure whether the QT_MESSAGE_PATTERN is already destructed - QVERIFY(output.contains("static destructor")); - QVERIFY(output.contains("debug tst_qlogging 57 main qDebug")); - QVERIFY(output.contains("warning tst_qlogging 58 main qWarning")); - QVERIFY(output.contains("critical tst_qlogging 59 main qCritical")); - QVERIFY(output.contains("warning tst_qlogging 62 main qDebug with category")); - QVERIFY(output.contains("debug tst_qlogging 66 main qDebug2")); - - environment = m_baseEnvironment; - environment.prepend("QT_MESSAGE_PATTERN=\"PREFIX: %{unknown} %{message}\""); - process.setEnvironment(environment); - - process.start(appExe); - QVERIFY2(process.waitForStarted(), qPrintable( - QString::fromLatin1("Could not start %1: %2").arg(appExe, process.errorString()))); - process.waitForFinished(); - - output = process.readAllStandardError(); -// qDebug() << output; - QVERIFY(!output.isEmpty()); + foreach (const QByteArray &e, expected) { + if (!output.contains(e)) { + qDebug() << output; + qDebug() << "expected: " << e; + QVERIFY(output.contains(e)); + } + } +#endif +} - QVERIFY(output.contains("QT_MESSAGE_PATTERN: Unknown placeholder %{unknown}")); - QVERIFY(output.contains("PREFIX: qDebug")); +void tst_qmessagehandler::setMessagePattern() +{ +#ifdef QT_NO_PROCESS + QSKIP("This test requires QProcess support"); +#else // // test qSetMessagePattern // + + QProcess process; + const QString appExe = m_appDir + "/app"; + + // make sure there is no QT_MESSAGE_PATTERN in the environment + QStringList environment = m_baseEnvironment; QMutableListIterator<QString> iter(environment); while (iter.hasNext()) { if (iter.next().startsWith("QT_MESSAGE_PATTERN")) @@ -706,7 +789,7 @@ void tst_qmessagehandler::qMessagePattern() QString::fromLatin1("Could not start %1: %2").arg(appExe, process.errorString()))); process.waitForFinished(); - output = process.readAllStandardError(); + QByteArray output = process.readAllStandardError(); //qDebug() << output; QByteArray expected = "static constructor\n" "[debug] qDebug\n" @@ -720,73 +803,6 @@ void tst_qmessagehandler::qMessagePattern() #endif // !QT_NO_PROCESS } -void tst_qmessagehandler::qMessagePatternIf() -{ -#ifdef QT_NO_PROCESS - QSKIP("This test requires QProcess support"); -#else - QProcess process; - const QString appExe = m_appDir + "/app"; - - QStringList environment = m_baseEnvironment; - environment.prepend("QT_MESSAGE_PATTERN=\"[%{if-debug}D%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{if-category}%{category}: %{endif}%{message}\""); - process.setEnvironment(environment); - process.start(appExe); - QVERIFY2(process.waitForStarted(), qPrintable( - QString::fromLatin1("Could not start %1: %2").arg(appExe, process.errorString()))); - process.waitForFinished(); - - QByteArray output = process.readAllStandardError(); - // qDebug() << output; - QVERIFY(!output.isEmpty()); - QVERIFY(!output.contains("QT_MESSAGE_PATTERN")); - - QVERIFY(output.contains("[D] static constructor")); - // we can't be sure whether the QT_MESSAGE_PATTERN is already destructed - QVERIFY(output.contains("static destructor")); - QVERIFY(output.contains("[D] qDebug")); - QVERIFY(output.contains("[W] qWarning")); - QVERIFY(output.contains("[C] qCritical")); - QVERIFY(output.contains("[W] category: qDebug with category")); - QVERIFY(output.contains("[D] qDebug2")); - - // - // Tests some errors - // - environment = m_baseEnvironment; - environment.prepend("QT_MESSAGE_PATTERN=\"PREFIX: %{unknown} %{endif} %{if-warning}\""); - process.setEnvironment(environment); - - process.start(appExe); - QVERIFY2(process.waitForStarted(), qPrintable( - QString::fromLatin1("Could not start %1: %2").arg(appExe, process.errorString()))); - process.waitForFinished(); - - output = process.readAllStandardError(); - // qDebug() << output; - QVERIFY(!output.isEmpty()); - QVERIFY(output.contains("QT_MESSAGE_PATTERN: Unknown placeholder %{unknown}")); - QVERIFY(output.contains("QT_MESSAGE_PATTERN: %{endif} without an %{if-*}")); - QVERIFY(output.contains("QT_MESSAGE_PATTERN: missing %{endif}")); - - - environment = m_baseEnvironment; - environment.prepend("QT_MESSAGE_PATTERN=\"A %{if-debug}DEBUG%{if-warning}WARNING%{endif} %{message} \""); - process.setEnvironment(environment); - - process.start(appExe); - QVERIFY2(process.waitForStarted(), qPrintable( - QString::fromLatin1("Could not start %1: %2").arg(appExe, process.errorString()))); - process.waitForFinished(); - - output = process.readAllStandardError(); - // qDebug() << output; - QVERIFY(!output.isEmpty()); - QVERIFY(output.contains("QT_MESSAGE_PATTERN: %{if-*} cannot be nested")); - QVERIFY(output.contains("A DEBUG qDebug")); - QVERIFY(output.contains("A qWarning")); -#endif // !QT_NO_PROCESS -} QTEST_MAIN(tst_qmessagehandler) #include "tst_qlogging.moc" |