tests/auto/qdebug/tst_qdebug.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtCore/QtCore>
       
    44 #include <QtCore/QtDebug>
       
    45 #include <QtTest/QtTest>
       
    46 
       
    47 class tst_QDebug: public QObject
       
    48 {
       
    49     Q_OBJECT
       
    50 private slots:
       
    51     void assignment() const;
       
    52     void warningWithoutDebug() const;
       
    53     void criticalWithoutDebug() const;
       
    54     void debugWithQBool() const;
       
    55     void veryLongWarningMessage() const;
       
    56     void qDebugQStringRef() const;
       
    57 };
       
    58 
       
    59 void tst_QDebug::assignment() const
       
    60 {
       
    61     QDebug debug1(QtDebugMsg);
       
    62     QDebug debug2(QtWarningMsg);
       
    63 
       
    64     QTest::ignoreMessage(QtDebugMsg, "foo ");
       
    65     QTest::ignoreMessage(QtWarningMsg, "bar 1 2 ");
       
    66 
       
    67     debug1 << "foo";
       
    68     debug2 << "bar";
       
    69     debug1 = debug2;
       
    70     debug1 << "1";
       
    71     debug2 << "2";
       
    72 }
       
    73 
       
    74 static QtMsgType s_msgType;
       
    75 static QByteArray s_msg;
       
    76 
       
    77 static void myMessageHandler(QtMsgType type, const char *msg)
       
    78 {
       
    79     s_msg = msg;
       
    80     s_msgType = type;
       
    81 }
       
    82 
       
    83 /*! \internal
       
    84   The qWarning() stream should be usable even if QT_NO_DEBUG is defined.
       
    85  */
       
    86 void tst_QDebug::warningWithoutDebug() const
       
    87 {
       
    88     qInstallMsgHandler(myMessageHandler);
       
    89     { qWarning() << "A qWarning() message"; }
       
    90     QCOMPARE(s_msgType, QtWarningMsg);
       
    91     QCOMPARE(QString::fromLatin1(s_msg.data()), QString::fromLatin1("A qWarning() message "));
       
    92     qInstallMsgHandler(0);
       
    93 }
       
    94 
       
    95 /*! \internal
       
    96   The qCritical() stream should be usable even if QT_NO_DEBUG is defined.
       
    97  */
       
    98 void tst_QDebug::criticalWithoutDebug() const
       
    99 {
       
   100     qInstallMsgHandler(myMessageHandler);
       
   101     { qCritical() << "A qCritical() message"; }
       
   102     QCOMPARE(s_msgType, QtCriticalMsg);
       
   103     QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("A qCritical() message "));
       
   104     qInstallMsgHandler(0);
       
   105 }
       
   106 
       
   107 void tst_QDebug::debugWithQBool() const
       
   108 {
       
   109     qInstallMsgHandler(myMessageHandler);
       
   110     { qDebug() << QBool(false) << QBool(true); }
       
   111     QCOMPARE(s_msgType, QtDebugMsg);
       
   112     QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("false true "));
       
   113     qInstallMsgHandler(0);
       
   114 }
       
   115 
       
   116 void tst_QDebug::veryLongWarningMessage() const
       
   117 {
       
   118     qInstallMsgHandler(myMessageHandler);
       
   119     QString test;
       
   120     {
       
   121         QString part("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\n");
       
   122         for (int i = 0; i < 1000; ++i)
       
   123             test.append(part);
       
   124         qWarning("Test output:\n%s\nend", qPrintable(test));
       
   125     }
       
   126     QCOMPARE(s_msgType, QtWarningMsg);
       
   127     QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("Test output:\n")+test+QString::fromLatin1("\nend"));
       
   128     qInstallMsgHandler(0);
       
   129 }
       
   130 
       
   131 void tst_QDebug::qDebugQStringRef() const
       
   132 {
       
   133     /* Use a basic string. */
       
   134     {
       
   135         const QString in(QLatin1String("input"));
       
   136         const QStringRef inRef(&in);
       
   137 
       
   138         qInstallMsgHandler(myMessageHandler);
       
   139         { qDebug() << inRef; }
       
   140         QCOMPARE(s_msgType, QtDebugMsg);
       
   141         QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("\"input\" "));
       
   142         qInstallMsgHandler(0);
       
   143     }
       
   144 
       
   145     /* Use a null QStringRef. */
       
   146     {
       
   147         const QStringRef inRef;
       
   148 
       
   149         qInstallMsgHandler(myMessageHandler);
       
   150         { qDebug() << inRef; }
       
   151         QCOMPARE(s_msgType, QtDebugMsg);
       
   152         QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("\"\" "));
       
   153         qInstallMsgHandler(0);
       
   154     }
       
   155 }
       
   156 
       
   157 QTEST_MAIN(tst_QDebug);
       
   158 #include "tst_qdebug.moc"