Qt 4.8
qtestlog.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "QtTest/qtestassert.h"
43 
44 #include "QtTest/private/qtestlog_p.h"
45 #include "QtTest/private/qtestresult_p.h"
46 #include "QtTest/private/qabstracttestlogger_p.h"
47 #include "QtTest/private/qplaintestlogger_p.h"
48 #include "QtTest/private/qxmltestlogger_p.h"
49 #include <QtCore/qatomic.h>
50 #include <QtCore/qbytearray.h>
51 
52 #include <stdlib.h>
53 #include <string.h>
54 #include <limits.h>
55 
56 
57 #include "qtestlogger_p.h"
58 
60 
61 namespace QTest {
62 
64  {
65  inline IgnoreResultList(QtMsgType tp, const char *message)
66  : type(tp), next(0)
67  { msg = qstrdup(message); }
69  { delete [] msg; }
70 
71  static inline void clearList(IgnoreResultList *&list)
72  {
73  while (list) {
74  IgnoreResultList *current = list;
75  list = list->next;
76  delete current;
77  }
78  }
79 
81  char *msg;
83  };
84 
86 
89  static int verbosity = 0;
90  static int maxWarnings = 2002;
91 
93  static const char *outFile = 0;
94 
96 
97  static bool handleIgnoredMessage(QtMsgType type, const char *msg)
98  {
99  IgnoreResultList *last = 0;
101  while (list) {
102  if (list->type == type && strcmp(msg, list->msg) == 0) {
103  // remove the item from the list
104  if (last)
105  last->next = list->next;
106  else if (list->next)
107  ignoreResultList = list->next;
108  else
109  ignoreResultList = 0;
110 
111  delete list;
112  return true;
113  }
114 
115  last = list;
116  list = list->next;
117  }
118  return false;
119  }
120 
121  static void messageHandler(QtMsgType type, const char *msg)
122  {
124 
125  if (!msg || !QTest::testLogger) {
126  // if this goes wrong, something is seriously broken.
127  qInstallMsgHandler(oldMessageHandler);
128  QTEST_ASSERT(msg);
130  }
131 
132  if (handleIgnoredMessage(type, msg))
133  // the message is expected, so just swallow it.
134  return;
135 
136  if (type != QtFatalMsg) {
137  if (counter <= 0)
138  return;
139 
140  if (!counter.deref()) {
142  "Maximum amount of warnings exceeded. Use -maxwarnings to override.");
143  return;
144  }
145  }
146 
147  switch (type) {
148  case QtDebugMsg:
150  break;
151  case QtCriticalMsg:
153  break;
154  case QtWarningMsg:
156  break;
157  case QtFatalMsg:
159  /* Right now, we're inside the custom message handler and we're
160  * being qt_message_output in qglobal.cpp. After we return from
161  * this function, it will proceed with calling exit() and abort()
162  * and hence crash. Therefore, we call these logging functions such
163  * that we wrap up nicely, and in particular produce well-formed XML. */
164  QTestResult::addFailure("Received a fatal error.", "Unknown file", 0);
167  break;
168  }
169  }
170 
172 {
173  switch (QTest::logMode) {
174  case QTestLog::Plain:
176  break;
177  case QTestLog::XML:{
180  else
182  break;
183  }case QTestLog::LightXML:{
186  else
188  break;
189  }case QTestLog::XunitXML:
191  }
192 }
193 
195 
196 }
197 
199 {
200 }
201 
203 {
204 }
205 
206 void QTestLog::enterTestFunction(const char* function)
207 {
209  return;
210 
212  QTEST_ASSERT(function);
213 
215 }
216 
218 {
219  int i = 0;
221  while (list) {
222  ++i;
223  list = list->next;
224  }
225  return i;
226 }
227 
229 {
231  return;
232 
234 
237 }
238 
240 {
242 
243  char msg[1024];
245  while (list) {
246  QTest::qt_snprintf(msg, 1024, "Did not receive message: \"%s\"", list->msg);
248 
249  list = list->next;
250  }
251 }
252 
253 void QTestLog::addPass(const char *msg)
254 {
256  return;
257 
259  QTEST_ASSERT(msg);
260 
262 }
263 
264 void QTestLog::addFail(const char *msg, const char *file, int line)
265 {
267 
269 }
270 
271 void QTestLog::addXFail(const char *msg, const char *file, int line)
272 {
274  QTEST_ASSERT(msg);
275  QTEST_ASSERT(file);
276 
278 }
279 
280 void QTestLog::addXPass(const char *msg, const char *file, int line)
281 {
283  QTEST_ASSERT(msg);
284  QTEST_ASSERT(file);
285 
287 }
288 
289 void QTestLog::addSkip(const char *msg, QTest::SkipMode /*mode*/,
290  const char *file, int line)
291 {
293  QTEST_ASSERT(msg);
294  QTEST_ASSERT(file);
295 
297 }
298 
300 {
303 }
304 
305 void QTestLog::startLogging(unsigned int randomSeed)
306 {
312 }
313 
315 {
320 }
321 
323 {
325 
328  delete QTest::testLogger;
329  QTest::testLogger = 0;
330 }
331 
332 void QTestLog::warn(const char *msg)
333 {
335  QTEST_ASSERT(msg);
336 
338 }
339 
340 void QTestLog::info(const char *msg, const char *file, int line)
341 {
342  QTEST_ASSERT(msg);
343 
344  if (QTest::testLogger)
346 }
347 
349 {
350  QTest::logMode = mode;
351 }
352 
354 {
355  return QTest::logMode;
356 }
357 
359 {
360  QTest::verbosity = level;
361 }
362 
364 {
365  return QTest::verbosity;
366 }
367 
369 {
370  QTest::IgnoreResultList *item = new QTest::IgnoreResultList(type, msg);
371 
373  if (!list) {
375  return;
376  }
377  while (list->next)
378  list = list->next;
379  list->next = item;
380 }
381 
383 {
384  QTEST_ASSERT(fileName);
385 
387 }
388 
390 {
391  return QTest::outFile;
392 }
393 
395 {
396  QTest::maxWarnings = m <= 0 ? INT_MAX : m + 2;
397 }
398 
400 {
401  QTest::flushMode = mode;
402 }
403 
virtual void registerRandomSeed(unsigned int seed)=0
void initLogger()
Definition: qtestlog.cpp:171
#define Q_TESTLIB_EXPORT
Definition: qtest_global.h:56
static LogMode logMode()
Definition: qtestlog.cpp:353
static void setMaxWarnings(int max)
Definition: qtestlog.cpp:394
static int verboseLevel()
Definition: qtestlog.cpp:363
static QTestLog::LogMode logMode
Definition: qtestlog.cpp:87
virtual void addIncident(IncidentTypes type, const char *description, const char *file=0, int line=0)=0
static void addPass(const char *msg)
Definition: qtestlog.cpp:253
static void addBenchmarkResult(const QBenchmarkResult &result)
Definition: qtestlog.cpp:299
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
SkipMode
This enum describes the modes for skipping tests during execution of the test data.
Definition: qtest_global.h:82
static IgnoreResultList * ignoreResultList
Definition: qtestlog.cpp:85
static void addFailure(const char *message, const char *file, int line)
void(* QtMsgHandler)(QtMsgType, const char *)
Definition: qglobal.h:1885
#define Q_BASIC_ATOMIC_INITIALIZER(a)
Definition: qbasicatomic.h:218
static void redirectOutput(const char *fileName)
Definition: qtestlog.cpp:382
QtMsgType
This enum describes the messages that can be sent to a message handler (QtMsgHandler).
Definition: qglobal.h:1881
static const char * outputFileName()
Definition: qtestlog.cpp:389
static void clearList(IgnoreResultList *&list)
Definition: qtestlog.cpp:71
static void enterTestFunction(const char *function)
Definition: qtestlog.cpp:206
static int unhandledIgnoreMessages()
Definition: qtestlog.cpp:217
static void printUnhandledIgnoreMessages()
Definition: qtestlog.cpp:239
static void addSkip(const char *msg, QTest::SkipMode mode, const char *file, int line)
Definition: qtestlog.cpp:289
static void warn(const char *msg)
Definition: qtestlog.cpp:332
static void addFail(const char *msg, const char *file, int line)
Definition: qtestlog.cpp:264
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static QTestLog::FlushMode flushMode
Definition: qtestlog.cpp:88
static int verbosity
Definition: qtestlog.cpp:89
static void startLogging()
Definition: qtestlog.cpp:314
static void setFlushMode(FlushMode mode)
Definition: qtestlog.cpp:399
static QAbstractTestLogger * testLogger
Definition: qtestlog.cpp:92
int Q_TESTLIB_EXPORT qt_snprintf(char *str, int size, const char *format,...)
Definition: qtestcase.cpp:961
static bool handleIgnoredMessage(QtMsgType type, const char *msg)
Definition: qtestlog.cpp:97
static void stopLogging()
Definition: qtestlog.cpp:322
static QtMsgHandler oldMessageHandler
Definition: qtestlog.cpp:95
static int maxWarnings
Definition: qtestlog.cpp:90
QtMsgHandler qInstallMsgHandler(QtMsgHandler h)
Definition: qglobal.cpp:2698
Q_CORE_EXPORT char * qstrdup(const char *)
Q_TESTLIB_EXPORT bool printAvailableTags
Definition: qtestcase.cpp:1087
static void messageHandler(QtMsgType type, const char *msg)
Definition: qtestlog.cpp:121
#define QTEST_ASSERT(cond)
Definition: qtestassert.h:53
static const char * outFile
Definition: qtestlog.cpp:93
IgnoreResultList * next
Definition: qtestlog.cpp:82
static void addXFail(const char *msg, const char *file, int line)
Definition: qtestlog.cpp:271
IgnoreResultList(QtMsgType tp, const char *message)
Definition: qtestlog.cpp:65
static void setVerboseLevel(int level)
Definition: qtestlog.cpp:358
static void info(const char *msg, const char *file, int line)
Definition: qtestlog.cpp:340
virtual void addMessage(MessageTypes type, const char *message, const char *file=0, int line=0)=0
static QString fileName(const QString &fileUrl)
virtual void enterTestFunction(const char *function)=0
The QTest namespace contains all the functions and declarations that are related to the QTestLib tool...
#define INT_MAX
virtual void addBenchmarkResult(const QBenchmarkResult &result)=0
static void addXPass(const char *msg, const char *file, int line)
Definition: qtestlog.cpp:280
static void setLogMode(LogMode mode)
Definition: qtestlog.cpp:348
static void addIgnoreMessage(QtMsgType type, const char *msg)
Definition: qtestlog.cpp:368
static void leaveTestFunction()
Definition: qtestlog.cpp:228
virtual void leaveTestFunction()=0