1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2016 Intel Corporation.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtTest module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QTEST_H
42#define QTEST_H
43
44#include <QtTest/qttestglobal.h>
45#include <QtTest/qtestcase.h>
46#include <QtTest/qtestdata.h>
47#include <QtTest/qbenchmark.h>
48
49#include <QtCore/qbitarray.h>
50#include <QtCore/qbytearray.h>
51#include <QtCore/qstring.h>
52#include <QtCore/qstringlist.h>
53#include <QtCore/qcborcommon.h>
54#include <QtCore/qdatetime.h>
55#if QT_CONFIG(itemmodel)
56#include <QtCore/qabstractitemmodel.h>
57#endif
58#include <QtCore/qobject.h>
59#include <QtCore/qvariant.h>
60#include <QtCore/qurl.h>
61#include <QtCore/quuid.h>
62
63#include <QtCore/qpoint.h>
64#include <QtCore/qsize.h>
65#include <QtCore/qrect.h>
66
67#include <memory>
68
69QT_BEGIN_NAMESPACE
70
71
72namespace QTest
73{
74
75template <> inline char *toString(const QStringView &str)
76{
77 return QTest::toPrettyUnicode(string: str);
78}
79
80template<> inline char *toString(const QString &str)
81{
82 return toString(str: QStringView(str));
83}
84
85template<> inline char *toString(const QLatin1String &str)
86{
87 return toString(str: QString(str));
88}
89
90template<> inline char *toString(const QByteArray &ba)
91{
92 return QTest::toPrettyCString(unicode: ba.constData(), length: ba.length());
93}
94
95template<> inline char *toString(const QBitArray &ba)
96{
97 qsizetype size = ba.size();
98 char *str = new char[size + 1];
99 for (qsizetype i = 0; i < size; ++i)
100 str[i] = "01"[ba.testBit(i)];
101 str[size] = '\0';
102 return str;
103}
104
105#if QT_CONFIG(datestring)
106template<> inline char *toString(const QTime &time)
107{
108 return time.isValid()
109 ? qstrdup(qPrintable(time.toString(u"hh:mm:ss.zzz")))
110 : qstrdup("Invalid QTime");
111}
112
113template<> inline char *toString(const QDate &date)
114{
115 return date.isValid()
116 ? qstrdup(qPrintable(date.toString(u"yyyy/MM/dd")))
117 : qstrdup("Invalid QDate");
118}
119
120template<> inline char *toString(const QDateTime &dateTime)
121{
122 return dateTime.isValid()
123 ? qstrdup(qPrintable(dateTime.toString(u"yyyy/MM/dd hh:mm:ss.zzz[t]")))
124 : qstrdup("Invalid QDateTime");
125}
126#endif // datestring
127
128template<> inline char *toString(const QCborError &c)
129{
130 // use the Q_ENUM formatting
131 return toString(t: c.c);
132}
133
134template<> inline char *toString(const QChar &c)
135{
136 const ushort uc = c.unicode();
137 if (uc < 128) {
138 char msg[32] = {'\0'};
139 qsnprintf(str: msg, n: sizeof(msg), fmt: "QChar: '%c' (0x%x)", char(uc), unsigned(uc));
140 return qstrdup(msg);
141 }
142 return qstrdup(qPrintable(QString::fromLatin1("QChar: '%1' (0x%2)").arg(c).arg(QString::number(static_cast<int>(c.unicode()), 16))));
143}
144
145#if QT_CONFIG(itemmodel)
146template<> inline char *toString(const QModelIndex &idx)
147{
148 char msg[128];
149 qsnprintf(str: msg, n: sizeof(msg), fmt: "QModelIndex(%d,%d,%p,%p)", idx.row(), idx.column(), idx.internalPointer(), idx.model());
150 return qstrdup(msg);
151}
152#endif
153
154template<> inline char *toString(const QPoint &p)
155{
156 char msg[128] = {'\0'};
157 qsnprintf(str: msg, n: sizeof(msg), fmt: "QPoint(%d,%d)", p.x(), p.y());
158 return qstrdup(msg);
159}
160
161template<> inline char *toString(const QSize &s)
162{
163 char msg[128] = {'\0'};
164 qsnprintf(str: msg, n: sizeof(msg), fmt: "QSize(%dx%d)", s.width(), s.height());
165 return qstrdup(msg);
166}
167
168template<> inline char *toString(const QRect &s)
169{
170 char msg[256] = {'\0'};
171 qsnprintf(str: msg, n: sizeof(msg), fmt: "QRect(%d,%d %dx%d) (bottomright %d,%d)",
172 s.left(), s.top(), s.width(), s.height(), s.right(), s.bottom());
173 return qstrdup(msg);
174}
175
176template<> inline char *toString(const QPointF &p)
177{
178 char msg[64] = {'\0'};
179 qsnprintf(str: msg, n: sizeof(msg), fmt: "QPointF(%g,%g)", p.x(), p.y());
180 return qstrdup(msg);
181}
182
183template<> inline char *toString(const QSizeF &s)
184{
185 char msg[64] = {'\0'};
186 qsnprintf(str: msg, n: sizeof(msg), fmt: "QSizeF(%gx%g)", s.width(), s.height());
187 return qstrdup(msg);
188}
189
190template<> inline char *toString(const QRectF &s)
191{
192 char msg[256] = {'\0'};
193 qsnprintf(str: msg, n: sizeof(msg), fmt: "QRectF(%g,%g %gx%g) (bottomright %g,%g)",
194 s.left(), s.top(), s.width(), s.height(), s.right(), s.bottom());
195 return qstrdup(msg);
196}
197
198template<> inline char *toString(const QUrl &uri)
199{
200 if (!uri.isValid())
201 return qstrdup(qPrintable(QLatin1String("Invalid URL: ") + uri.errorString()));
202 return qstrdup(uri.toEncoded().constData());
203}
204
205template <> inline char *toString(const QUuid &uuid)
206{
207 return qstrdup(uuid.toByteArray().constData());
208}
209
210template<> inline char *toString(const QVariant &v)
211{
212 QByteArray vstring("QVariant(");
213 if (v.isValid()) {
214 QByteArray type(v.typeName());
215 if (type.isEmpty()) {
216 type = QByteArray::number(v.userType());
217 }
218 vstring.append(a: type);
219 if (!v.isNull()) {
220 vstring.append(c: ',');
221 if (v.canConvert(targetTypeId: QMetaType::QString)) {
222 vstring.append(a: v.toString().toLocal8Bit());
223 }
224 else {
225 vstring.append(s: "<value not representable as string>");
226 }
227 }
228 }
229 vstring.append(c: ')');
230
231 return qstrdup(vstring.constData());
232}
233
234template <typename T1, typename T2>
235inline char *toString(const QPair<T1, T2> &pair)
236{
237 const QScopedArrayPointer<char> first(toString(pair.first));
238 const QScopedArrayPointer<char> second(toString(pair.second));
239 return toString(str: QString::asprintf(format: "QPair(%s,%s)", first.data(), second.data()));
240}
241
242template <typename T1, typename T2>
243inline char *toString(const std::pair<T1, T2> &pair)
244{
245 const QScopedArrayPointer<char> first(toString(pair.first));
246 const QScopedArrayPointer<char> second(toString(pair.second));
247 return toString(str: QString::asprintf(format: "std::pair(%s,%s)", first.data(), second.data()));
248}
249
250template <typename Tuple, int... I>
251inline char *toString(const Tuple & tuple, QtPrivate::IndexesList<I...>) {
252 using UP = std::unique_ptr<char[]>;
253 // Generate a table of N + 1 elements where N is the number of
254 // elements in the tuple.
255 // The last element is needed to support the empty tuple use case.
256 const UP data[] = {
257 UP(toString(std::get<I>(tuple)))..., UP{}
258 };
259 return formatString("std::tuple(", ")", sizeof...(I), data[I].get()...);
260}
261
262template <class... Types>
263inline char *toString(const std::tuple<Types...> &tuple)
264{
265 static const std::size_t params_count = sizeof...(Types);
266 return toString(tuple, typename QtPrivate::Indexes<params_count>::Value());
267}
268
269inline char *toString(std::nullptr_t)
270{
271 return toString(str: QLatin1String("nullptr"));
272}
273
274template<>
275inline bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual,
276 const char *expected, const char *file, int line)
277{
278 return qCompare(t1, t2: QString(t2), actual, expected, file, line);
279}
280template<>
281inline bool qCompare(QLatin1String const &t1, QString const &t2, const char *actual,
282 const char *expected, const char *file, int line)
283{
284 return qCompare(t1: QString(t1), t2, actual, expected, file, line);
285}
286
287template <typename T>
288inline bool qCompare(QList<T> const &t1, QList<T> const &t2, const char *actual, const char *expected,
289 const char *file, int line)
290{
291 char msg[1024];
292 msg[0] = '\0';
293 bool isOk = true;
294 const int actualSize = t1.count();
295 const int expectedSize = t2.count();
296 if (actualSize != expectedSize) {
297 qsnprintf(str: msg, n: sizeof(msg), fmt: "Compared lists have different sizes.\n"
298 " Actual (%s) size: %d\n"
299 " Expected (%s) size: %d", actual, actualSize, expected, expectedSize);
300 isOk = false;
301 }
302 for (int i = 0; isOk && i < actualSize; ++i) {
303 if (!(t1.at(i) == t2.at(i))) {
304 char *val1 = toString(t1.at(i));
305 char *val2 = toString(t2.at(i));
306
307 qsnprintf(str: msg, n: sizeof(msg), fmt: "Compared lists differ at index %d.\n"
308 " Actual (%s): %s\n"
309 " Expected (%s): %s", i, actual, val1 ? val1 : "<null>",
310 expected, val2 ? val2 : "<null>");
311 isOk = false;
312
313 delete [] val1;
314 delete [] val2;
315 }
316 }
317 return compare_helper(success: isOk, failureMsg: msg, val1: nullptr, val2: nullptr, actual, expected, file, line);
318}
319
320template <>
321inline bool qCompare(QStringList const &t1, QStringList const &t2, const char *actual, const char *expected,
322 const char *file, int line)
323{
324 return qCompare<QString>(t1, t2, actual, expected, file, line);
325}
326
327template <typename T>
328inline bool qCompare(QFlags<T> const &t1, T const &t2, const char *actual, const char *expected,
329 const char *file, int line)
330{
331 return qCompare(t1: int(t1), t2: int(t2), actual, expected, file, line);
332}
333
334template <typename T>
335inline bool qCompare(QFlags<T> const &t1, int const &t2, const char *actual, const char *expected,
336 const char *file, int line)
337{
338 return qCompare(t1: int(t1), t2, actual, expected, file, line);
339}
340
341template<>
342inline bool qCompare(qint64 const &t1, qint32 const &t2, const char *actual,
343 const char *expected, const char *file, int line)
344{
345 return qCompare(t1, t2: static_cast<qint64>(t2), actual, expected, file, line);
346}
347
348template<>
349inline bool qCompare(qint64 const &t1, quint32 const &t2, const char *actual,
350 const char *expected, const char *file, int line)
351{
352 return qCompare(t1, t2: static_cast<qint64>(t2), actual, expected, file, line);
353}
354
355template<>
356inline bool qCompare(quint64 const &t1, quint32 const &t2, const char *actual,
357 const char *expected, const char *file, int line)
358{
359 return qCompare(t1, t2: static_cast<quint64>(t2), actual, expected, file, line);
360}
361
362template<>
363inline bool qCompare(qint32 const &t1, qint64 const &t2, const char *actual,
364 const char *expected, const char *file, int line)
365{
366 return qCompare(t1: static_cast<qint64>(t1), t2, actual, expected, file, line);
367}
368
369template<>
370inline bool qCompare(quint32 const &t1, qint64 const &t2, const char *actual,
371 const char *expected, const char *file, int line)
372{
373 return qCompare(t1: static_cast<qint64>(t1), t2, actual, expected, file, line);
374}
375
376template<>
377inline bool qCompare(quint32 const &t1, quint64 const &t2, const char *actual,
378 const char *expected, const char *file, int line)
379{
380 return qCompare(t1: static_cast<quint64>(t1), t2, actual, expected, file, line);
381}
382namespace Internal {
383
384template <typename T>
385class HasInitMain // SFINAE test for the presence of initMain()
386{
387private:
388 using YesType = char[1];
389 using NoType = char[2];
390
391 template <typename C> static YesType& test( decltype(&C::initMain) ) ;
392 template <typename C> static NoType& test(...);
393
394public:
395 enum { value = sizeof(test<T>(nullptr)) == sizeof(YesType) };
396};
397
398template<typename T>
399typename std::enable_if<HasInitMain<T>::value, void>::type callInitMain()
400{
401 T::initMain();
402}
403
404template<typename T>
405typename std::enable_if<!HasInitMain<T>::value, void>::type callInitMain()
406{
407}
408
409} // namespace Internal
410
411} // namespace QTest
412QT_END_NAMESPACE
413
414#ifdef QT_TESTCASE_BUILDDIR
415# define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__, QT_TESTCASE_BUILDDIR);
416#else
417# define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__);
418#endif
419
420// Hooks for coverage-testing of QTestLib itself:
421#if QT_CONFIG(testlib_selfcover) && defined(__COVERAGESCANNER__)
422struct QtCoverageScanner
423{
424 QtCoverageScanner(const char *name)
425 {
426 __coveragescanner_clear();
427 __coveragescanner_testname(name);
428 }
429 ~QtCoverageScanner()
430 {
431 __coveragescanner_save();
432 __coveragescanner_testname("");
433 }
434};
435#define TESTLIB_SELFCOVERAGE_START(name) QtCoverageScanner _qtCoverageScanner(name);
436#else
437#define TESTLIB_SELFCOVERAGE_START(name)
438#endif
439
440#define QTEST_APPLESS_MAIN(TestObject) \
441int main(int argc, char *argv[]) \
442{ \
443 TESTLIB_SELFCOVERAGE_START(TestObject) \
444 TestObject tc; \
445 QTEST_SET_MAIN_SOURCE_PATH \
446 return QTest::qExec(&tc, argc, argv); \
447}
448
449#include <QtTest/qtestsystem.h>
450
451// Two backwards-compatibility defines for an obsolete feature:
452#define QTEST_ADD_GPU_BLACKLIST_SUPPORT_DEFS
453#define QTEST_ADD_GPU_BLACKLIST_SUPPORT
454// ### Qt 6: fully remove these.
455
456#if defined(QT_NETWORK_LIB)
457# include <QtTest/qtest_network.h>
458#endif
459
460#if defined(QT_WIDGETS_LIB)
461
462#include <QtTest/qtest_widgets.h>
463
464#ifdef QT_KEYPAD_NAVIGATION
465# define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone);
466#else
467# define QTEST_DISABLE_KEYPAD_NAVIGATION
468#endif
469
470#define QTEST_MAIN_IMPL(TestObject) \
471 TESTLIB_SELFCOVERAGE_START(#TestObject) \
472 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>(); \
473 QApplication app(argc, argv); \
474 app.setAttribute(Qt::AA_Use96Dpi, true); \
475 QTEST_DISABLE_KEYPAD_NAVIGATION \
476 TestObject tc; \
477 QTEST_SET_MAIN_SOURCE_PATH \
478 return QTest::qExec(&tc, argc, argv);
479
480#elif defined(QT_GUI_LIB)
481
482#include <QtTest/qtest_gui.h>
483
484#define QTEST_MAIN_IMPL(TestObject) \
485 TESTLIB_SELFCOVERAGE_START(#TestObject) \
486 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>(); \
487 QGuiApplication app(argc, argv); \
488 app.setAttribute(Qt::AA_Use96Dpi, true); \
489 TestObject tc; \
490 QTEST_SET_MAIN_SOURCE_PATH \
491 return QTest::qExec(&tc, argc, argv);
492
493#else
494
495#define QTEST_MAIN_IMPL(TestObject) \
496 TESTLIB_SELFCOVERAGE_START(#TestObject) \
497 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>(); \
498 QCoreApplication app(argc, argv); \
499 app.setAttribute(Qt::AA_Use96Dpi, true); \
500 TestObject tc; \
501 QTEST_SET_MAIN_SOURCE_PATH \
502 return QTest::qExec(&tc, argc, argv);
503
504#endif // QT_GUI_LIB
505
506#define QTEST_MAIN(TestObject) \
507int main(int argc, char *argv[]) \
508{ \
509 QTEST_MAIN_IMPL(TestObject) \
510}
511
512#define QTEST_GUILESS_MAIN(TestObject) \
513int main(int argc, char *argv[]) \
514{ \
515 TESTLIB_SELFCOVERAGE_START(#TestObject) \
516 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>(); \
517 QCoreApplication app(argc, argv); \
518 app.setAttribute(Qt::AA_Use96Dpi, true); \
519 TestObject tc; \
520 QTEST_SET_MAIN_SOURCE_PATH \
521 return QTest::qExec(&tc, argc, argv); \
522}
523
524#endif
525

source code of qtbase/src/testlib/qtest.h