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#include <QtCore/qstringview.h>
43
44#include <QtTest/private/qtestlog_p.h>
45#include <QtTest/qtest.h> // toString() specializations for QStringView
46#include <QtTest/qtestdata.h>
47#include <QtTest/qtestcase.h>
48#include <QtTest/qtestassert.h>
49
50#include <stdlib.h>
51#include <stdio.h>
52#include <string.h>
53
54static const char *currentAppName = nullptr;
55
56QT_BEGIN_NAMESPACE
57
58namespace QTest
59{
60 namespace Internal {
61 static bool failed = false;
62 }
63
64 static void setFailed(bool failed)
65 {
66 static const bool fatalFailure = []() {
67 static const char * const environmentVar = "QTEST_FATAL_FAIL";
68 if (!qEnvironmentVariableIsSet(environmentVar))
69 return false;
70
71 bool ok;
72 const int fatal = qEnvironmentVariableIntValue(environmentVar, &ok);
73 return ok && fatal;
74 }();
75
76 if (failed && fatalFailure)
77 qTerminate();
78 Internal::failed = failed;
79 }
80
81 static void resetFailed()
82 {
83 setFailed(false);
84 }
85
86 static bool hasFailed()
87 {
88 return Internal::failed;
89 }
90
91 static QTestData *currentTestData = nullptr;
92 static QTestData *currentGlobalTestData = nullptr;
93 static const char *currentTestFunc = nullptr;
94 static const char *currentTestObjectName = nullptr;
95 static bool skipCurrentTest = false;
96 static bool blacklistCurrentTest = false;
97
98 static const char *expectFailComment = nullptr;
99 static int expectFailMode = 0;
100}
101
102void QTestResult::reset()
103{
104 QTest::currentTestData = nullptr;
105 QTest::currentGlobalTestData = nullptr;
106 QTest::currentTestFunc = nullptr;
107 QTest::currentTestObjectName = nullptr;
108 QTest::resetFailed();
109
110 QTest::expectFailComment = nullptr;
111 QTest::expectFailMode = 0;
112 QTest::blacklistCurrentTest = false;
113
114 QTestLog::resetCounters();
115}
116
117void QTestResult::setBlacklistCurrentTest(bool b)
118{
119 QTest::blacklistCurrentTest = b;
120}
121
122bool QTestResult::currentTestFailed()
123{
124 return QTest::hasFailed();
125}
126
127QTestData *QTestResult::currentGlobalTestData()
128{
129 return QTest::currentGlobalTestData;
130}
131
132QTestData *QTestResult::currentTestData()
133{
134 return QTest::currentTestData;
135}
136
137void QTestResult::setCurrentGlobalTestData(QTestData *data)
138{
139 QTest::currentGlobalTestData = data;
140}
141
142void QTestResult::setCurrentTestData(QTestData *data)
143{
144 QTest::currentTestData = data;
145 QTest::resetFailed();
146 if (data)
147 QTestLog::enterTestData(data);
148}
149
150void QTestResult::setCurrentTestFunction(const char *func)
151{
152 QTest::currentTestFunc = func;
153 QTest::resetFailed();
154 if (func)
155 QTestLog::enterTestFunction(func);
156}
157
158static void clearExpectFail()
159{
160 QTest::expectFailMode = 0;
161 delete [] const_cast<char *>(QTest::expectFailComment);
162 QTest::expectFailComment = nullptr;
163}
164
165void QTestResult::finishedCurrentTestData()
166{
167 if (QTest::expectFailMode) {
168 addFailure("QEXPECT_FAIL was called without any subsequent verification statements",
169 "Unknown File", 0);
170 }
171 clearExpectFail();
172
173 if (!QTest::hasFailed() && QTestLog::unhandledIgnoreMessages()) {
174 QTestLog::printUnhandledIgnoreMessages();
175 addFailure("Not all expected messages were received", "Unknown File", 0);
176 }
177 QTestLog::clearIgnoreMessages();
178}
179
180void QTestResult::finishedCurrentTestDataCleanup()
181{
182 // If the current test hasn't failed or been skipped, then it passes.
183 if (!QTest::hasFailed() && !QTest::skipCurrentTest) {
184 if (QTest::blacklistCurrentTest)
185 QTestLog::addBPass("");
186 else
187 QTestLog::addPass("");
188 }
189
190 QTest::resetFailed();
191}
192
193void QTestResult::finishedCurrentTestFunction()
194{
195 QTest::currentTestFunc = nullptr;
196 QTest::resetFailed();
197
198 QTestLog::leaveTestFunction();
199}
200
201const char *QTestResult::currentTestFunction()
202{
203 return QTest::currentTestFunc;
204}
205
206const char *QTestResult::currentDataTag()
207{
208 return QTest::currentTestData ? QTest::currentTestData->dataTag() : nullptr;
209}
210
211const char *QTestResult::currentGlobalDataTag()
212{
213 return QTest::currentGlobalTestData ? QTest::currentGlobalTestData->dataTag() : nullptr;
214}
215
216static bool isExpectFailData(const char *dataIndex)
217{
218 if (!dataIndex || dataIndex[0] == '\0')
219 return true;
220 if (!QTest::currentTestData)
221 return false;
222 if (strcmp(dataIndex, QTest::currentTestData->dataTag()) == 0)
223 return true;
224 return false;
225}
226
227bool QTestResult::expectFail(const char *dataIndex, const char *comment,
228 QTest::TestFailMode mode, const char *file, int line)
229{
230 QTEST_ASSERT(comment);
231 QTEST_ASSERT(mode > 0);
232
233 if (!isExpectFailData(dataIndex)) {
234 delete[] comment;
235 return true; // we don't care
236 }
237
238 if (QTest::expectFailMode) {
239 delete[] comment;
240 clearExpectFail();
241 addFailure("Already expecting a fail", file, line);
242 return false;
243 }
244
245 QTest::expectFailMode = mode;
246 QTest::expectFailComment = comment;
247 return true;
248}
249
250static bool checkStatement(bool statement, const char *msg, const char *file, int line)
251{
252 if (statement) {
253 if (QTest::expectFailMode) {
254 if (QTest::blacklistCurrentTest)
255 QTestLog::addBXPass(msg, file, line);
256 else
257 QTestLog::addXPass(msg, file, line);
258
259 QTest::setFailed(true);
260 bool doContinue = (QTest::expectFailMode == QTest::Continue);
261 clearExpectFail();
262 return doContinue;
263 }
264 return true;
265 }
266
267 if (QTest::expectFailMode) {
268 if (QTest::blacklistCurrentTest)
269 QTestLog::addBXFail(QTest::expectFailComment, file, line);
270 else
271 QTestLog::addXFail(QTest::expectFailComment, file, line);
272 bool doContinue = (QTest::expectFailMode == QTest::Continue);
273 clearExpectFail();
274 return doContinue;
275 }
276
277 QTestResult::addFailure(msg, file, line);
278 return false;
279}
280
281bool QTestResult::verify(bool statement, const char *statementStr,
282 const char *description, const char *file, int line)
283{
284 QTEST_ASSERT(statementStr);
285
286 char msg[1024] = {'\0'};
287
288 if (QTestLog::verboseLevel() >= 2) {
289 qsnprintf(msg, 1024, "QVERIFY(%s)", statementStr);
290 QTestLog::info(msg, file, line);
291 }
292
293 if (!statement && !QTest::expectFailMode)
294 qsnprintf(msg, 1024, "'%s' returned FALSE. (%s)", statementStr, description ? description : "");
295 else if (statement && QTest::expectFailMode)
296 qsnprintf(msg, 1024, "'%s' returned TRUE unexpectedly. (%s)", statementStr, description ? description : "");
297
298 return checkStatement(statement, msg, file, line);
299}
300
301// Format failures using the toString() template
302template <class Actual, class Expected>
303void formatFailMessage(char *msg, size_t maxMsgLen,
304 const char *failureMsg,
305 const Actual &val1, const Expected &val2,
306 const char *actual, const char *expected)
307{
308 auto val1S = QTest::toString(val1);
309 auto val2S = QTest::toString(val2);
310
311 size_t len1 = mbstowcs(nullptr, actual, maxMsgLen); // Last parameter is not ignored on QNX
312 size_t len2 = mbstowcs(nullptr, expected, maxMsgLen); // (result is never larger than this).
313 qsnprintf(msg, maxMsgLen, "%s\n Actual (%s)%*s %s\n Expected (%s)%*s %s",
314 failureMsg,
315 actual, qMax(len1, len2) - len1 + 1, ":", val1S ? val1S : "<null>",
316 expected, qMax(len1, len2) - len2 + 1, ":", val2S ? val2S : "<null>");
317
318 delete [] val1S;
319 delete [] val2S;
320}
321
322// Overload to format failures for "const char *" - no need to strdup().
323void formatFailMessage(char *msg, size_t maxMsgLen,
324 const char *failureMsg,
325 const char *val1, const char *val2,
326 const char *actual, const char *expected)
327{
328 size_t len1 = mbstowcs(nullptr, actual, maxMsgLen); // Last parameter is not ignored on QNX
329 size_t len2 = mbstowcs(nullptr, expected, maxMsgLen); // (result is never larger than this).
330 qsnprintf(msg, maxMsgLen, "%s\n Actual (%s)%*s %s\n Expected (%s)%*s %s",
331 failureMsg,
332 actual, qMax(len1, len2) - len1 + 1, ":", val1 ? val1 : "<null>",
333 expected, qMax(len1, len2) - len2 + 1, ":", val2 ? val2 : "<null>");
334}
335
336template <class Actual, class Expected>
337static bool compareHelper(bool success, const char *failureMsg,
338 const Actual &val1, const Expected &val2,
339 const char *actual, const char *expected,
340 const char *file, int line,
341 bool hasValues = true)
342{
343 const size_t maxMsgLen = 1024;
344 char msg[maxMsgLen] = {'\0'};
345
346 QTEST_ASSERT(expected);
347 QTEST_ASSERT(actual);
348
349 if (QTestLog::verboseLevel() >= 2) {
350 qsnprintf(msg, maxMsgLen, "QCOMPARE(%s, %s)", actual, expected);
351 QTestLog::info(msg, file, line);
352 }
353
354 if (!failureMsg)
355 failureMsg = "Compared values are not the same";
356
357 if (success) {
358 if (QTest::expectFailMode) {
359 qsnprintf(msg, maxMsgLen,
360 "QCOMPARE(%s, %s) returned TRUE unexpectedly.", actual, expected);
361 }
362 return checkStatement(success, msg, file, line);
363 }
364
365
366 if (!hasValues) {
367 qsnprintf(msg, maxMsgLen, "%s", failureMsg);
368 return checkStatement(success, msg, file, line);
369 }
370
371 formatFailMessage(msg, maxMsgLen, failureMsg, val1, val2, actual, expected);
372
373 return checkStatement(success, msg, file, line);
374}
375
376bool QTestResult::compare(bool success, const char *failureMsg,
377 char *val1, char *val2,
378 const char *actual, const char *expected,
379 const char *file, int line)
380{
381 const bool result = compareHelper(success, failureMsg,
382 val1 != nullptr ? val1 : "<null>",
383 val2 != nullptr ? val2 : "<null>",
384 actual, expected, file, line,
385 val1 != nullptr && val2 != nullptr);
386
387 // Our caller got these from QTest::toString()
388 delete [] val1;
389 delete [] val2;
390
391 return result;
392}
393
394bool QTestResult::compare(bool success, const char *failureMsg,
395 double val1, double val2,
396 const char *actual, const char *expected,
397 const char *file, int line)
398{
399 return compareHelper(success, failureMsg, val1, val2, actual, expected, file, line);
400}
401
402bool QTestResult::compare(bool success, const char *failureMsg,
403 float val1, float val2,
404 const char *actual, const char *expected,
405 const char *file, int line)
406{
407 return compareHelper(success, failureMsg, val1, val2, actual, expected, file, line);
408}
409
410bool QTestResult::compare(bool success, const char *failureMsg,
411 int val1, int val2,
412 const char *actual, const char *expected,
413 const char *file, int line)
414{
415 return compareHelper(success, failureMsg, val1, val2, actual, expected, file, line);
416}
417
418#if QT_POINTER_SIZE == 8
419bool QTestResult::compare(bool success, const char *failureMsg,
420 qsizetype val1, qsizetype val2,
421 const char *actual, const char *expected,
422 const char *file, int line)
423{
424 return compareHelper(success, failureMsg, val1, val2, actual, expected, file, line);
425}
426#endif // QT_POINTER_SIZE == 8
427
428bool QTestResult::compare(bool success, const char *failureMsg,
429 unsigned val1, unsigned val2,
430 const char *actual, const char *expected,
431 const char *file, int line)
432{
433 return compareHelper(success, failureMsg, val1, val2, actual, expected, file, line);
434}
435
436bool QTestResult::compare(bool success, const char *failureMsg,
437 QStringView val1, QStringView val2,
438 const char *actual, const char *expected,
439 const char *file, int line)
440{
441 return compareHelper(success, failureMsg, val1, val2, actual, expected, file, line);
442}
443
444bool QTestResult::compare(bool success, const char *failureMsg,
445 QStringView val1, const QLatin1String &val2,
446 const char *actual, const char *expected,
447 const char *file, int line)
448{
449 return compareHelper(success, failureMsg, val1, val2, actual, expected, file, line);
450}
451
452bool QTestResult::compare(bool success, const char *failureMsg,
453 const QLatin1String & val1, QStringView val2,
454 const char *actual, const char *expected,
455 const char *file, int line)
456{
457 return compareHelper(success, failureMsg, val1, val2, actual, expected, file, line);
458}
459
460void QTestResult::addFailure(const char *message, const char *file, int line)
461{
462 clearExpectFail();
463
464 if (QTest::blacklistCurrentTest)
465 QTestLog::addBFail(message, file, line);
466 else
467 QTestLog::addFail(message, file, line);
468 QTest::setFailed(true);
469}
470
471void QTestResult::addSkip(const char *message, const char *file, int line)
472{
473 clearExpectFail();
474
475 QTestLog::addSkip(message, file, line);
476}
477
478void QTestResult::setCurrentTestObject(const char *name)
479{
480 QTest::currentTestObjectName = name;
481}
482
483const char *QTestResult::currentTestObjectName()
484{
485 return QTest::currentTestObjectName ? QTest::currentTestObjectName : "";
486}
487
488void QTestResult::setSkipCurrentTest(bool value)
489{
490 QTest::skipCurrentTest = value;
491}
492
493bool QTestResult::skipCurrentTest()
494{
495 return QTest::skipCurrentTest;
496}
497
498void QTestResult::setCurrentAppName(const char *appName)
499{
500 ::currentAppName = appName;
501}
502
503const char *QTestResult::currentAppName()
504{
505 return ::currentAppName;
506}
507
508QT_END_NAMESPACE
509