source: trunk/src/testlib/qtestresult.cpp@ 117

Last change on this file since 117 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 9.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtTest module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
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**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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
52QT_BEGIN_NAMESPACE
53
54namespace 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;
71};
72
73void 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
91bool QTestResult::allDataPassed()
92{
93 return !QTest::failed;
94}
95
96bool QTestResult::currentTestFailed()
97{
98 return QTest::dataFailed;
99}
100
101QTestData *QTestResult::currentGlobalTestData()
102{
103 return QTest::currentGlobalTestData;
104}
105
106QTestData *QTestResult::currentTestData()
107{
108 return QTest::currentTestData;
109}
110
111void QTestResult::setCurrentGlobalTestData(QTestData *data)
112{
113 QTest::currentGlobalTestData = data;
114}
115
116void QTestResult::setCurrentTestData(QTestData *data)
117{
118 QTest::currentTestData = data;
119 QTest::dataFailed = false;
120}
121
122void 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
132static void clearExpectFail()
133{
134 QTest::expectFailMode = 0;
135 delete [] const_cast<char *>(QTest::expectFailComment);
136 QTest::expectFailComment = 0;
137}
138
139void 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
160const char *QTestResult::currentTestFunction()
161{
162 return QTest::currentTestFunc;
163}
164
165const char *QTestResult::currentDataTag()
166{
167 return QTest::currentTestData ? QTest::currentTestData->dataTag()
168 : static_cast<const char *>(0);
169}
170
171const char *QTestResult::currentGlobalDataTag()
172{
173 return QTest::currentGlobalTestData ? QTest::currentGlobalTestData->dataTag()
174 : static_cast<const char *>(0);
175}
176
177static 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
188bool 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
208static 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
233bool 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
248bool 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
257bool 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
275void 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
285void 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
294QTestResult::TestLocation QTestResult::currentTestLocation()
295{
296 return QTest::location;
297}
298
299void QTestResult::setCurrentTestLocation(TestLocation loc)
300{
301 QTest::location = loc;
302}
303
304void QTestResult::setCurrentTestObject(const char *name)
305{
306 QTest::currentTestObjectName = name;
307}
308
309const char *QTestResult::currentTestObjectName()
310{
311 return QTest::currentTestObjectName ? QTest::currentTestObjectName : "";
312}
313
314int QTestResult::passCount()
315{
316 return QTest::passes;
317}
318
319int QTestResult::failCount()
320{
321 return QTest::fails;
322}
323
324int QTestResult::skipCount()
325{
326 return QTest::skips;
327}
328
329void QTestResult::ignoreMessage(QtMsgType type, const char *msg)
330{
331 QTestLog::addIgnoreMessage(type, msg);
332}
333
334bool QTestResult::testFailed()
335{
336 return QTest::failed;
337}
338
339void QTestResult::setSkipCurrentTest(bool value)
340{
341 QTest::skipCurrentTest = value;
342}
343
344bool QTestResult::skipCurrentTest()
345{
346 return QTest::skipCurrentTest;
347}
348
349QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.