Debian Codegraph @

tests/auto/core5/io/qdebug/tst_qdebug.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // Copyright (C) 2016 Intel Corporation.
3 // Copyright (C) 2020 The Qt Company Ltd.
4 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
5
6 #include <QtCore/QtDebug>
7 #include <QtTest/QtTest>
8 #include <qstringref.h>
9
10 class tst_QDebug: public QObject
11 {
12 Q_OBJECT
13
14 private slots:
15 void qDebugQStringRef() const;
16 };
17
18 static QtMsgType s_msgType;
19 static QString s_msg;
20 static QByteArray s_file;
21 static int s_line;
22 static QByteArray s_function;
23
24 static void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
25 {
26 s_msg = msg;
27 s_msgType = type;
28 s_file = context.file;
29 s_line = context.line;
30 s_function = context.function;
31 }
32
33 // Helper class to ensure that the testlib message handler gets
34 // restored at the end of each test function, even if the test
35 // fails or throws an exception.
36 class MessageHandlerSetter
37 {
38 public:
39 MessageHandlerSetter(QtMessageHandler newMessageHandler)
40 : oldMessageHandler(qInstallMessageHandler(newMessageHandler))
41 { }
42
43 ~MessageHandlerSetter()
44 {
45 qInstallMessageHandler(oldMessageHandler);
46 }
47
48 private:
49 QtMessageHandler oldMessageHandler;
50 };
51
52 void tst_QDebug::qDebugQStringRef() const
53 {
54 /* Use a basic string. */
55 {
56 QString file, function;
57 int line = 0;
58 const QString in(QLatin1String("input"));
59 const QStringRef inRef(&in);
60
61 MessageHandlerSetter mhs(myMessageHandler);
62 { qDebug() << inRef; }
63 #ifndef QT_NO_MESSAGELOGCONTEXT
64 file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
65 #endif
66 QCOMPARE(s_msgType, QtDebugMsg);
67 QCOMPARE(s_msg, QString::fromLatin1("\"input\""));
68 QCOMPARE(QString::fromLatin1(s_file), file);
69 QCOMPARE(s_line, line);
70 QCOMPARE(QString::fromLatin1(s_function), function);
71 }
72
73 /* Use a null QStringRef. */
74 {
75 QString file, function;
76 int line = 0;
77
78 const QStringRef inRef;
79
80 MessageHandlerSetter mhs(myMessageHandler);
81 { qDebug() << inRef; }
82 #ifndef QT_NO_MESSAGELOGCONTEXT
83 file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
84 #endif
85 QCOMPARE(s_msgType, QtDebugMsg);
86 QCOMPARE(s_msg, QString::fromLatin1("\"\""));
87 QCOMPARE(QString::fromLatin1(s_file), file);
88 QCOMPARE(s_line, line);
89 QCOMPARE(QString::fromLatin1(s_function), function);
90 }
91 }
92
93 QTEST_MAIN(tst_QDebug);
94 #include "tst_qdebug.moc"