Qt 4.8
qtestresult.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/private/qtestresult_p.h"
43 #include <QtCore/qglobal.h>
44 
45 #include "QtTest/private/qtestlog_p.h"
46 #include "QtTest/qtestdata.h"
47 #include "QtTest/qtestassert.h"
48 
49 #include <stdio.h>
50 #include <string.h>
51 
53 
54 namespace QTest
55 {
58  static const char *currentTestFunc = 0;
59  static const char *currentTestObjectName = 0;
60  static bool failed = false;
61  static bool dataFailed = false;
62  static bool skipCurrentTest = false;
64 
65  static int fails = 0;
66  static int passes = 0;
67  static int skips = 0;
68 
69  static const char *expectFailComment = 0;
70  static int expectFailMode = 0;
71 
72  static const char *currentAppName = 0;
73 }
74 
76 {
81  QTest::failed = false;
82  QTest::dataFailed = false;
84 
85  QTest::fails = 0;
86  QTest::passes = 0;
87  QTest::skips = 0;
88 
91 }
92 
94 {
95  return !QTest::failed;
96 }
97 
99 {
100  return QTest::dataFailed;
101 }
102 
104 {
106 }
107 
109 {
110  return QTest::currentTestData;
111 }
112 
114 {
116 }
117 
119 {
121  QTest::dataFailed = false;
122 }
123 
125 {
126  QTest::currentTestFunc = func;
127  QTest::failed = false;
128  if (!func)
129  QTest::location = NoWhere;
130  if (func)
132 }
133 
134 static void clearExpectFail()
135 {
137  delete [] const_cast<char *>(QTest::expectFailComment);
139 }
140 
142 {
145  addFailure("Not all expected messages were received", 0, 0);
146  }
147 
149  QTestLog::addPass("");
150  ++QTest::passes;
151  }
153  QTest::failed = false;
154  QTest::dataFailed = false;
155  QTest::location = NoWhere;
156 
158 
159  clearExpectFail();
160 }
161 
163 {
164  return QTest::currentTestFunc;
165 }
166 
168 {
170  : static_cast<const char *>(0);
171 }
172 
174 {
176  : static_cast<const char *>(0);
177 }
178 
179 static bool isExpectFailData(const char *dataIndex)
180 {
181  if (!dataIndex || dataIndex[0] == '\0')
182  return true;
184  return false;
185  if (strcmp(dataIndex, QTest::currentTestData->dataTag()) == 0)
186  return true;
187  return false;
188 }
189 
190 bool QTestResult::expectFail(const char *dataIndex, const char *comment,
191  QTest::TestFailMode mode, const char *file, int line)
192 {
193  QTEST_ASSERT(comment);
194  QTEST_ASSERT(mode > 0);
195 
196  if (!isExpectFailData(dataIndex)) {
197  delete[] comment;
198  return true; // we don't care
199  }
200 
201  if (QTest::expectFailMode) {
202  delete[] comment;
203  clearExpectFail();
204  addFailure("Already expecting a fail", file, line);
205  return false;
206  }
207 
208  QTest::expectFailMode = mode;
209  QTest::expectFailComment = comment;
210  return true;
211 }
212 
213 static bool checkStatement(bool statement, const char *msg, const char *file, int line)
214 {
215  if (statement) {
216  if (QTest::expectFailMode) {
217  QTestLog::addXPass(msg, file, line);
218  bool doContinue = (QTest::expectFailMode == QTest::Continue);
219  clearExpectFail();
220  QTest::failed = true;
221  ++QTest::fails;
222  return doContinue;
223  }
224  return true;
225  }
226 
227  if (QTest::expectFailMode) {
229  bool doContinue = (QTest::expectFailMode == QTest::Continue);
230  clearExpectFail();
231  return doContinue;
232  }
233 
234  QTestResult::addFailure(msg, file, line);
235  return false;
236 }
237 
238 bool QTestResult::verify(bool statement, const char *statementStr,
239  const char *description, const char *file, int line)
240 {
241  char msg[1024];
242 
243  if (QTestLog::verboseLevel() >= 2) {
244  QTest::qt_snprintf(msg, 1024, "QVERIFY(%s)", statementStr);
245  QTestLog::info(msg, file, line);
246  }
247 
248  QTest::qt_snprintf(msg, 1024, "'%s' returned FALSE. (%s)", statementStr, description);
249 
250  return checkStatement(statement, msg, file, line);
251 }
252 
253 bool QTestResult::compare(bool success, const char *msg, const char *file, int line)
254 {
255  if (QTestLog::verboseLevel() >= 2) {
256  QTestLog::info(msg, file, line);
257  }
258 
259  return checkStatement(success, msg, file, line);
260 }
261 
262 bool QTestResult::compare(bool success, const char *msg, char *val1, char *val2,
263  const char *actual, const char *expected, const char *file, int line)
264 {
265  QTEST_ASSERT(expected);
266  QTEST_ASSERT(actual);
267 
268  if (!val1 && !val2)
269  return compare(success, msg, file, line);
270 
271  char buf[1024];
272  QTest::qt_snprintf(buf, 1024, "%s\n Actual (%s): %s\n Expected (%s): %s", msg,
273  actual, val1 ? val1 : "<null>",
274  expected, val2 ? val2 : "<null>");
275  delete [] val1;
276  delete [] val2;
277  return compare(success, buf, file, line);
278 }
279 
280 void QTestResult::addFailure(const char *message, const char *file, int line)
281 {
282  clearExpectFail();
283 
284  QTestLog::addFail(message, file, line);
285  QTest::failed = true;
286  QTest::dataFailed = true;
287  ++QTest::fails;
288 }
289 
290 void QTestResult::addSkip(const char *message, QTest::SkipMode mode,
291  const char *file, int line)
292 {
293  clearExpectFail();
294 
295  QTestLog::addSkip(message, mode, file, line);
296  ++QTest::skips;
297 }
298 
300 {
301  return QTest::location;
302 }
303 
305 {
306  QTest::location = loc;
307 }
308 
310 {
312 }
313 
315 {
317 }
318 
320 {
321  return QTest::passes;
322 }
323 
325 {
326  return QTest::fails;
327 }
328 
330 {
331  return QTest::skips;
332 }
333 
335 {
336  QTestLog::addIgnoreMessage(type, msg);
337 }
338 
340 {
341  return QTest::failed;
342 }
343 
345 {
346  QTest::skipCurrentTest = value;
347 }
348 
350 {
351  return QTest::skipCurrentTest;
352 }
353 
355 {
357 }
359 {
360  return QTest::currentAppName;
361 }
362 
static const char * currentTestFunction()
static int verboseLevel()
Definition: qtestlog.cpp:363
static void addPass(const char *msg)
Definition: qtestlog.cpp:253
int type
Definition: qmetatype.cpp:239
static bool dataFailed
Definition: qtestresult.cpp:61
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static bool testFailed()
static const char * currentAppName
Definition: qtestresult.cpp:72
SkipMode
This enum describes the modes for skipping tests during execution of the test data.
Definition: qtest_global.h:82
static void setCurrentTestLocation(TestLocation loc)
static int failCount()
static bool skipCurrentTest
Definition: qtestresult.cpp:62
const char * dataTag() const
Definition: qtestdata.cpp:112
static void ignoreMessage(QtMsgType type, const char *msg)
static const char * currentTestFunc
Definition: qtestresult.cpp:58
static void setCurrentTestFunction(const char *func)
static void addFailure(const char *message, const char *file, int line)
static void addSkip(const char *message, QTest::SkipMode mode, const char *file, int line)
static TestLocation currentTestLocation()
QtMsgType
This enum describes the messages that can be sent to a message handler (QtMsgHandler).
Definition: qglobal.h:1881
static int expectFailMode
Definition: qtestresult.cpp:70
static bool expectFail(const char *dataIndex, const char *comment, QTest::TestFailMode mode, const char *file, int line)
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 finishedCurrentTestFunction()
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 const char * currentTestObjectName()
static bool compare(const QVariant::Private *a, const QVariant::Private *b)
Compares a to b.
Definition: qvariant.cpp:383
static int skipCount()
static QTestData * currentTestData()
static bool verify(bool statement, const char *statementStr, const char *extraInfo, const char *file, int line)
static void clearExpectFail()
static const char * currentAppName()
const char * name
static const char * data(const QByteArray &arr)
static QTestData * currentGlobalTestData()
static int fails
Definition: qtestresult.cpp:65
static void reset()
Definition: qtestresult.cpp:75
static int passes
Definition: qtestresult.cpp:66
static bool compare(bool success, const char *msg, const char *file, int line)
static bool isExpectFailData(const char *dataIndex)
int Q_TESTLIB_EXPORT qt_snprintf(char *str, int size, const char *format,...)
Definition: qtestcase.cpp:961
static const char * currentGlobalDataTag()
static void setCurrentGlobalTestData(QTestData *data)
static bool failed
Definition: qtestresult.cpp:60
static void setCurrentTestData(QTestData *data)
static bool checkStatement(bool statement, const char *msg, const char *file, int line)
#define QTEST_ASSERT(cond)
Definition: qtestassert.h:53
static QTestResult::TestLocation location
Definition: qtestresult.cpp:63
static void setSkipCurrentTest(bool value)
static void setCurrentAppName(const char *appName)
TestFailMode
This enum describes the modes for handling an expected failure of the QVERIFY() or QCOMPARE() macros...
Definition: qtest_global.h:83
static const char * currentDataTag()
static void addXFail(const char *msg, const char *file, int line)
Definition: qtestlog.cpp:271
static bool skipCurrentTest()
static QString appName
static int passCount()
static bool currentTestFailed()
Definition: qtestresult.cpp:98
static bool allDataPassed()
Definition: qtestresult.cpp:93
static QTestData * currentTestData
Definition: qtestresult.cpp:56
static const char * expectFailComment
Definition: qtestresult.cpp:69
static void info(const char *msg, const char *file, int line)
Definition: qtestlog.cpp:340
static int skips
Definition: qtestresult.cpp:67
static void setCurrentTestObject(const char *name)
The QTest namespace contains all the functions and declarations that are related to the QTestLib tool...
static void addXPass(const char *msg, const char *file, int line)
Definition: qtestlog.cpp:280
static const char * currentTestObjectName
Definition: qtestresult.cpp:59
static QTestData * currentGlobalTestData
Definition: qtestresult.cpp:57
static void addIgnoreMessage(QtMsgType type, const char *msg)
Definition: qtestlog.cpp:368
static void leaveTestFunction()
Definition: qtestlog.cpp:228