Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qabstracttestlogger_p.h
Go to the documentation of this file.
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
25QT_BEGIN_NAMESPACE
26
27class QBenchmarkResult;
28class QTestData;
29
30class Q_TESTLIB_EXPORT QAbstractTestLogger
31{
32 Q_DISABLE_COPY_MOVE(QAbstractTestLogger)
33public:
34 enum IncidentTypes {
35 Skip,
36 Pass,
37 XFail,
38 Fail,
39 XPass,
40 BlacklistedPass,
41 BlacklistedFail,
42 BlacklistedXPass,
43 BlacklistedXFail
44 };
45
52 // testlib's internal messages:
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 virtual bool isRepeatSupported() const;
80
81 bool isLoggingToStdout() const;
82
83 void outputString(const char *msg);
84
85protected:
86 void filterUnprintable(char *str) const;
87 FILE *stream;
88};
89
91{
92 enum { InitialSize = 512 };
93
94 inline QTestCharBuffer() : buf(staticBuf)
95 {
96 staticBuf[0] = '\0';
97 }
98
100
102 {
103 if (buf != staticBuf)
104 free(buf);
105 }
106
107 inline char *data()
108 {
109 return buf;
110 }
111
112 inline char **buffer()
113 {
114 return &buf;
115 }
116
117 inline const char* constData() const
118 {
119 return buf;
120 }
121
122 inline int size() const
123 {
124 return _size;
125 }
126
127 bool reset(int newSize, bool copy = false)
128 {
129 char *newBuf = nullptr;
130 if (buf == staticBuf) {
131 // if we point to our internal buffer, we need to malloc first
132 newBuf = reinterpret_cast<char *>(malloc(newSize));
133 if (copy && newBuf)
135 } else {
136 // if we already malloc'ed, just realloc
137 newBuf = reinterpret_cast<char *>(realloc(buf, newSize));
138 }
139
140 // if the allocation went wrong (newBuf == 0), we leave the object as is
141 if (!newBuf)
142 return false;
143
144 _size = newSize;
145 buf = newBuf;
146 return true;
147 }
148
149 bool resize(int newSize) {
150 return newSize <= _size || reset(newSize, true);
151 }
152
153 void clear() { buf[0] = '\0'; }
154 bool isEmpty() { return buf[0] == '\0'; }
155
156private:
157 int _size = InitialSize;
158 char* buf;
159 char staticBuf[InitialSize];
160};
161
162namespace QTest
163{
164 int qt_asprintf(QTestCharBuffer *buf, const char *format, ...);
165}
166
167namespace QTestPrivate
168{
169 enum IdentifierPart { TestObject = 0x1, TestFunction = 0x2, TestDataTag = 0x4, AllParts = 0xFFFF };
170 void Q_TESTLIB_EXPORT generateTestIdentifier(QTestCharBuffer *identifier, int parts = AllParts);
171 bool appendCharBuffer(QTestCharBuffer *accumulator, const QTestCharBuffer &more);
172}
173
174QT_END_NAMESPACE
175
176#endif
Base class for test loggers.
void outputString(const char *msg)
Convenience method to write msg to the output stream.
bool isLoggingToStdout() const
Returns true if the output stream is standard output.
virtual void addIncident(IncidentTypes type, const char *description, const char *file=nullptr, int line=0)=0
This virtual method is called when an event occurs that relates to the resolution of the test.
virtual void addBenchmarkResult(const QBenchmarkResult &result)=0
This virtual method is called after a benchmark has been run enough times to produce usable data.
QAbstractTestLogger(const char *filename)
Constructs the base-class parts of the logger.
virtual void startLogging()
Called before the start of a test run.
virtual void addMessage(QtMsgType, const QMessageLogContext &, const QString &)
This is an overloaded member function, provided for convenience. It differs from the above function o...
MessageTypes
The members whose names begin with Q describe messages that originate in calls, by the test or code u...
virtual void addMessage(MessageTypes type, const QString &message, const char *file=nullptr, int line=0)=0
This is an overloaded member function, provided for convenience. It differs from the above function o...
virtual bool isRepeatSupported() const
Returns true if the logger supports repeated test runs.
virtual void addBenchmarkResults(const QList< QBenchmarkResult > &result)
virtual void leaveTestFunction()=0
This virtual method is called after a test function has completed, to match \l enterTestFunction().
virtual void stopLogging()
Called after the end of a test run.
virtual void enterTestData(QTestData *)
This virtual method is called before and after each call to a test function.
virtual void enterTestFunction(const char *function)=0
This virtual method is called before each test function is invoked.
QCsvBenchmarkLogger(const char *filename)
void addIncident(IncidentTypes type, const char *description, const char *file=nullptr, int line=0) override
This virtual method is called when an event occurs that relates to the resolution of the test.
void enterTestFunction(const char *function) override
This virtual method is called before each test function is invoked.
void addBenchmarkResult(const QBenchmarkResult &result) override
This virtual method is called after a benchmark has been run enough times to produce usable data.
void stopLogging() override
Called after the end of a test run.
void leaveTestFunction() override
This virtual method is called after a test function has completed, to match \l enterTestFunction().
void addMessage(MessageTypes type, const QString &message, const char *file=nullptr, int line=0) override
This is an overloaded member function, provided for convenience. It differs from the above function o...
void startLogging() override
Called before the start of a test run.