1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtTest module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include <QtTest/private/qtestresult_p.h>
43 #include <QtCore/qglobal.h>
45 #include <QtTest/private/qtestlog_p.h>
46 #include <QtTest/qtestdata.h>
47 #include <QtTest/qtestassert.h>
56 static QTestData *currentTestData = 0;
57 static QTestData *currentGlobalTestData = 0;
58 static const char *currentTestFunc = 0;
59 static const char *currentTestObjectName = 0;
60 static bool failed = false;
61 static bool skipCurrentTest = false;
63 static const char *expectFailComment = 0;
64 static int expectFailMode = 0;
66 static const char *currentAppname = 0;
69 void QTestResult::reset()
71 QTest::currentTestData = 0;
72 QTest::currentGlobalTestData = 0;
73 QTest::currentTestFunc = 0;
74 QTest::currentTestObjectName = 0;
75 QTest::failed = false;
77 QTest::expectFailComment = 0;
78 QTest::expectFailMode = 0;
80 QTestLog::resetCounters();
83 bool QTestResult::currentTestFailed()
88 QTestData *QTestResult::currentGlobalTestData()
90 return QTest::currentGlobalTestData;
93 QTestData *QTestResult::currentTestData()
95 return QTest::currentTestData;
98 void QTestResult::setCurrentGlobalTestData(QTestData *data)
100 QTest::currentGlobalTestData = data;
103 void QTestResult::setCurrentTestData(QTestData *data)
105 QTest::currentTestData = data;
106 QTest::failed = false;
109 void QTestResult::setCurrentTestFunction(const char *func)
111 QTest::currentTestFunc = func;
112 QTest::failed = false;
114 QTestLog::enterTestFunction(func);
117 static void clearExpectFail()
119 QTest::expectFailMode = 0;
120 delete [] const_cast<char *>(QTest::expectFailComment);
121 QTest::expectFailComment = 0;
124 void QTestResult::finishedCurrentTestData()
126 if (QTest::expectFailMode)
127 addFailure("QEXPECT_FAIL was called without any subsequent verification statements", 0, 0);
130 if (!QTest::failed && QTestLog::unhandledIgnoreMessages()) {
131 QTestLog::printUnhandledIgnoreMessages();
132 addFailure("Not all expected messages were received", 0, 0);
134 QTestLog::clearIgnoreMessages();
137 void QTestResult::finishedCurrentTestDataCleanup()
139 // If the current test hasn't failed or been skipped, then it passes.
140 if (!QTest::failed && !QTest::skipCurrentTest) {
141 QTestLog::addPass("");
144 QTest::failed = false;
147 void QTestResult::finishedCurrentTestFunction()
149 QTest::currentTestFunc = 0;
150 QTest::failed = false;
152 QTestLog::leaveTestFunction();
155 const char *QTestResult::currentTestFunction()
157 return QTest::currentTestFunc;
160 const char *QTestResult::currentDataTag()
162 return QTest::currentTestData ? QTest::currentTestData->dataTag()
163 : static_cast<const char *>(0);
166 const char *QTestResult::currentGlobalDataTag()
168 return QTest::currentGlobalTestData ? QTest::currentGlobalTestData->dataTag()
169 : static_cast<const char *>(0);
172 static bool isExpectFailData(const char *dataIndex)
174 if (!dataIndex || dataIndex[0] == '\0')
176 if (!QTest::currentTestData)
178 if (strcmp(dataIndex, QTest::currentTestData->dataTag()) == 0)
183 bool QTestResult::expectFail(const char *dataIndex, const char *comment,
184 QTest::TestFailMode mode, const char *file, int line)
186 QTEST_ASSERT(comment);
187 QTEST_ASSERT(mode > 0);
189 if (!isExpectFailData(dataIndex)) {
191 return true; // we don't care
194 if (QTest::expectFailMode) {
197 addFailure("Already expecting a fail", file, line);
201 QTest::expectFailMode = mode;
202 QTest::expectFailComment = comment;
206 static bool checkStatement(bool statement, const char *msg, const char *file, int line)
209 if (QTest::expectFailMode) {
210 QTestLog::addXPass(msg, file, line);
211 bool doContinue = (QTest::expectFailMode == QTest::Continue);
213 QTest::failed = true;
219 if (QTest::expectFailMode) {
220 QTestLog::addXFail(QTest::expectFailComment, file, line);
221 bool doContinue = (QTest::expectFailMode == QTest::Continue);
226 QTestResult::addFailure(msg, file, line);
230 bool QTestResult::verify(bool statement, const char *statementStr,
231 const char *description, const char *file, int line)
233 QTEST_ASSERT(statementStr);
237 if (QTestLog::verboseLevel() >= 2) {
238 qsnprintf(msg, 1024, "QVERIFY(%s)", statementStr);
239 QTestLog::info(msg, file, line);
242 const char * format = QTest::expectFailMode
243 ? "'%s' returned TRUE unexpectedly. (%s)"
244 : "'%s' returned FALSE. (%s)";
245 qsnprintf(msg, 1024, format, statementStr, description ? description : "");
247 return checkStatement(statement, msg, file, line);
250 bool QTestResult::compare(bool success, const char *failureMsg,
251 char *val1, char *val2,
252 const char *actual, const char *expected,
253 const char *file, int line)
255 QTEST_ASSERT(expected);
256 QTEST_ASSERT(actual);
260 if (QTestLog::verboseLevel() >= 2) {
261 qsnprintf(msg, 1024, "QCOMPARE(%s, %s)", actual, expected);
262 QTestLog::info(msg, file, line);
266 failureMsg = "Compared values are not the same";
268 if (success && QTest::expectFailMode) {
269 qsnprintf(msg, 1024, "QCOMPARE(%s, %s) returned TRUE unexpectedly.", actual, expected);
270 } else if (val1 || val2) {
271 qsnprintf(msg, 1024, "%s\n Actual (%s): %s\n Expected (%s): %s",
273 actual, val1 ? val1 : "<null>",
274 expected, val2 ? val2 : "<null>");
276 qsnprintf(msg, 1024, "%s", failureMsg);
281 return checkStatement(success, msg, file, line);
284 void QTestResult::addFailure(const char *message, const char *file, int line)
288 QTestLog::addFail(message, file, line);
289 QTest::failed = true;
292 void QTestResult::addSkip(const char *message, const char *file, int line)
296 QTestLog::addSkip(message, file, line);
299 void QTestResult::setCurrentTestObject(const char *name)
301 QTest::currentTestObjectName = name;
304 const char *QTestResult::currentTestObjectName()
306 return QTest::currentTestObjectName ? QTest::currentTestObjectName : "";
309 void QTestResult::setSkipCurrentTest(bool value)
311 QTest::skipCurrentTest = value;
314 bool QTestResult::skipCurrentTest()
316 return QTest::skipCurrentTest;
319 void QTestResult::setCurrentAppname(const char *appname)
321 QTest::currentAppname = appname;
324 const char *QTestResult::currentAppname()
326 return QTest::currentAppname;