Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_qtestlib_qtestcase.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3#include <QTest>
4#include <QSqlDatabase>
5#include <QFontDatabase>
6#include <QtCore/qatomicscopedvaluerollback.h>
7
8#include <initializer_list>
9
10using namespace Qt::StringLiterals;
11
12// dummy
13class TestBenchmark : public QObject
14{
16private slots:
17 void simple();
18};
19
20// dummy
21class MyTestClass : public QObject
22{
23 public:
24 void cleanup();
27 void addDataRow();
28
29 private Q_SLOTS:
30 void initTestCase();
31 void defaultTryTimeout();
32};
33
34void MyTestClass::initTestCase()
35{
36//! [set defaultTryTimeout]
37 using namespace std::chrono_literals;
38 // Since the atomic itself (defaultTryTimeout) is the only data,
39 // all reads and stores can be relaxed.
40 QTest::defaultTryTimeout.store(1s, std::memory_order_relaxed);
41//! [set defaultTryTimeout]
42}
43
44// dummy
46{
47};
48
49class TestQString : public QObject
50{
51 public:
52 void toInt_data();
53 void toInt();
54 void toUpper();
55 void Compare();
56};
57
59{
60//! [1]
61QVERIFY2(QFileInfo("file.txt").exists(), "file.txt does not exist.");
62//! [1]
63
64//! [2]
65QCOMPARE(QString("hello").toUpper(), QString("HELLO"));
66//! [2]
67}
68
69//! [3]
71{
72 QTest::addColumn<QString>("aString");
73 QTest::addColumn<int>("expected");
74
75 QTest::newRow("positive+value") << "42" << 42;
76 QTest::newRow("negative-value") << "-42" << -42;
77 QTest::newRow("zero") << "0" << 0;
78}
79//! [3]
80
81//! [4]
83{
84 QFETCH(QString, aString);
85 QFETCH(int, expected);
86
87 QCOMPARE(aString.toInt(), expected);
88}
89//! [4]
90
91void testInt()
92{
93// dummy
94int i = 0, j = 0;
95//! [5]
96if (sizeof(int) != 4)
97 QFAIL("This test has not been ported to this platform yet.");
98//! [5]
99
100//! [6]
101QFETCH(QString, myString);
102QCOMPARE(QString("hello").toUpper(), myString);
103//! [6]
104
105//! [7]
106QTEST(QString("hello").toUpper(), "myString");
107//! [7]
108
109//! [8]
110if (!QSqlDatabase::drivers().contains("SQLITE"))
111 QSKIP("This test requires the SQLITE database driver");
112//! [8]
113
114//! [9]
115QEXPECT_FAIL("", "Will fix in the next release", Continue);
116QCOMPARE(i, 42);
117QCOMPARE(j, 43);
118//! [9]
119
120//! [10]
121QEXPECT_FAIL("data27", "Oh my, this is soooo broken", Abort);
122QCOMPARE(i, 42);
123//! [10]
124}
125
126//! [11]
127QTEST_MAIN(TestQString)
128//! [11]
129
131{
132 class MyTestObject : public QObject
133 {
134 };
135//! [18]
136MyTestObject test1;
137QTest::qExec(&test1);
138//! [18]
139}
140
142{
143//! [19]
144QDir dir;
145QTest::ignoreMessage(QtWarningMsg, "QDir::mkdir: Empty or null file name(s)");
146dir.mkdir("");
147//! [19]
148}
149
150//! [20]
152{
153 QTest::addColumn<QString>("aString");
154 QTest::newRow("just.hello") << QString("hello");
155 QTest::newRow("a.null.string") << QString();
156}
157//! [20]
158
160{
161//! [addRow]
162 QTest::addColumn<int>("input");
163 QTest::addColumn<QString>("output");
164 QTest::addRow("%d", 0) << 0 << QString("0");
165 QTest::addRow("%d", 1) << 1 << QString("1");
166//! [addRow]
167}
168
170{
171//! [21]
172 QTest::addColumn<int>("intval");
173 QTest::addColumn<QString>("str");
174 QTest::addColumn<double>("dbl");
175 QTest::newRow("row1") << 1 << "hello" << 1.5;
176//! [21]
177}
178
179//! [22]
181{
182 if (qstrcmp(QTest::currentTestFunction(), "myDatabaseTest") == 0) {
183 // clean up all database connections
185 }
186}
187//! [22]
188
190{
191//! [23]
192using namespace std::chrono_literals;
193QTest::qSleep(250ms);
194//! [23]
195}
196
197//! [27]
198void TestBenchmark::simple()
199{
200 QString str1 = u"This is a test string"_s;
201 QString str2 = u"This is a test string"_s;
202 QCOMPARE(str1.localeAwareCompare(str2), 0);
203 QBENCHMARK {
204 str1.localeAwareCompare(str2);
205 }
206}
207//! [27]
208
210{
211QFile file;
212//! [32]
213bool opened = file.open(QIODevice::WriteOnly);
214QVERIFY(opened);
215//! [32]
216//! [33]
217QVERIFY2(file.open(QIODevice::WriteOnly),
218 qPrintable(QString("open %1: %2")
219 .arg(file.fileName()).arg(file.errorString())));
220//! [33]
221}
222
224{
225//! [34]
226 const int expected[] = {8, 10, 12, 16, 20, 24};
227 QCOMPARE(QFontDatabase::standardSizes(), expected);
228//! [34]
229}
230
232{
233//! [35]
234 #define ARG(...) __VA_ARGS__
235 QCOMPARE(QFontDatabase::standardSizes(), ARG({8, 10, 12, 16, 20, 24}));
236 #undef ARG
237//! [35]
238}
239
241{
242//! [36]
243const auto restoreDefaultLocale = qScopeGuard([prior = QLocale()]() {
244 QLocale::setDefault(prior);
245});
246//! [36]
247QLocale::setDefault(QLocale::c());
248}
249
250void MyTestClass::defaultTryTimeout()
251{
252 using namespace std::chrono_literals;
253
254//! [rollback defaultTryTimeout]
255 const auto timeoutRollback = QAtomicScopedValueRollback(
256 QTest::defaultTryTimeout, 1s, std::memory_order_relaxed);
257//! [rollback defaultTryTimeout]
258
259//! [get defaultTryTimeout]
260 // Since the atomic itself is all the data, all reads and stores can be relaxed.
261 QCOMPARE(QTest::defaultTryTimeout.load(std::memory_order_relaxed), 1s);
262//! [get defaultTryTimeout]
263}
void wrapInFunction()
[16]
void compareListToInitializerList()
void closeAllDatabases()
void quarterSecondSleep()
[22]
#define ARG(...)
void tstQDir()
void testObject()
[11]
void compareListToArray()
void withRestoredDefaultLocale()
void verifyString()
[27]
void testInt()
[4]