[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the QtTest module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 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.
|
---|
| 23 | **
|
---|
[561] | 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.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 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 |
|
---|
| 52 | QT_BEGIN_NAMESPACE
|
---|
| 53 |
|
---|
| 54 | namespace QTest
|
---|
| 55 | {
|
---|
| 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;
|
---|
| 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;
|
---|
[561] | 71 | }
|
---|
[2] | 72 |
|
---|
| 73 | void QTestResult::reset()
|
---|
| 74 | {
|
---|
| 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;
|
---|
| 82 |
|
---|
| 83 | QTest::fails = 0;
|
---|
| 84 | QTest::passes = 0;
|
---|
| 85 | QTest::skips = 0;
|
---|
| 86 |
|
---|
| 87 | QTest::expectFailComment = 0;
|
---|
| 88 | QTest::expectFailMode = 0;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | bool QTestResult::allDataPassed()
|
---|
| 92 | {
|
---|
| 93 | return !QTest::failed;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | bool QTestResult::currentTestFailed()
|
---|
| 97 | {
|
---|
| 98 | return QTest::dataFailed;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | QTestData *QTestResult::currentGlobalTestData()
|
---|
| 102 | {
|
---|
| 103 | return QTest::currentGlobalTestData;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | QTestData *QTestResult::currentTestData()
|
---|
| 107 | {
|
---|
| 108 | return QTest::currentTestData;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | void QTestResult::setCurrentGlobalTestData(QTestData *data)
|
---|
| 112 | {
|
---|
| 113 | QTest::currentGlobalTestData = data;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | void QTestResult::setCurrentTestData(QTestData *data)
|
---|
| 117 | {
|
---|
| 118 | QTest::currentTestData = data;
|
---|
| 119 | QTest::dataFailed = false;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void QTestResult::setCurrentTestFunction(const char *func)
|
---|
| 123 | {
|
---|
| 124 | QTest::currentTestFunc = func;
|
---|
| 125 | QTest::failed = false;
|
---|
| 126 | if (!func)
|
---|
| 127 | QTest::location = NoWhere;
|
---|
| 128 | if (func)
|
---|
| 129 | QTestLog::enterTestFunction(func);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | static void clearExpectFail()
|
---|
| 133 | {
|
---|
| 134 | QTest::expectFailMode = 0;
|
---|
| 135 | delete [] const_cast<char *>(QTest::expectFailComment);
|
---|
| 136 | QTest::expectFailComment = 0;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | void QTestResult::finishedCurrentTestFunction()
|
---|
| 140 | {
|
---|
| 141 | if (!QTest::failed && QTestLog::unhandledIgnoreMessages()) {
|
---|
| 142 | QTestLog::printUnhandledIgnoreMessages();
|
---|
| 143 | addFailure("Not all expected messages were received", 0, 0);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | if (!QTest::failed && !QTest::skipCurrentTest) {
|
---|
| 147 | QTestLog::addPass("");
|
---|
| 148 | ++QTest::passes;
|
---|
| 149 | }
|
---|
| 150 | QTest::currentTestFunc = 0;
|
---|
| 151 | QTest::failed = false;
|
---|
| 152 | QTest::dataFailed = false;
|
---|
| 153 | QTest::location = NoWhere;
|
---|
| 154 |
|
---|
| 155 | QTestLog::leaveTestFunction();
|
---|
| 156 |
|
---|
| 157 | clearExpectFail();
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | const char *QTestResult::currentTestFunction()
|
---|
| 161 | {
|
---|
| 162 | return QTest::currentTestFunc;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | const char *QTestResult::currentDataTag()
|
---|
| 166 | {
|
---|
| 167 | return QTest::currentTestData ? QTest::currentTestData->dataTag()
|
---|
| 168 | : static_cast<const char *>(0);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | const char *QTestResult::currentGlobalDataTag()
|
---|
| 172 | {
|
---|
| 173 | return QTest::currentGlobalTestData ? QTest::currentGlobalTestData->dataTag()
|
---|
| 174 | : static_cast<const char *>(0);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | static bool isExpectFailData(const char *dataIndex)
|
---|
| 178 | {
|
---|
| 179 | if (!dataIndex || dataIndex[0] == '\0')
|
---|
| 180 | return true;
|
---|
| 181 | if (!QTest::currentTestData)
|
---|
| 182 | return false;
|
---|
| 183 | if (strcmp(dataIndex, QTest::currentTestData->dataTag()) == 0)
|
---|
| 184 | return true;
|
---|
| 185 | return false;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | bool QTestResult::expectFail(const char *dataIndex, const char *comment,
|
---|
| 189 | QTest::TestFailMode mode, const char *file, int line)
|
---|
| 190 | {
|
---|
| 191 | QTEST_ASSERT(comment);
|
---|
| 192 | QTEST_ASSERT(mode > 0);
|
---|
| 193 |
|
---|
| 194 | if (!isExpectFailData(dataIndex))
|
---|
| 195 | return true; // we don't care
|
---|
| 196 |
|
---|
| 197 | if (QTest::expectFailMode) {
|
---|
| 198 | clearExpectFail();
|
---|
| 199 | addFailure("Already expecting a fail", file, line);
|
---|
| 200 | return false;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | QTest::expectFailMode = mode;
|
---|
| 204 | QTest::expectFailComment = comment;
|
---|
| 205 | return true;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | static bool checkStatement(bool statement, const char *msg, const char *file, int line)
|
---|
| 209 | {
|
---|
| 210 | if (statement) {
|
---|
| 211 | if (QTest::expectFailMode) {
|
---|
| 212 | QTestLog::addXPass(msg, file, line);
|
---|
| 213 | bool doContinue = (QTest::expectFailMode == QTest::Continue);
|
---|
| 214 | clearExpectFail();
|
---|
| 215 | QTest::failed = true;
|
---|
| 216 | ++QTest::fails;
|
---|
| 217 | return doContinue;
|
---|
| 218 | }
|
---|
| 219 | return true;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | if (QTest::expectFailMode) {
|
---|
| 223 | QTestLog::addXFail(QTest::expectFailComment, file, line);
|
---|
| 224 | bool doContinue = (QTest::expectFailMode == QTest::Continue);
|
---|
| 225 | clearExpectFail();
|
---|
| 226 | return doContinue;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | QTestResult::addFailure(msg, file, line);
|
---|
| 230 | return false;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | bool QTestResult::verify(bool statement, const char *statementStr,
|
---|
| 234 | const char *description, const char *file, int line)
|
---|
| 235 | {
|
---|
| 236 | char msg[1024];
|
---|
| 237 |
|
---|
| 238 | if (QTestLog::verboseLevel() >= 2) {
|
---|
| 239 | QTest::qt_snprintf(msg, 1024, "QVERIFY(%s)", statementStr);
|
---|
| 240 | QTestLog::info(msg, file, line);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | QTest::qt_snprintf(msg, 1024, "'%s' returned FALSE. (%s)", statementStr, description);
|
---|
| 244 |
|
---|
| 245 | return checkStatement(statement, msg, file, line);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | bool QTestResult::compare(bool success, const char *msg, const char *file, int line)
|
---|
| 249 | {
|
---|
| 250 | if (QTestLog::verboseLevel() >= 2) {
|
---|
| 251 | QTestLog::info(msg, file, line);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | return checkStatement(success, msg, file, line);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 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)
|
---|
| 259 | {
|
---|
| 260 | QTEST_ASSERT(expected);
|
---|
| 261 | QTEST_ASSERT(actual);
|
---|
| 262 |
|
---|
| 263 | if (!val1 && !val2)
|
---|
| 264 | return compare(success, msg, file, line);
|
---|
| 265 |
|
---|
| 266 | char buf[1024];
|
---|
| 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>");
|
---|
| 270 | delete [] val1;
|
---|
| 271 | delete [] val2;
|
---|
| 272 | return compare(success, buf, file, line);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | void QTestResult::addFailure(const char *message, const char *file, int line)
|
---|
| 276 | {
|
---|
| 277 | clearExpectFail();
|
---|
| 278 |
|
---|
| 279 | QTestLog::addFail(message, file, line);
|
---|
| 280 | QTest::failed = true;
|
---|
| 281 | QTest::dataFailed = true;
|
---|
| 282 | ++QTest::fails;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | void QTestResult::addSkip(const char *message, QTest::SkipMode mode,
|
---|
| 286 | const char *file, int line)
|
---|
| 287 | {
|
---|
| 288 | clearExpectFail();
|
---|
| 289 |
|
---|
| 290 | QTestLog::addSkip(message, mode, file, line);
|
---|
| 291 | ++QTest::skips;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | QTestResult::TestLocation QTestResult::currentTestLocation()
|
---|
| 295 | {
|
---|
| 296 | return QTest::location;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | void QTestResult::setCurrentTestLocation(TestLocation loc)
|
---|
| 300 | {
|
---|
| 301 | QTest::location = loc;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | void QTestResult::setCurrentTestObject(const char *name)
|
---|
| 305 | {
|
---|
| 306 | QTest::currentTestObjectName = name;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | const char *QTestResult::currentTestObjectName()
|
---|
| 310 | {
|
---|
| 311 | return QTest::currentTestObjectName ? QTest::currentTestObjectName : "";
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | int QTestResult::passCount()
|
---|
| 315 | {
|
---|
| 316 | return QTest::passes;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | int QTestResult::failCount()
|
---|
| 320 | {
|
---|
| 321 | return QTest::fails;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | int QTestResult::skipCount()
|
---|
| 325 | {
|
---|
| 326 | return QTest::skips;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | void QTestResult::ignoreMessage(QtMsgType type, const char *msg)
|
---|
| 330 | {
|
---|
| 331 | QTestLog::addIgnoreMessage(type, msg);
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | bool QTestResult::testFailed()
|
---|
| 335 | {
|
---|
| 336 | return QTest::failed;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | void QTestResult::setSkipCurrentTest(bool value)
|
---|
| 340 | {
|
---|
| 341 | QTest::skipCurrentTest = value;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | bool QTestResult::skipCurrentTest()
|
---|
| 345 | {
|
---|
| 346 | return QTest::skipCurrentTest;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | QT_END_NAMESPACE
|
---|