1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtTest module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QABSTRACTTESTLOGGER_P_H
41 #define QABSTRACTTESTLOGGER_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtTest/qttestglobal.h>
55 
56 #include <stdio.h>
57 #include <stdlib.h>
58 
59 QT_BEGIN_NAMESPACE
60 
61 class QBenchmarkResult;
62 class QTestData;
63 
64 class Q_TESTLIB_EXPORT QAbstractTestLogger
65 {
66 public:
67     enum IncidentTypes {
68         Pass,
69         XFail,
70         Fail,
71         XPass,
72         BlacklistedPass,
73         BlacklistedFail,
74         BlacklistedXPass,
75         BlacklistedXFail
76     };
77 
78     enum MessageTypes {
79         Warn,
80         QWarning,
81         QDebug,
82         QSystem,
83         QFatal,
84         Skip,
85         Info,
86         QInfo
87     };
88 
89     QAbstractTestLogger(const char *filename);
90     virtual ~QAbstractTestLogger();
91 
92     virtual void startLogging();
93     virtual void stopLogging();
94 
95     virtual void enterTestFunction(const char *function) = 0;
96     virtual void leaveTestFunction() = 0;
97 
enterTestData(QTestData *)98     virtual void enterTestData(QTestData *) {}
99 
100     virtual void addIncident(IncidentTypes type, const char *description,
101                              const char *file = nullptr, int line = 0) = 0;
102     virtual void addBenchmarkResult(const QBenchmarkResult &result) = 0;
103 
104     virtual void addMessage(QtMsgType, const QMessageLogContext &,
105                             const QString &);
106 
107     virtual void addMessage(MessageTypes type, const QString &message,
108                             const char *file = nullptr, int line = 0) = 0;
109 
110     bool isLoggingToStdout() const;
111 
112     void outputString(const char *msg);
113 
114 protected:
115     void filterUnprintable(char *str) const;
116     FILE *stream;
117 };
118 
119 struct QTestCharBuffer
120 {
121     enum { InitialSize = 512 };
122 
QTestCharBufferQTestCharBuffer123     inline QTestCharBuffer() : buf(staticBuf)
124     {
125         staticBuf[0] = '\0';
126     }
127 
~QTestCharBufferQTestCharBuffer128     inline ~QTestCharBuffer()
129     {
130         if (buf != staticBuf)
131             free(buf);
132     }
133 
dataQTestCharBuffer134     inline char *data()
135     {
136         return buf;
137     }
138 
bufferQTestCharBuffer139     inline char **buffer()
140     {
141         return &buf;
142     }
143 
constDataQTestCharBuffer144     inline const char* constData() const
145     {
146         return buf;
147     }
148 
sizeQTestCharBuffer149     inline int size() const
150     {
151         return _size;
152     }
153 
resetQTestCharBuffer154     inline bool reset(int newSize)
155     {
156         char *newBuf = nullptr;
157         if (buf == staticBuf) {
158             // if we point to our internal buffer, we need to malloc first
159             newBuf = reinterpret_cast<char *>(malloc(newSize));
160         } else {
161             // if we already malloc'ed, just realloc
162             newBuf = reinterpret_cast<char *>(realloc(buf, newSize));
163         }
164 
165         // if the allocation went wrong (newBuf == 0), we leave the object as is
166         if (!newBuf)
167             return false;
168 
169         _size = newSize;
170         buf = newBuf;
171         return true;
172     }
173 
174 private:
175     int _size = InitialSize;
176     char* buf;
177     char staticBuf[InitialSize];
178 };
179 
180 namespace QTest
181 {
182     int qt_asprintf(QTestCharBuffer *buf, const char *format, ...);
183 }
184 
185 namespace QTestPrivate
186 {
187     enum IdentifierPart { TestObject = 0x1, TestFunction = 0x2, TestDataTag = 0x4, AllParts = 0xFFFF };
188     void Q_TESTLIB_EXPORT generateTestIdentifier(QTestCharBuffer *identifier, int parts = AllParts);
189 }
190 
191 QT_END_NAMESPACE
192 
193 #endif
194