diff options
Diffstat (limited to 'tests/auto/corelib/global')
-rw-r--r-- | tests/auto/corelib/global/global.pro | 2 | ||||
-rw-r--r-- | tests/auto/corelib/global/qflags/tst_qflags.cpp | 24 | ||||
-rw-r--r-- | tests/auto/corelib/global/qglobal/tst_qglobal.cpp | 64 | ||||
-rw-r--r-- | tests/auto/corelib/global/qhooks/qhooks.pro | 4 | ||||
-rw-r--r-- | tests/auto/corelib/global/qhooks/tst_qhooks.cpp | 90 | ||||
-rw-r--r-- | tests/auto/corelib/global/qlogging/app/app.pro | 3 | ||||
-rw-r--r-- | tests/auto/corelib/global/qlogging/app/main.cpp | 31 | ||||
-rw-r--r-- | tests/auto/corelib/global/qlogging/tst_qlogging.cpp | 260 |
8 files changed, 385 insertions, 93 deletions
diff --git a/tests/auto/corelib/global/global.pro b/tests/auto/corelib/global/global.pro index c05905bd15..219e9de818 100644 --- a/tests/auto/corelib/global/global.pro +++ b/tests/auto/corelib/global/global.pro @@ -9,4 +9,4 @@ SUBDIRS=\ qlogging \ qtendian \ qglobalstatic \ - + qhooks diff --git a/tests/auto/corelib/global/qflags/tst_qflags.cpp b/tests/auto/corelib/global/qflags/tst_qflags.cpp index 73a69a1309..42add6150a 100644 --- a/tests/auto/corelib/global/qflags/tst_qflags.cpp +++ b/tests/auto/corelib/global/qflags/tst_qflags.cpp @@ -50,6 +50,7 @@ private slots: void constExpr(); void signedness(); void classEnum(); + void initializerLists(); }; void tst_QFlags::testFlag() const @@ -143,6 +144,9 @@ enum class MyStrictEnum { StrictZero, StrictOne, StrictTwo, StrictFour=4 }; Q_DECLARE_FLAGS( MyStrictFlags, MyStrictEnum ) Q_DECLARE_OPERATORS_FOR_FLAGS( MyStrictFlags ) +enum class MyStrictNoOpEnum { StrictZero, StrictOne, StrictTwo, StrictFour=4 }; +Q_DECLARE_FLAGS( MyStrictNoOpFlags, MyStrictNoOpEnum ) + Q_STATIC_ASSERT( !QTypeInfo<MyStrictFlags>::isComplex ); Q_STATIC_ASSERT( !QTypeInfo<MyStrictFlags>::isStatic ); Q_STATIC_ASSERT( !QTypeInfo<MyStrictFlags>::isLarge ); @@ -253,6 +257,26 @@ void tst_QFlags::classEnum() #endif } +void tst_QFlags::initializerLists() +{ +#if defined(Q_COMPILER_INITIALIZER_LISTS) + Qt::MouseButtons bts = { Qt::LeftButton, Qt::RightButton }; + QVERIFY(bts.testFlag(Qt::LeftButton)); + QVERIFY(bts.testFlag(Qt::RightButton)); + QVERIFY(!bts.testFlag(Qt::MiddleButton)); + +#if defined(Q_COMPILER_CLASS_ENUM) + MyStrictNoOpFlags flags = { MyStrictNoOpEnum::StrictOne, MyStrictNoOpEnum::StrictFour }; + QVERIFY(flags.testFlag(MyStrictNoOpEnum::StrictOne)); + QVERIFY(flags.testFlag(MyStrictNoOpEnum::StrictFour)); + QVERIFY(!flags.testFlag(MyStrictNoOpEnum::StrictTwo)); +#endif // Q_COMPILER_CLASS_ENUM + +#else + QSKIP("This test requires C++11 initializer_list support."); +#endif // Q_COMPILER_INITIALIZER_LISTS +} + // (statically) check QTypeInfo for QFlags instantiations: enum MyEnum { Zero, One, Two, Four=4 }; Q_DECLARE_FLAGS( MyFlags, MyEnum ) diff --git a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp index 4eb3e4fc98..0389ae7976 100644 --- a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp +++ b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp @@ -43,6 +43,9 @@ #include <QtTest/QtTest> #include <QtCore/qtypetraits.h> +#include <QPair> +#include <QTextCodec> + class tst_QGlobal: public QObject { Q_OBJECT @@ -60,6 +63,8 @@ private slots: void isEnum(); void qAlignOf(); void integerForSize(); + void qprintable(); + void qprintable_data(); }; void tst_QGlobal::qIsNull() @@ -588,5 +593,64 @@ void tst_QGlobal::integerForSize() Q_STATIC_ASSERT(sizeof(QIntegerForSize<8>::Unsigned) == 8); } +typedef QPair<const char *, const char *> stringpair; +Q_DECLARE_METATYPE(stringpair) + +void tst_QGlobal::qprintable() +{ + QFETCH(QList<stringpair>, localestrings); + QFETCH(int, utf8index); + + QVERIFY(utf8index >= 0 && utf8index < localestrings.count()); + if (utf8index < 0 || utf8index >= localestrings.count()) + return; + + const char *const utf8string = localestrings.at(utf8index).second; + + QString string = QString::fromUtf8(utf8string); + + foreach (const stringpair &pair, localestrings) { + QTextCodec *codec = QTextCodec::codecForName(pair.first); + if (!codec) + continue; + QTextCodec::setCodecForLocale(codec); + // test qPrintable() + QVERIFY(qstrcmp(qPrintable(string), pair.second) == 0); + foreach (const stringpair &pair2, localestrings) { + if (pair2.second == pair.second) + continue; + QVERIFY(qstrcmp(qPrintable(string), pair2.second) != 0); + } + // test qUtf8Printable() + QVERIFY(qstrcmp(qUtf8Printable(string), utf8string) == 0); + foreach (const stringpair &pair2, localestrings) { + if (qstrcmp(pair2.second, utf8string) == 0) + continue; + QVERIFY(qstrcmp(qUtf8Printable(string), pair2.second) != 0); + } + } + + QTextCodec::setCodecForLocale(0); +} + +void tst_QGlobal::qprintable_data() +{ + QTest::addColumn<QList<stringpair> >("localestrings"); + QTest::addColumn<int>("utf8index"); // index of utf8 string + + // Unicode: HIRAGANA LETTER A, I, U, E, O (U+3442, U+3444, U+3446, U+3448, U+344a) + static const char *const utf8string = "\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a"; + static const char *const eucjpstring = "\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa"; + static const char *const sjisstring = "\x82\xa0\x82\xa2\x82\xa4\x82\xa6\x82\xa8"; + + QList<stringpair> japanesestrings; + japanesestrings << stringpair("UTF-8", utf8string) + << stringpair("EUC-JP", eucjpstring) + << stringpair("Shift_JIS", sjisstring); + + QTest::newRow("Japanese") << japanesestrings << 0; + +} + QTEST_APPLESS_MAIN(tst_QGlobal) #include "tst_qglobal.moc" diff --git a/tests/auto/corelib/global/qhooks/qhooks.pro b/tests/auto/corelib/global/qhooks/qhooks.pro new file mode 100644 index 0000000000..f886e7d49a --- /dev/null +++ b/tests/auto/corelib/global/qhooks/qhooks.pro @@ -0,0 +1,4 @@ +CONFIG += testcase parallel_test +TARGET = tst_qhooks +QT = core-private testlib +SOURCES = tst_qhooks.cpp diff --git a/tests/auto/corelib/global/qhooks/tst_qhooks.cpp b/tests/auto/corelib/global/qhooks/tst_qhooks.cpp new file mode 100644 index 0000000000..817c0b8173 --- /dev/null +++ b/tests/auto/corelib/global/qhooks/tst_qhooks.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Volker Krause <volker.krause@kdab.com> +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> +#include <QtCore/private/qhooks_p.h> + +class tst_QHooks: public QObject +{ + Q_OBJECT + +private slots: + void testVersion(); + void testAddRemoveObject(); +}; + +void tst_QHooks::testVersion() +{ + QVERIFY(qtHookData[QHooks::HookDataVersion] >= 1); + QCOMPARE(qtHookData[QHooks::HookDataSize], (quintptr)QHooks::LastHookIndex); + QCOMPARE(qtHookData[QHooks::QtVersion], (quintptr)QT_VERSION); +} + +static int objectCount = 0; + +static void objectAddHook(QObject*) +{ + ++objectCount; +} + +static void objectRemoveHook(QObject*) +{ + --objectCount; +} + +void tst_QHooks::testAddRemoveObject() +{ + QCOMPARE(qtHookData[QHooks::AddQObject], (quintptr)0); + QCOMPARE(qtHookData[QHooks::RemoveQObject], (quintptr)0); + + qtHookData[QHooks::AddQObject] = (quintptr)&objectAddHook; + qtHookData[QHooks::RemoveQObject] = (quintptr)&objectRemoveHook; + + QCOMPARE(objectCount, 0); + QObject *obj = new QObject; + QVERIFY(objectCount > 0); + delete obj; + QCOMPARE(objectCount, 0); +} + +QTEST_APPLESS_MAIN(tst_QHooks) +#include "tst_qhooks.moc" diff --git a/tests/auto/corelib/global/qlogging/app/app.pro b/tests/auto/corelib/global/qlogging/app/app.pro index 24ac571bac..b11e792a4c 100644 --- a/tests/auto/corelib/global/qlogging/app/app.pro +++ b/tests/auto/corelib/global/qlogging/app/app.pro @@ -10,3 +10,6 @@ CONFIG += console SOURCES += main.cpp DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 + +gcc:!mingw: QMAKE_LFLAGS += -rdynamic + diff --git a/tests/auto/corelib/global/qlogging/app/main.cpp b/tests/auto/corelib/global/qlogging/app/main.cpp index 621059caad..e190a48873 100644 --- a/tests/auto/corelib/global/qlogging/app/main.cpp +++ b/tests/auto/corelib/global/qlogging/app/main.cpp @@ -42,11 +42,37 @@ #include <QCoreApplication> #include <QLoggingCategory> +#ifdef Q_CC_GNU +#define NEVER_INLINE __attribute__((__noinline__)) +#else +#define NEVER_INLINE +#endif + struct T { T() { qDebug("static constructor"); } ~T() { qDebug("static destructor"); } } t; +class MyClass : public QObject +{ + Q_OBJECT +public slots: + virtual NEVER_INLINE QString mySlot1(); +private: + virtual NEVER_INLINE void myFunction(int a); +}; + +QString MyClass::mySlot1() +{ + myFunction(34); + return QString(); +} + +void MyClass::myFunction(int a) +{ + qDebug() << "from_a_function" << a; +} + int main(int argc, char **argv) { QCoreApplication app(argc, argv); @@ -65,5 +91,10 @@ int main(int argc, char **argv) qDebug("qDebug2"); + MyClass cl; + QMetaObject::invokeMethod(&cl, "mySlot1"); + return 0; } + +#include "main.moc" diff --git a/tests/auto/corelib/global/qlogging/tst_qlogging.cpp b/tests/auto/corelib/global/qlogging/tst_qlogging.cpp index 21e07630e2..1df5404995 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 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,12 @@ private slots: void cleanupFuncinfo(); #endif + void qMessagePattern_data(); void qMessagePattern(); - void qMessagePatternIf(); + void setMessagePattern(); + + void formatLogMessage_data(); + void formatLogMessage(); private: QString m_appDir; @@ -641,11 +646,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 +742,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 +753,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 +792,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 +806,63 @@ 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")); +Q_DECLARE_METATYPE(QtMsgType) - // - // 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(); +void tst_qmessagehandler::formatLogMessage_data() +{ + QTest::addColumn<QString>("pattern"); + QTest::addColumn<QString>("result"); + + QTest::addColumn<QtMsgType>("type"); + QTest::addColumn<QByteArray>("file"); + QTest::addColumn<int>("line"); + QTest::addColumn<QByteArray>("function"); + QTest::addColumn<QByteArray>("category"); + QTest::addColumn<QString>("message"); + +#define BA QByteArrayLiteral + + QTest::newRow("basic") << "%{type} %{file} %{line} %{function} %{message}" + << "debug main.cpp 1 func msg\n" + << QtDebugMsg << BA("main.cpp") << 1 << BA("func") << BA("") << "msg"; + + // test the if conditions + QString format = "[%{if-debug}D%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{if-category}%{category}: %{endif}%{message}"; + QTest::newRow("if-debug") + << format << "[D] msg\n" + << QtDebugMsg << BA("") << 0 << BA("func") << QByteArray() << "msg"; + QTest::newRow("if_warning") + << format << "[W] msg\n" + << QtWarningMsg << BA("") << 0 << BA("func") << QByteArray() << "msg"; + QTest::newRow("if_critical") + << format << "[C] msg\n" + << QtCriticalMsg << BA("") << 0 << BA("func") << QByteArray() << "msg"; + QTest::newRow("if_fatal") + << format << "[F] msg\n" + << QtFatalMsg << BA("") << 0 << BA("func") << QByteArray() << "msg"; + QTest::newRow("if_cat") + << format << "[F] cat: msg\n" + << QtFatalMsg << BA("") << 0 << BA("func") << BA("cat") << "msg"; +} - 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 +void tst_qmessagehandler::formatLogMessage() +{ + QFETCH(QString, pattern); + QFETCH(QString, result); + + QFETCH(QtMsgType, type); + QFETCH(QByteArray, file); + QFETCH(int, line); + QFETCH(QByteArray, function); + QFETCH(QByteArray, category); + QFETCH(QString, message); + + qSetMessagePattern(pattern); + QMessageLogContext ctxt(file, line, function, category.isEmpty() ? 0 : category.data()); + QString r = qFormatLogMessage(type, ctxt, message); + QCOMPARE(r, result); } + QTEST_MAIN(tst_qmessagehandler) #include "tst_qlogging.moc" |