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 
54 static const char *currentAppName = nullptr;
55 
56 QT_BEGIN_NAMESPACE
57 
58 namespace 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 
reset()72 void 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 
setBlacklistCurrentTest(bool b)87 void QTestResult::setBlacklistCurrentTest(bool b)
88 {
89     QTest::blacklistCurrentTest = b;
90 }
91 
currentTestFailed()92 bool QTestResult::currentTestFailed()
93 {
94     return QTest::failed;
95 }
96 
currentGlobalTestData()97 QTestData *QTestResult::currentGlobalTestData()
98 {
99     return QTest::currentGlobalTestData;
100 }
101 
currentTestData()102 QTestData *QTestResult::currentTestData()
103 {
104     return QTest::currentTestData;
105 }
106 
setCurrentGlobalTestData(QTestData * data)107 void QTestResult::setCurrentGlobalTestData(QTestData *data)
108 {
109     QTest::currentGlobalTestData = data;
110 }
111 
setCurrentTestData(QTestData * data)112 void QTestResult::setCurrentTestData(QTestData *data)
113 {
114     QTest::currentTestData = data;
115     QTest::failed = false;
116     if (data)
117         QTestLog::enterTestData(data);
118 }
119 
setCurrentTestFunction(const char * func)120 void QTestResult::setCurrentTestFunction(const char *func)
121 {
122     QTest::currentTestFunc = func;
123     QTest::failed = false;
124     if (func)
125         QTestLog::enterTestFunction(func);
126 }
127 
clearExpectFail()128 static void clearExpectFail()
129 {
130     QTest::expectFailMode = 0;
131     delete [] const_cast<char *>(QTest::expectFailComment);
132     QTest::expectFailComment = nullptr;
133 }
134 
finishedCurrentTestData()135 void QTestResult::finishedCurrentTestData()
136 {
137     if (QTest::expectFailMode)
138         addFailure("QEXPECT_FAIL was called without any subsequent verification statements", nullptr, 0);
139     clearExpectFail();
140 
141     if (!QTest::failed && QTestLog::unhandledIgnoreMessages()) {
142         QTestLog::printUnhandledIgnoreMessages();
143         addFailure("Not all expected messages were received", nullptr, 0);
144     }
145     QTestLog::clearIgnoreMessages();
146 }
147 
finishedCurrentTestDataCleanup()148 void 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("");
154         else
155             QTestLog::addPass("");
156     }
157 
158     QTest::failed = false;
159 }
160 
finishedCurrentTestFunction()161 void QTestResult::finishedCurrentTestFunction()
162 {
163     QTest::currentTestFunc = nullptr;
164     QTest::failed = false;
165 
166     QTestLog::leaveTestFunction();
167 }
168 
currentTestFunction()169 const char *QTestResult::currentTestFunction()
170 {
171     return QTest::currentTestFunc;
172 }
173 
currentDataTag()174 const char *QTestResult::currentDataTag()
175 {
176     return QTest::currentTestData ? QTest::currentTestData->dataTag() : nullptr;
177 }
178 
currentGlobalDataTag()179 const char *QTestResult::currentGlobalDataTag()
180 {
181     return QTest::currentGlobalTestData ? QTest::currentGlobalTestData->dataTag() : nullptr;
182 }
183 
isExpectFailData(const char * dataIndex)184 static 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(dataIndex, QTest::currentTestData->dataTag()) == 0)
191         return true;
192     return false;
193 }
194 
expectFail(const char * dataIndex,const char * comment,QTest::TestFailMode mode,const char * file,int line)195 bool 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("Already expecting a fail", file, line);
210         return false;
211     }
212 
213     QTest::expectFailMode = mode;
214     QTest::expectFailComment = comment;
215     return true;
216 }
217 
checkStatement(bool statement,const char * msg,const char * file,int line)218 static 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(QTest::expectFailComment, file, line);
238         else
239             QTestLog::addXFail(QTest::expectFailComment, file, line);
240         bool doContinue = (QTest::expectFailMode == QTest::Continue);
241         clearExpectFail();
242         return doContinue;
243     }
244 
245     QTestResult::addFailure(msg, file, line);
246     return false;
247 }
248 
verify(bool statement,const char * statementStr,const char * description,const char * file,int line)249 bool 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(msg, 1024, "QVERIFY(%s)", statementStr);
258         QTestLog::info(msg, file, line);
259     }
260 
261     if (!statement && !QTest::expectFailMode)
262         qsnprintf(msg, 1024, "'%s' returned FALSE. (%s)", statementStr, description ? description : "");
263     else if (statement && QTest::expectFailMode)
264         qsnprintf(msg, 1024, "'%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
270 template <class Actual, class Expected>
formatFailMessage(char * msg,size_t maxMsgLen,const char * failureMsg,const Actual & val1,const Expected & val2,const char * actual,const char * expected)271 void 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(nullptr, actual, maxMsgLen);    // Last parameter is not ignored on QNX
280     size_t len2 = mbstowcs(nullptr, expected, 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(len1, len2) - len1 + 1, ":", val1S ? val1S : "<null>",
284               expected, qMax(len1, 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().
formatFailMessage(char * msg,size_t maxMsgLen,const char * failureMsg,const char * val1,const char * val2,const char * actual,const char * expected)291 void 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(nullptr, actual, maxMsgLen);    // Last parameter is not ignored on QNX
297     size_t len2 = mbstowcs(nullptr, expected, maxMsgLen);  // (result is never larger than this).
298     qsnprintf(msg, maxMsgLen, "%s\n   Actual   (%s)%*s %s\n   Expected (%s)%*s %s",
299               failureMsg,
300               actual, qMax(len1, len2) - len1 + 1, ":", val1 ? val1 : "<null>",
301               expected, qMax(len1, len2) - len2 + 1, ":", val2 ? val2 : "<null>");
302 }
303 
304 template <class Actual, class Expected>
compareHelper(bool success,const char * failureMsg,const Actual & val1,const Expected & val2,const char * actual,const char * expected,const char * file,int line,bool hasValues=true)305 static 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(msg, maxMsgLen, "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(msg, maxMsgLen,
328                       "QCOMPARE(%s, %s) returned TRUE unexpectedly.", actual, expected);
329         }
330         return checkStatement(success, msg, file, line);
331     }
332 
333 
334     if (!hasValues) {
335         qsnprintf(msg, maxMsgLen, "%s", failureMsg);
336         return checkStatement(success, msg, file, line);
337     }
338 
339     formatFailMessage(msg, maxMsgLen, failureMsg, val1, val2, actual, expected);
340 
341     return checkStatement(success, msg, file, line);
342 }
343 
compare(bool success,const char * failureMsg,char * val1,char * val2,const char * actual,const char * expected,const char * file,int line)344 bool 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 != nullptr ? val1 : "<null>",
351                                       val2 != nullptr ? val2 : "<null>",
352                                       actual, expected, file, line,
353                                       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 
compare(bool success,const char * failureMsg,double val1,double val2,const char * actual,const char * expected,const char * file,int line)362 bool 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 
compare(bool success,const char * failureMsg,float val1,float val2,const char * actual,const char * expected,const char * file,int line)370 bool 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 
compare(bool success,const char * failureMsg,int val1,int val2,const char * actual,const char * expected,const char * file,int line)378 bool 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 
compare(bool success,const char * failureMsg,unsigned val1,unsigned val2,const char * actual,const char * expected,const char * file,int line)386 bool 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 
compare(bool success,const char * failureMsg,QStringView val1,QStringView val2,const char * actual,const char * expected,const char * file,int line)394 bool 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 
compare(bool success,const char * failureMsg,QStringView val1,const QLatin1String & val2,const char * actual,const char * expected,const char * file,int line)402 bool 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 
compare(bool success,const char * failureMsg,const QLatin1String & val1,QStringView val2,const char * actual,const char * expected,const char * file,int line)410 bool 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 
addFailure(const char * message,const char * file,int line)418 void QTestResult::addFailure(const char *message, const char *file, int line)
419 {
420     clearExpectFail();
421 
422     if (QTest::blacklistCurrentTest)
423         QTestLog::addBFail(message, file, line);
424     else
425         QTestLog::addFail(message, file, line);
426     QTest::failed = true;
427 }
428 
addSkip(const char * message,const char * file,int line)429 void QTestResult::addSkip(const char *message, const char *file, int line)
430 {
431     clearExpectFail();
432 
433     QTestLog::addSkip(message, file, line);
434 }
435 
setCurrentTestObject(const char * name)436 void QTestResult::setCurrentTestObject(const char *name)
437 {
438     QTest::currentTestObjectName = name;
439 }
440 
currentTestObjectName()441 const char *QTestResult::currentTestObjectName()
442 {
443     return QTest::currentTestObjectName ? QTest::currentTestObjectName : "";
444 }
445 
setSkipCurrentTest(bool value)446 void QTestResult::setSkipCurrentTest(bool value)
447 {
448     QTest::skipCurrentTest = value;
449 }
450 
skipCurrentTest()451 bool QTestResult::skipCurrentTest()
452 {
453     return QTest::skipCurrentTest;
454 }
455 
setCurrentAppName(const char * appName)456 void QTestResult::setCurrentAppName(const char *appName)
457 {
458     ::currentAppName = appName;
459 }
460 
currentAppName()461 const char *QTestResult::currentAppName()
462 {
463     return ::currentAppName;
464 }
465 
466 QT_END_NAMESPACE
467