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

source code of qtbase/src/testlib/qtestresult.cpp