Add overload of QTest::ignoreMessage() taking a QRegularExpression.

Make it possible to match messages by a pattern.

Change-Id: I713312e86db5471755459f1ecc43e8f1ac7a95fb
Reviewed-by: Jason McDonald <macadder1@gmail.com>
This commit is contained in:
Friedemann Kleint 2013-11-12 15:03:54 +01:00 committed by The Qt Project
parent 331bc16afd
commit 943ae8bb70
9 changed files with 127 additions and 19 deletions

View File

@ -2332,6 +2332,27 @@ void QTest::ignoreMessage(QtMsgType type, const char *message)
QTestLog::ignoreMessage(type, message);
}
/*!
\overload
Ignores messages created by qDebug() or qWarning(). If the \a message
matching \a messagePattern
with the corresponding \a type is outputted, it will be removed from the
test log. If the test finished and the \a message was not outputted,
a test failure is appended to the test log.
\b {Note:} Invoking this function will only ignore one message.
If the message you want to ignore is outputted twice, you have to
call ignoreMessage() twice, too.
\since 5.3
*/
void QTest::ignoreMessage(QtMsgType type, const QRegularExpression &messagePattern)
{
QTestLog::ignoreMessage(type, messagePattern);
}
/*! \internal
*/

View File

@ -53,6 +53,7 @@
QT_BEGIN_NAMESPACE
class QRegularExpression;
#define QVERIFY(statement) \
do {\
@ -191,6 +192,7 @@ namespace QTest
const char *file, int line);
Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = 0, int line = 0);
Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message);
Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const QRegularExpression &messagePattern);
Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = 0, int line = 0, const char* builddir = 0);
Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = 0, int line = 0, const char* builddir = 0);

View File

@ -49,6 +49,8 @@
#include <QtTest/private/qxmltestlogger_p.h>
#include <QtCore/qatomic.h>
#include <QtCore/qbytearray.h>
#include <QtCore/QVariant>
#include <QtCore/QRegularExpression>
#include <stdlib.h>
#include <string.h>
@ -84,11 +86,8 @@ namespace QTest {
struct IgnoreResultList
{
inline IgnoreResultList(QtMsgType tp, const char *message)
: type(tp), next(0)
{ msg = qstrdup(message); }
inline ~IgnoreResultList()
{ delete [] msg; }
inline IgnoreResultList(QtMsgType tp, const QVariant &patternIn)
: type(tp), pattern(patternIn), next(0) {}
static inline void clearList(IgnoreResultList *&list)
{
@ -99,8 +98,29 @@ namespace QTest {
}
}
static void append(IgnoreResultList *&list, QtMsgType type, const QVariant &patternIn)
{
QTest::IgnoreResultList *item = new QTest::IgnoreResultList(type, patternIn);
if (!list) {
list = item;
return;
}
IgnoreResultList *last = list;
for ( ; last->next; last = last->next) ;
last->next = item;
}
inline bool matches(QtMsgType tp, const QString &message) const
{
return tp == type
&& (pattern.type() == QVariant::String ?
pattern.toString() == message :
pattern.toRegularExpression().match(message).hasMatch());
}
QtMsgType type;
char *msg;
QVariant pattern;
IgnoreResultList *next;
};
@ -208,10 +228,13 @@ namespace QTest {
static bool handleIgnoredMessage(QtMsgType type, const char *msg)
{
if (!ignoreResultList)
return false;
const QString message = QString::fromLocal8Bit(msg);
IgnoreResultList *last = 0;
IgnoreResultList *list = ignoreResultList;
while (list) {
if (list->type == type && strcmp(msg, list->msg) == 0) {
if (list->matches(type, message)) {
// remove the item from the list
if (last)
last->next = list->next;
@ -316,7 +339,11 @@ void QTestLog::printUnhandledIgnoreMessages()
char msg[1024];
QTest::IgnoreResultList *list = QTest::ignoreResultList;
while (list) {
qsnprintf(msg, 1024, "Did not receive message: \"%s\"", list->msg);
if (list->pattern.type() == QVariant::String) {
qsnprintf(msg, 1024, "Did not receive message: \"%s\"", qPrintable(list->pattern.toString()));
} else {
qsnprintf(msg, 1024, "Did not receive any message matching: \"%s\"", qPrintable(list->pattern.toRegularExpression().pattern()));
}
QTest::TestLoggers::addMessage(QAbstractTestLogger::Info, msg);
list = list->next;
@ -462,16 +489,14 @@ void QTestLog::ignoreMessage(QtMsgType type, const char *msg)
{
QTEST_ASSERT(msg);
QTest::IgnoreResultList *item = new QTest::IgnoreResultList(type, msg);
QTest::IgnoreResultList::append(QTest::ignoreResultList, type, QString::fromLocal8Bit(msg));
}
QTest::IgnoreResultList *list = QTest::ignoreResultList;
if (!list) {
QTest::ignoreResultList = item;
return;
}
while (list->next)
list = list->next;
list->next = item;
void QTestLog::ignoreMessage(QtMsgType type, const QRegularExpression &expression)
{
QTEST_ASSERT(expression.isValid());
QTest::IgnoreResultList::append(QTest::ignoreResultList, type, QVariant(expression));
}
void QTestLog::setMaxWarnings(int m)

View File

@ -58,6 +58,7 @@
QT_BEGIN_NAMESPACE
class QBenchmarkResult;
class QRegularExpression;
class Q_TESTLIB_EXPORT QTestLog
{
@ -75,6 +76,7 @@ public:
static void addBenchmarkResult(const QBenchmarkResult &result);
static void ignoreMessage(QtMsgType type, const char *msg);
static void ignoreMessage(QtMsgType type, const QRegularExpression &expression);
static int unhandledIgnoreMessages();
static void printUnhandledIgnoreMessages();
static void clearIgnoreMessages();

View File

@ -24,6 +24,12 @@
<Message type="qdebug" file="" line="0">
<Description><![CDATA[Baba]]></Description>
</Message>
<Message type="qdebug" file="" line="0">
<Description><![CDATA[Bubublabla]]></Description>
</Message>
<Message type="qwarn" file="" line="0">
<Description><![CDATA[Babablabla]]></Description>
</Message>
<Incident type="pass" file="" line="0" />
</TestFunction>
<TestFunction name="testMissingWarnings">
@ -37,6 +43,14 @@
<Description><![CDATA[Not all expected messages were received]]></Description>
</Incident>
</TestFunction>
<TestFunction name="testMissingWarningsRegularExpression">
<Message type="info" file="" line="0">
<Description><![CDATA[Did not receive any message matching: "Warning\s\d"]]></Description>
</Message>
<Incident type="fail" file="" line="0">
<Description><![CDATA[Not all expected messages were received]]></Description>
</Incident>
</TestFunction>
<TestFunction name="testMissingWarningsWithData">
<Message type="info" file="" line="0">
<DataTag><![CDATA[first row]]></DataTag>

View File

@ -7,10 +7,14 @@ QDEBUG : tst_Warnings::testWarnings() Debug
QDEBUG : tst_Warnings::testWarnings() Debug
QDEBUG : tst_Warnings::testWarnings() Baba
QDEBUG : tst_Warnings::testWarnings() Baba
QDEBUG : tst_Warnings::testWarnings() Bubublabla
QWARN : tst_Warnings::testWarnings() Babablabla
PASS : tst_Warnings::testWarnings()
INFO : tst_Warnings::testMissingWarnings() Did not receive message: "Warning0"
INFO : tst_Warnings::testMissingWarnings() Did not receive message: "Warning1"
FAIL! : tst_Warnings::testMissingWarnings() Not all expected messages were received
INFO : tst_Warnings::testMissingWarningsRegularExpression() Did not receive any message matching: "Warning\s\d"
FAIL! : tst_Warnings::testMissingWarningsRegularExpression() Not all expected messages were received
INFO : tst_Warnings::testMissingWarningsWithData(first row) Did not receive message: "Warning0"
INFO : tst_Warnings::testMissingWarningsWithData(first row) Did not receive message: "Warning1"
FAIL! : tst_Warnings::testMissingWarningsWithData(first row) Not all expected messages were received
@ -18,5 +22,5 @@ INFO : tst_Warnings::testMissingWarningsWithData(second row) Did not receive m
INFO : tst_Warnings::testMissingWarningsWithData(second row) Did not receive message: "Warning1"
FAIL! : tst_Warnings::testMissingWarningsWithData(second row) Not all expected messages were received
PASS : tst_Warnings::cleanupTestCase()
Totals: 3 passed, 3 failed, 0 skipped
Totals: 3 passed, 4 failed, 0 skipped
********* Finished testing of tst_Warnings *********

View File

@ -26,6 +26,12 @@
<Message type="qdebug" file="" line="0">
<Description><![CDATA[Baba]]></Description>
</Message>
<Message type="qdebug" file="" line="0">
<Description><![CDATA[Bubublabla]]></Description>
</Message>
<Message type="qwarn" file="" line="0">
<Description><![CDATA[Babablabla]]></Description>
</Message>
<Incident type="pass" file="" line="0" />
</TestFunction>
<TestFunction name="testMissingWarnings">
@ -39,6 +45,14 @@
<Description><![CDATA[Not all expected messages were received]]></Description>
</Incident>
</TestFunction>
<TestFunction name="testMissingWarningsRegularExpression">
<Message type="info" file="" line="0">
<Description><![CDATA[Did not receive any message matching: "Warning\s\d"]]></Description>
</Message>
<Incident type="fail" file="" line="0">
<Description><![CDATA[Not all expected messages were received]]></Description>
</Incident>
</TestFunction>
<TestFunction name="testMissingWarningsWithData">
<Message type="info" file="" line="0">
<DataTag><![CDATA[first row]]></DataTag>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite errors="12" failures="3" tests="5" name="tst_Warnings">
<testsuite errors="15" failures="4" tests="6" name="tst_Warnings">
<properties>
<property value="@INSERT_QT_VERSION_HERE@" name="QTestVersion"/>
<property value="@INSERT_QT_VERSION_HERE@" name="QtVersion"/>
@ -12,12 +12,18 @@
<!-- message="Debug" type="qdebug" -->
<!-- message="Baba" type="qdebug" -->
<!-- message="Baba" type="qdebug" -->
<!-- message="Bubublabla" type="qdebug" -->
<!-- message="Babablabla" type="qwarn" -->
</testcase>
<testcase result="fail" name="testMissingWarnings">
<!-- message="Did not receive message: &quot;Warning0&quot;" type="info" -->
<!-- message="Did not receive message: &quot;Warning1&quot;" type="info" -->
<failure message="Not all expected messages were received" result="fail"/>
</testcase>
<testcase result="fail" name="testMissingWarningsRegularExpression">
<!-- message="Did not receive any message matching: &quot;Warning\s\d&quot;" type="info" -->
<failure message="Not all expected messages were received" result="fail"/>
</testcase>
<testcase result="fail" name="testMissingWarningsWithData">
<!-- tag="first row" message="Did not receive message: &quot;Warning0&quot;" type="info" -->
<!-- tag="first row" message="Did not receive message: &quot;Warning1&quot;" type="info" -->
@ -34,8 +40,11 @@
<![CDATA[Debug]]>
<![CDATA[Baba]]>
<![CDATA[Baba]]>
<![CDATA[Bubublabla]]>
<![CDATA[Babablabla]]>
<![CDATA[Did not receive message: "Warning0"]]>
<![CDATA[Did not receive message: "Warning1"]]>
<![CDATA[Did not receive any message matching: "Warning\s\d"]]>
<![CDATA[Did not receive message: "Warning0"]]>
<![CDATA[Did not receive message: "Warning1"]]>
<![CDATA[Did not receive message: "Warning0"]]>

View File

@ -41,6 +41,7 @@
#include <QtCore/QCoreApplication>
#include <QtCore/QRegularExpression>
#include <QtTest/QtTest>
class tst_Warnings: public QObject
@ -49,6 +50,7 @@ class tst_Warnings: public QObject
private slots:
void testWarnings();
void testMissingWarnings();
void testMissingWarningsRegularExpression();
void testMissingWarningsWithData_data();
void testMissingWarningsWithData();
};
@ -73,6 +75,13 @@ void tst_Warnings::testWarnings()
qDebug("Baba");
qDebug("Bubu");
qDebug("Baba");
QTest::ignoreMessage(QtDebugMsg, QRegularExpression("^Bubu.*"));
QTest::ignoreMessage(QtWarningMsg, QRegularExpression("^Baba.*"));
qDebug("Bubublabla");
qWarning("Babablabla");
qDebug("Bubublabla");
qWarning("Babablabla");
}
void tst_Warnings::testMissingWarnings()
@ -84,6 +93,14 @@ void tst_Warnings::testMissingWarnings()
qWarning("Warning2");
}
void tst_Warnings::testMissingWarningsRegularExpression()
{
QTest::ignoreMessage(QtWarningMsg, QRegularExpression("Warning\\d\\d"));
QTest::ignoreMessage(QtWarningMsg, QRegularExpression("Warning\\s\\d"));
qWarning("Warning11");
}
void tst_Warnings::testMissingWarningsWithData_data()
{
QTest::addColumn<int>("dummy");