1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtTest module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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 dataFailed
= false;
62 static bool skipCurrentTest
= false;
63 static QTestResult::TestLocation location
= QTestResult::NoWhere
;
66 static int passes
= 0;
69 static const char *expectFailComment
= 0;
70 static int expectFailMode
= 0;
73 void QTestResult::reset()
75 QTest::currentTestData
= 0;
76 QTest::currentGlobalTestData
= 0;
77 QTest::currentTestFunc
= 0;
78 QTest::currentTestObjectName
= 0;
79 QTest::failed
= false;
80 QTest::dataFailed
= false;
81 QTest::location
= QTestResult::NoWhere
;
87 QTest::expectFailComment
= 0;
88 QTest::expectFailMode
= 0;
91 bool QTestResult::allDataPassed()
93 return !QTest::failed
;
96 bool QTestResult::currentTestFailed()
98 return QTest::dataFailed
;
101 QTestData
*QTestResult::currentGlobalTestData()
103 return QTest::currentGlobalTestData
;
106 QTestData
*QTestResult::currentTestData()
108 return QTest::currentTestData
;
111 void QTestResult::setCurrentGlobalTestData(QTestData
*data
)
113 QTest::currentGlobalTestData
= data
;
116 void QTestResult::setCurrentTestData(QTestData
*data
)
118 QTest::currentTestData
= data
;
119 QTest::dataFailed
= false;
122 void QTestResult::setCurrentTestFunction(const char *func
)
124 QTest::currentTestFunc
= func
;
125 QTest::failed
= false;
127 QTest::location
= NoWhere
;
129 QTestLog::enterTestFunction(func
);
132 static void clearExpectFail()
134 QTest::expectFailMode
= 0;
135 delete [] const_cast<char *>(QTest::expectFailComment
);
136 QTest::expectFailComment
= 0;
139 void QTestResult::finishedCurrentTestFunction()
141 if (!QTest::failed
&& QTestLog::unhandledIgnoreMessages()) {
142 QTestLog::printUnhandledIgnoreMessages();
143 addFailure("Not all expected messages were received", 0, 0);
146 if (!QTest::failed
&& !QTest::skipCurrentTest
) {
147 QTestLog::addPass("");
150 QTest::currentTestFunc
= 0;
151 QTest::failed
= false;
152 QTest::dataFailed
= false;
153 QTest::location
= NoWhere
;
155 QTestLog::leaveTestFunction();
160 const char *QTestResult::currentTestFunction()
162 return QTest::currentTestFunc
;
165 const char *QTestResult::currentDataTag()
167 return QTest::currentTestData
? QTest::currentTestData
->dataTag()
168 : static_cast<const char *>(0);
171 const char *QTestResult::currentGlobalDataTag()
173 return QTest::currentGlobalTestData
? QTest::currentGlobalTestData
->dataTag()
174 : static_cast<const char *>(0);
177 static bool isExpectFailData(const char *dataIndex
)
179 if (!dataIndex
|| dataIndex
[0] == '\0')
181 if (!QTest::currentTestData
)
183 if (strcmp(dataIndex
, QTest::currentTestData
->dataTag()) == 0)
188 bool QTestResult::expectFail(const char *dataIndex
, const char *comment
,
189 QTest::TestFailMode mode
, const char *file
, int line
)
191 QTEST_ASSERT(comment
);
192 QTEST_ASSERT(mode
> 0);
194 if (!isExpectFailData(dataIndex
))
195 return true; // we don't care
197 if (QTest::expectFailMode
) {
199 addFailure("Already expecting a fail", file
, line
);
203 QTest::expectFailMode
= mode
;
204 QTest::expectFailComment
= comment
;
208 static bool checkStatement(bool statement
, const char *msg
, const char *file
, int line
)
211 if (QTest::expectFailMode
) {
212 QTestLog::addXPass(msg
, file
, line
);
213 bool doContinue
= (QTest::expectFailMode
== QTest::Continue
);
215 QTest::failed
= true;
222 if (QTest::expectFailMode
) {
223 QTestLog::addXFail(QTest::expectFailComment
, file
, line
);
224 bool doContinue
= (QTest::expectFailMode
== QTest::Continue
);
229 QTestResult::addFailure(msg
, file
, line
);
233 bool QTestResult::verify(bool statement
, const char *statementStr
,
234 const char *description
, const char *file
, int line
)
238 if (QTestLog::verboseLevel() >= 2) {
239 QTest::qt_snprintf(msg
, 1024, "QVERIFY(%s)", statementStr
);
240 QTestLog::info(msg
, file
, line
);
243 QTest::qt_snprintf(msg
, 1024, "'%s' returned FALSE. (%s)", statementStr
, description
);
245 return checkStatement(statement
, msg
, file
, line
);
248 bool QTestResult::compare(bool success
, const char *msg
, const char *file
, int line
)
250 if (QTestLog::verboseLevel() >= 2) {
251 QTestLog::info(msg
, file
, line
);
254 return checkStatement(success
, msg
, file
, line
);
257 bool QTestResult::compare(bool success
, const char *msg
, char *val1
, char *val2
,
258 const char *actual
, const char *expected
, const char *file
, int line
)
260 QTEST_ASSERT(expected
);
261 QTEST_ASSERT(actual
);
264 return compare(success
, msg
, file
, line
);
267 QTest::qt_snprintf(buf
, 1024, "%s\n Actual (%s): %s\n Expected (%s): %s", msg
,
268 actual
, val1
? val1
: "<null>",
269 expected
, val2
? val2
: "<null>");
272 return compare(success
, buf
, file
, line
);
275 void QTestResult::addFailure(const char *message
, const char *file
, int line
)
279 QTestLog::addFail(message
, file
, line
);
280 QTest::failed
= true;
281 QTest::dataFailed
= true;
285 void QTestResult::addSkip(const char *message
, QTest::SkipMode mode
,
286 const char *file
, int line
)
290 QTestLog::addSkip(message
, mode
, file
, line
);
294 QTestResult::TestLocation
QTestResult::currentTestLocation()
296 return QTest::location
;
299 void QTestResult::setCurrentTestLocation(TestLocation loc
)
301 QTest::location
= loc
;
304 void QTestResult::setCurrentTestObject(const char *name
)
306 QTest::currentTestObjectName
= name
;
309 const char *QTestResult::currentTestObjectName()
311 return QTest::currentTestObjectName
? QTest::currentTestObjectName
: "";
314 int QTestResult::passCount()
316 return QTest::passes
;
319 int QTestResult::failCount()
324 int QTestResult::skipCount()
329 void QTestResult::ignoreMessage(QtMsgType type
, const char *msg
)
331 QTestLog::addIgnoreMessage(type
, msg
);
334 bool QTestResult::testFailed()
336 return QTest::failed
;
339 void QTestResult::setSkipCurrentTest(bool value
)
341 QTest::skipCurrentTest
= value
;
344 bool QTestResult::skipCurrentTest()
346 return QTest::skipCurrentTest
;