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 <qglobal.h>
55#include <stdio.h>
56#include <stdlib.h>
57
58QT_BEGIN_NAMESPACE
59
60class QBenchmarkResult;
61class QTestData;
62
63class QAbstractTestLogger
64{
65public:
66 enum IncidentTypes {
67 Pass,
68 XFail,
69 Fail,
70 XPass,
71 BlacklistedPass,
72 BlacklistedFail,
73 BlacklistedXPass,
74 BlacklistedXFail
75 };
76
77 enum MessageTypes {
78 Warn,
79 QWarning,
80 QDebug,
81 QSystem,
82 QFatal,
83 Skip,
84 Info,
85 QInfo
86 };
87
88 QAbstractTestLogger(const char *filename);
89 virtual ~QAbstractTestLogger();
90
91 virtual void startLogging();
92 virtual void stopLogging();
93
94 virtual void enterTestFunction(const char *function) = 0;
95 virtual void leaveTestFunction() = 0;
96
97 virtual void enterTestData(QTestData *) {}
98
99 virtual void addIncident(IncidentTypes type, const char *description,
100 const char *file = nullptr, int line = 0) = 0;
101 virtual void addBenchmarkResult(const QBenchmarkResult &result) = 0;
102
103 virtual void addMessage(QtMsgType, const QMessageLogContext &,
104 const QString &);
105
106 virtual void addMessage(MessageTypes type, const QString &message,
107 const char *file = nullptr, int line = 0) = 0;
108
109 void outputString(const char *msg);
110
111protected:
112 void filterUnprintable(char *str) const;
113 FILE *stream;
114};
115
116struct QTestCharBuffer
117{
118 enum { InitialSize = 512 };
119
120 inline QTestCharBuffer() : buf(staticBuf)
121 {
122 staticBuf[0] = '\0';
123 }
124
125 inline ~QTestCharBuffer()
126 {
127 if (buf != staticBuf)
128 free(buf);
129 }
130
131 inline char *data()
132 {
133 return buf;
134 }
135
136 inline char **buffer()
137 {
138 return &buf;
139 }
140
141 inline const char* constData() const
142 {
143 return buf;
144 }
145
146 inline int size() const
147 {
148 return _size;
149 }
150
151 inline bool reset(int newSize)
152 {
153 char *newBuf = nullptr;
154 if (buf == staticBuf) {
155 // if we point to our internal buffer, we need to malloc first
156 newBuf = reinterpret_cast<char *>(malloc(newSize));
157 } else {
158 // if we already malloc'ed, just realloc
159 newBuf = reinterpret_cast<char *>(realloc(buf, newSize));
160 }
161
162 // if the allocation went wrong (newBuf == 0), we leave the object as is
163 if (!newBuf)
164 return false;
165
166 _size = newSize;
167 buf = newBuf;
168 return true;
169 }
170
171private:
172 int _size = InitialSize;
173 char* buf;
174 char staticBuf[InitialSize];
175};
176
177namespace QTest
178{
179 int qt_asprintf(QTestCharBuffer *buf, const char *format, ...);
180}
181
182namespace QTestPrivate
183{
184 enum IdentifierPart { TestObject = 0x1, TestFunction = 0x2, TestDataTag = 0x4, AllParts = 0xFFFF };
185 void generateTestIdentifier(QTestCharBuffer *identifier, int parts = AllParts);
186}
187
188QT_END_NAMESPACE
189
190#endif
191