testlib: Replace manual logger linked list with QVector
Removes useless indirection and builds on existing primitives in Qt. Change-Id: I9fe50b21f5f77fc02566d5f5ff04c3e94c830e81 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
parent
f5cbd61f92
commit
83864b315f
@ -60,6 +60,7 @@
|
|||||||
#include <QtCore/qbytearray.h>
|
#include <QtCore/qbytearray.h>
|
||||||
#include <QtCore/QElapsedTimer>
|
#include <QtCore/QElapsedTimer>
|
||||||
#include <QtCore/QVariant>
|
#include <QtCore/QVariant>
|
||||||
|
#include <QtCore/qvector.h>
|
||||||
#if QT_CONFIG(regularexpression)
|
#if QT_CONFIG(regularexpression)
|
||||||
#include <QtCore/QRegularExpression>
|
#include <QtCore/QRegularExpression>
|
||||||
#endif
|
#endif
|
||||||
@ -93,6 +94,8 @@ static void saveCoverageTool(const char * appname, bool testfailed, bool install
|
|||||||
static QElapsedTimer elapsedFunctionTime;
|
static QElapsedTimer elapsedFunctionTime;
|
||||||
static QElapsedTimer elapsedTotalTime;
|
static QElapsedTimer elapsedTotalTime;
|
||||||
|
|
||||||
|
#define FOREACH_TEST_LOGGER for (QAbstractTestLogger *logger : QTest::loggers)
|
||||||
|
|
||||||
namespace QTest {
|
namespace QTest {
|
||||||
|
|
||||||
int fails = 0;
|
int fails = 0;
|
||||||
@ -160,109 +163,7 @@ namespace QTest {
|
|||||||
|
|
||||||
static IgnoreResultList *ignoreResultList = 0;
|
static IgnoreResultList *ignoreResultList = 0;
|
||||||
|
|
||||||
struct LoggerList
|
static QVector<QAbstractTestLogger*> loggers;
|
||||||
{
|
|
||||||
QAbstractTestLogger *logger;
|
|
||||||
LoggerList *next;
|
|
||||||
};
|
|
||||||
|
|
||||||
class TestLoggers
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
static void addLogger(QAbstractTestLogger *logger)
|
|
||||||
{
|
|
||||||
LoggerList *l = new LoggerList;
|
|
||||||
l->logger = logger;
|
|
||||||
l->next = loggers;
|
|
||||||
loggers = l;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void destroyLoggers()
|
|
||||||
{
|
|
||||||
while (loggers) {
|
|
||||||
LoggerList *l = loggers;
|
|
||||||
loggers = loggers->next;
|
|
||||||
delete l->logger;
|
|
||||||
delete l;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#define FOREACH_LOGGER(operation) \
|
|
||||||
LoggerList *l = loggers; \
|
|
||||||
while (l) { \
|
|
||||||
QAbstractTestLogger *logger = l->logger; \
|
|
||||||
Q_UNUSED(logger); \
|
|
||||||
operation; \
|
|
||||||
l = l->next; \
|
|
||||||
}
|
|
||||||
|
|
||||||
static void startLogging()
|
|
||||||
{
|
|
||||||
FOREACH_LOGGER(logger->startLogging());
|
|
||||||
}
|
|
||||||
|
|
||||||
static void stopLogging()
|
|
||||||
{
|
|
||||||
FOREACH_LOGGER(logger->stopLogging());
|
|
||||||
}
|
|
||||||
|
|
||||||
static void enterTestFunction(const char *function)
|
|
||||||
{
|
|
||||||
FOREACH_LOGGER(logger->enterTestFunction(function));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void leaveTestFunction()
|
|
||||||
{
|
|
||||||
FOREACH_LOGGER(logger->leaveTestFunction());
|
|
||||||
}
|
|
||||||
|
|
||||||
static void enterTestData(QTestData *data)
|
|
||||||
{
|
|
||||||
FOREACH_LOGGER(logger->enterTestData(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void addIncident(QAbstractTestLogger::IncidentTypes type, const char *description,
|
|
||||||
const char *file = 0, int line = 0)
|
|
||||||
{
|
|
||||||
FOREACH_LOGGER(logger->addIncident(type, description, file, line));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void addBenchmarkResult(const QBenchmarkResult &result)
|
|
||||||
{
|
|
||||||
FOREACH_LOGGER(logger->addBenchmarkResult(result));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void addMessage(QtMsgType type, const QMessageLogContext &context,
|
|
||||||
const QString &message)
|
|
||||||
{
|
|
||||||
FOREACH_LOGGER(logger->addMessage(type, context, message));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void addMessage(QAbstractTestLogger::MessageTypes type, const QString &message,
|
|
||||||
const char *file = 0, int line = 0)
|
|
||||||
{
|
|
||||||
FOREACH_LOGGER(logger->addMessage(type, message, file, line));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void outputString(const char *msg)
|
|
||||||
{
|
|
||||||
FOREACH_LOGGER(logger->outputString(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
static int loggerCount()
|
|
||||||
{
|
|
||||||
int count = 0;
|
|
||||||
FOREACH_LOGGER(++count);
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
static LoggerList *loggers;
|
|
||||||
};
|
|
||||||
|
|
||||||
#undef FOREACH_LOGGER
|
|
||||||
|
|
||||||
LoggerList *TestLoggers::loggers = 0;
|
|
||||||
static bool loggerUsingStdout = false;
|
static bool loggerUsingStdout = false;
|
||||||
|
|
||||||
static int verbosity = 0;
|
static int verbosity = 0;
|
||||||
@ -301,10 +202,10 @@ namespace QTest {
|
|||||||
{
|
{
|
||||||
static QBasicAtomicInt counter = Q_BASIC_ATOMIC_INITIALIZER(QTest::maxWarnings);
|
static QBasicAtomicInt counter = Q_BASIC_ATOMIC_INITIALIZER(QTest::maxWarnings);
|
||||||
|
|
||||||
if (QTest::TestLoggers::loggerCount() == 0) {
|
if (QTestLog::loggerCount() == 0) {
|
||||||
// if this goes wrong, something is seriously broken.
|
// if this goes wrong, something is seriously broken.
|
||||||
qInstallMessageHandler(oldMessageHandler);
|
qInstallMessageHandler(oldMessageHandler);
|
||||||
QTEST_ASSERT(QTest::TestLoggers::loggerCount() != 0);
|
QTEST_ASSERT(QTestLog::loggerCount() != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (handleIgnoredMessage(type, message)) {
|
if (handleIgnoredMessage(type, message)) {
|
||||||
@ -317,13 +218,16 @@ namespace QTest {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
if (!counter.deref()) {
|
if (!counter.deref()) {
|
||||||
QTest::TestLoggers::addMessage(QAbstractTestLogger::QSystem,
|
FOREACH_TEST_LOGGER {
|
||||||
|
logger->addMessage(QAbstractTestLogger::QSystem,
|
||||||
QStringLiteral("Maximum amount of warnings exceeded. Use -maxwarnings to override."));
|
QStringLiteral("Maximum amount of warnings exceeded. Use -maxwarnings to override."));
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QTest::TestLoggers::addMessage(type, context, message);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->addMessage(type, context, message);
|
||||||
|
|
||||||
if (type == QtFatalMsg) {
|
if (type == QtFatalMsg) {
|
||||||
/* Right now, we're inside the custom message handler and we're
|
/* Right now, we're inside the custom message handler and we're
|
||||||
@ -346,13 +250,16 @@ void QTestLog::enterTestFunction(const char* function)
|
|||||||
|
|
||||||
QTEST_ASSERT(function);
|
QTEST_ASSERT(function);
|
||||||
|
|
||||||
QTest::TestLoggers::enterTestFunction(function);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->enterTestFunction(function);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::enterTestData(QTestData *data)
|
void QTestLog::enterTestData(QTestData *data)
|
||||||
{
|
{
|
||||||
QTEST_ASSERT(data);
|
QTEST_ASSERT(data);
|
||||||
QTest::TestLoggers::enterTestData(data);
|
|
||||||
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->enterTestData(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
int QTestLog::unhandledIgnoreMessages()
|
int QTestLog::unhandledIgnoreMessages()
|
||||||
@ -371,7 +278,8 @@ void QTestLog::leaveTestFunction()
|
|||||||
if (printAvailableTags)
|
if (printAvailableTags)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QTest::TestLoggers::leaveTestFunction();
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->leaveTestFunction();
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::printUnhandledIgnoreMessages()
|
void QTestLog::printUnhandledIgnoreMessages()
|
||||||
@ -386,7 +294,8 @@ void QTestLog::printUnhandledIgnoreMessages()
|
|||||||
message = QStringLiteral("Did not receive any message matching: \"") + list->pattern.toRegularExpression().pattern() + QLatin1Char('"');
|
message = QStringLiteral("Did not receive any message matching: \"") + list->pattern.toRegularExpression().pattern() + QLatin1Char('"');
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
QTest::TestLoggers::addMessage(QAbstractTestLogger::Info, message);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->addMessage(QAbstractTestLogger::Info, message);
|
||||||
|
|
||||||
list = list->next;
|
list = list->next;
|
||||||
}
|
}
|
||||||
@ -406,7 +315,8 @@ void QTestLog::addPass(const char *msg)
|
|||||||
|
|
||||||
++QTest::passes;
|
++QTest::passes;
|
||||||
|
|
||||||
QTest::TestLoggers::addIncident(QAbstractTestLogger::Pass, msg);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->addIncident(QAbstractTestLogger::Pass, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::addFail(const char *msg, const char *file, int line)
|
void QTestLog::addFail(const char *msg, const char *file, int line)
|
||||||
@ -415,7 +325,8 @@ void QTestLog::addFail(const char *msg, const char *file, int line)
|
|||||||
|
|
||||||
++QTest::fails;
|
++QTest::fails;
|
||||||
|
|
||||||
QTest::TestLoggers::addIncident(QAbstractTestLogger::Fail, msg, file, line);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->addIncident(QAbstractTestLogger::Fail, msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::addXFail(const char *msg, const char *file, int line)
|
void QTestLog::addXFail(const char *msg, const char *file, int line)
|
||||||
@ -423,7 +334,8 @@ void QTestLog::addXFail(const char *msg, const char *file, int line)
|
|||||||
QTEST_ASSERT(msg);
|
QTEST_ASSERT(msg);
|
||||||
QTEST_ASSERT(file);
|
QTEST_ASSERT(file);
|
||||||
|
|
||||||
QTest::TestLoggers::addIncident(QAbstractTestLogger::XFail, msg, file, line);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->addIncident(QAbstractTestLogger::XFail, msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::addXPass(const char *msg, const char *file, int line)
|
void QTestLog::addXPass(const char *msg, const char *file, int line)
|
||||||
@ -433,7 +345,8 @@ void QTestLog::addXPass(const char *msg, const char *file, int line)
|
|||||||
|
|
||||||
++QTest::fails;
|
++QTest::fails;
|
||||||
|
|
||||||
QTest::TestLoggers::addIncident(QAbstractTestLogger::XPass, msg, file, line);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->addIncident(QAbstractTestLogger::XPass, msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::addBPass(const char *msg)
|
void QTestLog::addBPass(const char *msg)
|
||||||
@ -442,7 +355,8 @@ void QTestLog::addBPass(const char *msg)
|
|||||||
|
|
||||||
++QTest::blacklists;
|
++QTest::blacklists;
|
||||||
|
|
||||||
QTest::TestLoggers::addIncident(QAbstractTestLogger::BlacklistedPass, msg);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->addIncident(QAbstractTestLogger::BlacklistedPass, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::addBFail(const char *msg, const char *file, int line)
|
void QTestLog::addBFail(const char *msg, const char *file, int line)
|
||||||
@ -452,7 +366,8 @@ void QTestLog::addBFail(const char *msg, const char *file, int line)
|
|||||||
|
|
||||||
++QTest::blacklists;
|
++QTest::blacklists;
|
||||||
|
|
||||||
QTest::TestLoggers::addIncident(QAbstractTestLogger::BlacklistedFail, msg, file, line);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->addIncident(QAbstractTestLogger::BlacklistedFail, msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::addBXPass(const char *msg, const char *file, int line)
|
void QTestLog::addBXPass(const char *msg, const char *file, int line)
|
||||||
@ -462,7 +377,8 @@ void QTestLog::addBXPass(const char *msg, const char *file, int line)
|
|||||||
|
|
||||||
++QTest::blacklists;
|
++QTest::blacklists;
|
||||||
|
|
||||||
QTest::TestLoggers::addIncident(QAbstractTestLogger::BlacklistedXPass, msg, file, line);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->addIncident(QAbstractTestLogger::BlacklistedXPass, msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::addBXFail(const char *msg, const char *file, int line)
|
void QTestLog::addBXFail(const char *msg, const char *file, int line)
|
||||||
@ -472,7 +388,8 @@ void QTestLog::addBXFail(const char *msg, const char *file, int line)
|
|||||||
|
|
||||||
++QTest::blacklists;
|
++QTest::blacklists;
|
||||||
|
|
||||||
QTest::TestLoggers::addIncident(QAbstractTestLogger::BlacklistedXFail, msg, file, line);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->addIncident(QAbstractTestLogger::BlacklistedXFail, msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::addSkip(const char *msg, const char *file, int line)
|
void QTestLog::addSkip(const char *msg, const char *file, int line)
|
||||||
@ -482,27 +399,33 @@ void QTestLog::addSkip(const char *msg, const char *file, int line)
|
|||||||
|
|
||||||
++QTest::skips;
|
++QTest::skips;
|
||||||
|
|
||||||
QTest::TestLoggers::addMessage(QAbstractTestLogger::Skip, QString::fromUtf8(msg), file, line);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->addMessage(QAbstractTestLogger::Skip, QString::fromUtf8(msg), file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::addBenchmarkResult(const QBenchmarkResult &result)
|
void QTestLog::addBenchmarkResult(const QBenchmarkResult &result)
|
||||||
{
|
{
|
||||||
QTest::TestLoggers::addBenchmarkResult(result);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->addBenchmarkResult(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::startLogging()
|
void QTestLog::startLogging()
|
||||||
{
|
{
|
||||||
elapsedTotalTime.start();
|
elapsedTotalTime.start();
|
||||||
elapsedFunctionTime.start();
|
elapsedFunctionTime.start();
|
||||||
QTest::TestLoggers::startLogging();
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->startLogging();
|
||||||
QTest::oldMessageHandler = qInstallMessageHandler(QTest::messageHandler);
|
QTest::oldMessageHandler = qInstallMessageHandler(QTest::messageHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::stopLogging()
|
void QTestLog::stopLogging()
|
||||||
{
|
{
|
||||||
qInstallMessageHandler(QTest::oldMessageHandler);
|
qInstallMessageHandler(QTest::oldMessageHandler);
|
||||||
QTest::TestLoggers::stopLogging();
|
FOREACH_TEST_LOGGER {
|
||||||
QTest::TestLoggers::destroyLoggers();
|
logger->stopLogging();
|
||||||
|
delete logger;
|
||||||
|
}
|
||||||
|
QTest::loggers.clear();
|
||||||
QTest::loggerUsingStdout = false;
|
QTest::loggerUsingStdout = false;
|
||||||
saveCoverageTool(QTestResult::currentAppName(), failCount() != 0, QTestLog::installedTestCoverage());
|
saveCoverageTool(QTestResult::currentAppName(), failCount() != 0, QTestLog::installedTestCoverage());
|
||||||
}
|
}
|
||||||
@ -553,12 +476,12 @@ void QTestLog::addLogger(LogMode mode, const char *filename)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
QTEST_ASSERT(logger);
|
QTEST_ASSERT(logger);
|
||||||
QTest::TestLoggers::addLogger(logger);
|
QTest::loggers.append(logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
int QTestLog::loggerCount()
|
int QTestLog::loggerCount()
|
||||||
{
|
{
|
||||||
return QTest::TestLoggers::loggerCount();
|
return QTest::loggers.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QTestLog::loggerUsingStdout()
|
bool QTestLog::loggerUsingStdout()
|
||||||
@ -570,15 +493,16 @@ void QTestLog::warn(const char *msg, const char *file, int line)
|
|||||||
{
|
{
|
||||||
QTEST_ASSERT(msg);
|
QTEST_ASSERT(msg);
|
||||||
|
|
||||||
if (QTest::TestLoggers::loggerCount() > 0)
|
FOREACH_TEST_LOGGER
|
||||||
QTest::TestLoggers::addMessage(QAbstractTestLogger::Warn, QString::fromUtf8(msg), file, line);
|
logger->addMessage(QAbstractTestLogger::Warn, QString::fromUtf8(msg), file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::info(const char *msg, const char *file, int line)
|
void QTestLog::info(const char *msg, const char *file, int line)
|
||||||
{
|
{
|
||||||
QTEST_ASSERT(msg);
|
QTEST_ASSERT(msg);
|
||||||
|
|
||||||
QTest::TestLoggers::addMessage(QAbstractTestLogger::Info, QString::fromUtf8(msg), file, line);
|
FOREACH_TEST_LOGGER
|
||||||
|
logger->addMessage(QAbstractTestLogger::Info, QString::fromUtf8(msg), file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QTestLog::setVerboseLevel(int level)
|
void QTestLog::setVerboseLevel(int level)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user