1 | // Copyright (C) 2022 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QABSTRACTTESTLOGGER_P_H |
5 | #define QABSTRACTTESTLOGGER_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtTest/qttestglobal.h> |
19 | #include <QtCore/private/qglobal_p.h> |
20 | #include <QtCore/qbytearrayalgorithms.h> |
21 | |
22 | #include <stdio.h> |
23 | #include <stdlib.h> |
24 | |
25 | QT_BEGIN_NAMESPACE |
26 | |
27 | class QBenchmarkResult; |
28 | class QTestData; |
29 | |
30 | class Q_TESTLIB_EXPORT QAbstractTestLogger |
31 | { |
32 | Q_DISABLE_COPY_MOVE(QAbstractTestLogger) |
33 | public: |
34 | enum IncidentTypes { |
35 | Skip, |
36 | Pass, |
37 | XFail, |
38 | Fail, |
39 | XPass, |
40 | BlacklistedPass, |
41 | BlacklistedFail, |
42 | BlacklistedXPass, |
43 | BlacklistedXFail |
44 | }; |
45 | |
46 | enum MessageTypes { |
47 | QDebug, |
48 | QInfo, |
49 | QWarning, |
50 | QCritical, |
51 | QFatal, |
52 | // testlib's internal messages: |
53 | Info, |
54 | Warn |
55 | }; |
56 | |
57 | QAbstractTestLogger(const char *filename); |
58 | virtual ~QAbstractTestLogger(); |
59 | |
60 | virtual void startLogging(); |
61 | virtual void stopLogging(); |
62 | |
63 | virtual void enterTestFunction(const char *function) = 0; |
64 | virtual void leaveTestFunction() = 0; |
65 | |
66 | virtual void enterTestData(QTestData *) {} |
67 | |
68 | virtual void addIncident(IncidentTypes type, const char *description, |
69 | const char *file = nullptr, int line = 0) = 0; |
70 | virtual void addBenchmarkResult(const QBenchmarkResult &result) = 0; |
71 | virtual void addBenchmarkResults(const QList<QBenchmarkResult> &result); |
72 | |
73 | virtual void addMessage(QtMsgType, const QMessageLogContext &, |
74 | const QString &); |
75 | |
76 | virtual void addMessage(MessageTypes type, const QString &message, |
77 | const char *file = nullptr, int line = 0) = 0; |
78 | |
79 | bool isLoggingToStdout() const; |
80 | |
81 | void outputString(const char *msg); |
82 | |
83 | protected: |
84 | void filterUnprintable(char *str) const; |
85 | FILE *stream; |
86 | }; |
87 | |
88 | struct QTestCharBuffer |
89 | { |
90 | enum { InitialSize = 512 }; |
91 | |
92 | inline QTestCharBuffer() : buf(staticBuf) |
93 | { |
94 | staticBuf[0] = '\0'; |
95 | } |
96 | |
97 | Q_DISABLE_COPY_MOVE(QTestCharBuffer) |
98 | |
99 | inline ~QTestCharBuffer() |
100 | { |
101 | if (buf != staticBuf) |
102 | free(ptr: buf); |
103 | } |
104 | |
105 | inline char *data() |
106 | { |
107 | return buf; |
108 | } |
109 | |
110 | inline char **buffer() |
111 | { |
112 | return &buf; |
113 | } |
114 | |
115 | inline const char* constData() const |
116 | { |
117 | return buf; |
118 | } |
119 | |
120 | inline int size() const |
121 | { |
122 | return _size; |
123 | } |
124 | |
125 | bool reset(int newSize, bool copy = false) |
126 | { |
127 | char *newBuf = nullptr; |
128 | if (buf == staticBuf) { |
129 | // if we point to our internal buffer, we need to malloc first |
130 | newBuf = reinterpret_cast<char *>(malloc(size: newSize)); |
131 | if (copy && newBuf) |
132 | qstrncpy(dst: newBuf, src: buf, len: _size); |
133 | } else { |
134 | // if we already malloc'ed, just realloc |
135 | newBuf = reinterpret_cast<char *>(realloc(ptr: buf, size: newSize)); |
136 | } |
137 | |
138 | // if the allocation went wrong (newBuf == 0), we leave the object as is |
139 | if (!newBuf) |
140 | return false; |
141 | |
142 | _size = newSize; |
143 | buf = newBuf; |
144 | return true; |
145 | } |
146 | |
147 | bool resize(int newSize) { |
148 | return newSize <= _size || reset(newSize, copy: true); |
149 | } |
150 | |
151 | void clear() { buf[0] = '\0'; } |
152 | bool isEmpty() { return buf[0] == '\0'; } |
153 | |
154 | private: |
155 | int _size = InitialSize; |
156 | char* buf; |
157 | char staticBuf[InitialSize]; |
158 | }; |
159 | |
160 | namespace QTest |
161 | { |
162 | int qt_asprintf(QTestCharBuffer *buf, const char *format, ...); |
163 | } |
164 | |
165 | namespace QTestPrivate |
166 | { |
167 | enum IdentifierPart { TestObject = 0x1, TestFunction = 0x2, TestDataTag = 0x4, AllParts = 0xFFFF }; |
168 | void Q_TESTLIB_EXPORT generateTestIdentifier(QTestCharBuffer *identifier, int parts = AllParts); |
169 | bool appendCharBuffer(QTestCharBuffer *accumulator, const QTestCharBuffer &more); |
170 | } |
171 | |
172 | QT_END_NAMESPACE |
173 | |
174 | #endif |
175 | |