1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <QtTest/private/qtestresult_p.h>
41#include <QtCore/qglobal.h>
42
43#include <QtTest/private/qtestlog_p.h>
44#include <QtTest/qtestdata.h>
45#include <QtTest/qtestcase.h>
46#include <QtTest/qtestassert.h>
47
48#include <stdlib.h>
49#include <stdio.h>
50#include <string.h>
51
52static const char *currentAppName = nullptr;
53
54QT_BEGIN_NAMESPACE
55
56namespace QTest
57{
58 static QTestData *currentTestData = nullptr;
59 static QTestData *currentGlobalTestData = nullptr;
60 static const char *currentTestFunc = nullptr;
61 static const char *currentTestObjectName = nullptr;
62 static bool failed = false;
63 static bool skipCurrentTest = false;
64 static bool blacklistCurrentTest = false;
65
66 static const char *expectFailComment = nullptr;
67 static int expectFailMode = 0;
68}
69
70void QTestResult::reset()
71{
72 QTest::currentTestData = nullptr;
73 QTest::currentGlobalTestData = nullptr;
74 QTest::currentTestFunc = nullptr;
75 QTest::currentTestObjectName = nullptr;
76 QTest::failed = false;
77
78 QTest::expectFailComment = nullptr;
79 QTest::expectFailMode = 0;
80 QTest::blacklistCurrentTest = false;
81
82 QTestLog::resetCounters();
83}
84
85void QTestResult::setBlacklistCurrentTest(bool b)
86{
87 QTest::blacklistCurrentTest = b;
88}
89
90bool QTestResult::currentTestFailed()
91{
92 return QTest::failed;
93}
94
95QTestData *QTestResult::currentGlobalTestData()
96{
97 return QTest::currentGlobalTestData;
98}
99
100QTestData *QTestResult::currentTestData()
101{
102 return QTest::currentTestData;
103}
104
105void QTestResult::setCurrentGlobalTestData(QTestData *data)
106{
107 QTest::currentGlobalTestData = data;
108}
109
110void QTestResult::setCurrentTestData(QTestData *data)
111{
112 QTest::currentTestData = data;
113 QTest::failed = false;
114 if (data)
115 QTestLog::enterTestData(data);
116}
117
118void QTestResult::setCurrentTestFunction(const char *func)
119{
120 QTest::currentTestFunc = func;
121 QTest::failed = false;
122 if (func)
123 QTestLog::enterTestFunction(func);
124}
125
126static void clearExpectFail()
127{
128 QTest::expectFailMode = 0;
129 delete [] const_cast<char *>(QTest::expectFailComment);
130 QTest::expectFailComment = nullptr;
131}
132
133void QTestResult::finishedCurrentTestData()
134{
135 if (QTest::expectFailMode)
136 addFailure("QEXPECT_FAIL was called without any subsequent verification statements", nullptr, 0);
137 clearExpectFail();
138
139 if (!QTest::failed && QTestLog::unhandledIgnoreMessages()) {
140 QTestLog::printUnhandledIgnoreMessages();
141 addFailure("Not all expected messages were received", nullptr, 0);
142 }
143 QTestLog::clearIgnoreMessages();
144}
145
146void QTestResult::finishedCurrentTestDataCleanup()
147{
148 // If the current test hasn't failed or been skipped, then it passes.
149 if (!QTest::failed && !QTest::skipCurrentTest) {
150 if (QTest::blacklistCurrentTest)
151 QTestLog::addBPass("");
152 else
153 QTestLog::addPass("");
154 }
155
156 QTest::failed = false;
157}
158
159void QTestResult::finishedCurrentTestFunction()
160{
161 QTest::currentTestFunc = nullptr;
162 QTest::failed = false;
163
164 QTestLog::leaveTestFunction();
165}
166
167const char *QTestResult::currentTestFunction()
168{
169 return QTest::currentTestFunc;
170}
171
172const char *QTestResult::currentDataTag()
173{
174 return QTest::currentTestData ? QTest::currentTestData->dataTag() : nullptr;
175}
176
177const char *QTestResult::currentGlobalDataTag()
178{
179 return QTest::currentGlobalTestData ? QTest::currentGlobalTestData->dataTag() : nullptr;
180}
181
182static bool isExpectFailData(const char *dataIndex)
183{
184 if (!dataIndex || dataIndex[0] == '\0')
185 return true;
186 if (!QTest::currentTestData)
187 return false;
188 if (strcmp(dataIndex, QTest::currentTestData->dataTag()) == 0)
189 return true;
190 return false;
191}
192
193bool QTestResult::expectFail(const char *dataIndex, const char *comment,
194 QTest::TestFailMode mode, const char *file, int line)
195{
196 QTEST_ASSERT(comment);
197 QTEST_ASSERT(mode > 0);
198
199 if (!isExpectFailData(dataIndex)) {
200 delete[] comment;
201 return true; // we don't care
202 }
203
204 if (QTest::expectFailMode) {
205 delete[] comment;
206 clearExpectFail();
207 addFailure("Already expecting a fail", file, line);
208 return false;
209 }
210
211 QTest::expectFailMode = mode;
212 QTest::expectFailComment = comment;
213 return true;
214}
215
216static bool checkStatement(bool statement, const char *msg, const char *file, int line)
217{
218 if (statement) {
219 if (QTest::expectFailMode) {
220 if (QTest::blacklistCurrentTest)
221 QTestLog::addBXPass(msg, file, line);
222 else
223 QTestLog::addXPass(msg, file, line);
224
225 QTest::failed = true;
226 bool doContinue = (QTest::expectFailMode == QTest::Continue);
227 clearExpectFail();
228 return doContinue;
229 }
230 return true;
231 }
232
233 if (QTest::expectFailMode) {
234 if (QTest::blacklistCurrentTest)
235 QTestLog::addBXFail(QTest::expectFailComment, file, line);
236 else
237 QTestLog::addXFail(QTest::expectFailComment, file, line);
238 bool doContinue = (QTest::expectFailMode == QTest::Continue);
239 clearExpectFail();
240 return doContinue;
241 }
242
243 QTestResult::addFailure(msg, file, line);
244 return false;
245}
246
247bool QTestResult::verify(bool statement, const char *statementStr,
248 const char *description, const char *file, int line)
249{
250 QTEST_ASSERT(statementStr);
251
252 char msg[1024] = {'\0'};
253
254 if (QTestLog::verboseLevel() >= 2) {
255 qsnprintf(msg, 1024, "QVERIFY(%s)", statementStr);
256 QTestLog::info(msg, file, line);
257 }
258
259 if (!statement && !QTest::expectFailMode)
260 qsnprintf(msg, 1024, "'%s' returned FALSE. (%s)", statementStr, description ? description : "");
261 else if (statement && QTest::expectFailMode)
262 qsnprintf(msg, 1024, "'%s' returned TRUE unexpectedly. (%s)", statementStr, description ? description : "");
263
264 return checkStatement(statement, msg, file, line);
265}
266
267// Format failures using the toString() template
268template <class Actual, class Expected>
269void formatFailMessage(char *msg, size_t maxMsgLen,
270 const char *failureMsg,
271 const Actual &val1, const Expected &val2,
272 const char *actual, const char *expected)
273{
274 auto val1S = QTest::toString(val1);
275 auto val2S = QTest::toString(val2);
276
277 size_t len1 = mbstowcs(nullptr, actual, maxMsgLen); // Last parameter is not ignored on QNX
278 size_t len2 = mbstowcs(nullptr, expected, maxMsgLen); // (result is never larger than this).
279 qsnprintf(msg, maxMsgLen, "%s\n Actual (%s)%*s %s\n Expected (%s)%*s %s",
280 failureMsg,
281 actual, qMax(len1, len2) - len1 + 1, ":", val1S ? val1S : "<null>",
282 expected, qMax(len1, len2) - len2 + 1, ":", val2S ? val2S : "<null>");
283
284 delete [] val1S;
285 delete [] val2S;
286}
287
288// Overload to format failures for "const char *" - no need to strdup().
289void formatFailMessage(char *msg, size_t maxMsgLen,
290 const char *failureMsg,
291 const char *val1, const char *val2,
292 const char *actual, const char *expected)
293{
294 size_t len1 = mbstowcs(nullptr, actual, maxMsgLen); // Last parameter is not ignored on QNX
295 size_t len2 = mbstowcs(nullptr, expected, maxMsgLen); // (result is never larger than this).
296 qsnprintf(msg, maxMsgLen, "%s\n Actual (%s)%*s %s\n Expected (%s)%*s %s",
297 failureMsg,
298 actual, qMax(len1, len2) - len1 + 1, ":", val1 ? val1 : "<null>",
299 expected, qMax(len1, len2) - len2 + 1, ":", val2 ? val2 : "<null>");
300}
301
302template <class Actual, class Expected>
303static bool compareHelper(bool success, const char *failureMsg,
304 const Actual &val1, const Expected &val2,
305 const char *actual, const char *expected,
306 const char *file, int line,
307 bool hasValues = true)
308{
309 const size_t maxMsgLen = 1024;
310 char msg[maxMsgLen] = {'\0'};
311
312 QTEST_ASSERT(expected);
313 QTEST_ASSERT(actual);
314
315 if (QTestLog::verboseLevel() >= 2) {
316 qsnprintf(msg, maxMsgLen, "QCOMPARE(%s, %s)", actual, expected);
317 QTestLog::info(msg, file, line);
318 }
319
320 if (!failureMsg)
321 failureMsg = "Compared values are not the same";
322
323 if (success) {
324 if (QTest::expectFailMode) {
325 qsnprintf(msg, maxMsgLen,
326 "QCOMPARE(%s, %s) returned TRUE unexpectedly.", actual, expected);
327 }
328 return checkStatement(success, msg, file, line);
329 }
330
331
332 if (!hasValues) {
333 qsnprintf(msg, maxMsgLen, "%s", failureMsg);
334 return checkStatement(success, msg, file, line);
335 }
336
337 formatFailMessage(msg, maxMsgLen, failureMsg, val1, val2, actual, expected);
338
339 return checkStatement(success, msg, file, line);
340}
341
342bool QTestResult::compare(bool success, const char *failureMsg,
343 char *val1, char *val2,
344 const char *actual, const char *expected,
345 const char *file, int line)
346{
347 const bool result = compareHelper(success, failureMsg,
348 val1 != nullptr ? val1 : "<null>",
349 val2 != nullptr ? val2 : "<null>",
350 actual, expected, file, line,
351 val1 != nullptr && val2 != nullptr);
352
353 // Our caller got these from QTest::toString()
354 delete [] val1;
355 delete [] val2;
356
357 return result;
358}
359
360bool QTestResult::compare(bool success, const char *failureMsg,
361 double val1, double val2,
362 const char *actual, const char *expected,
363 const char *file, int line)
364{
365 return compareHelper(success, failureMsg, val1, val2, actual, expected, file, line);
366}
367
368bool QTestResult::compare(bool success, const char *failureMsg,
369 float val1, float val2,
370 const char *actual, const char *expected,
371 const char *file, int line)
372{
373 return compareHelper(success, failureMsg, val1, val2, actual, expected, file, line);
374}
375
376bool QTestResult::compare(bool success, const char *failureMsg,
377 int val1, int val2,
378 const char *actual, const char *expected,
379 const char *file, int line)
380{
381 return compareHelper(success, failureMsg, val1, val2, actual, expected, file, line);
382}
383
384bool QTestResult::compare(bool success, const char *failureMsg,
385 unsigned val1, unsigned val2,
386 const char *actual, const char *expected,
387 const char *file, int line)
388{
389 return compareHelper(success, failureMsg, val1, val2, actual, expected, file, line);
390}
391
392void QTestResult::addFailure(const char *message, const char *file, int line)
393{
394 clearExpectFail();
395
396 if (QTest::blacklistCurrentTest)
397 QTestLog::addBFail(message, file, line);
398 else
399 QTestLog::addFail(message, file, line);
400 QTest::failed = true;
401}
402
403void QTestResult::addSkip(const char *message, const char *file, int line)
404{
405 clearExpectFail();
406
407 QTestLog::addSkip(message, file, line);
408}
409
410void QTestResult::setCurrentTestObject(const char *name)
411{
412 QTest::currentTestObjectName = name;
413}
414
415const char *QTestResult::currentTestObjectName()
416{
417 return QTest::currentTestObjectName ? QTest::currentTestObjectName : "";
418}
419
420void QTestResult::setSkipCurrentTest(bool value)
421{
422 QTest::skipCurrentTest = value;
423}
424
425bool QTestResult::skipCurrentTest()
426{
427 return QTest::skipCurrentTest;
428}
429
430void QTestResult::setCurrentAppName(const char *appName)
431{
432 ::currentAppName = appName;
433}
434
435const char *QTestResult::currentAppName()
436{
437 return ::currentAppName;
438}
439
440QT_END_NAMESPACE
441