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{
23public:
24 void cleanup();
27 void addDataRow();
28 void scopeGuardReport(const QLocale &locale, const QStringList &expected) const;
29
30private Q_SLOTS:
31 void initTestCase();
32 void defaultTryTimeout();
33};
34
35void MyTestClass::initTestCase()
36{
37//! [set defaultTryTimeout]
38 using namespace std::chrono_literals;
39 // Since the atomic itself (defaultTryTimeout) is the only data,
40 // all reads and stores can be relaxed.
41 QTest::defaultTryTimeout.store(1s, std::memory_order_relaxed);
42//! [set defaultTryTimeout]
43}
44
45// dummy
47{
48};
49
50class TestQString : public QObject
51{
52 public:
53 void toInt_data();
54 void toInt();
55 void toUpper();
56 void Compare();
57};
58
60{
61//! [1]
62QVERIFY2(QFileInfo("file.txt").exists(), "file.txt does not exist.");
63//! [1]
64
65//! [2]
66QCOMPARE(QString("hello").toUpper(), QString("HELLO"));
67//! [2]
68}
69
70//! [3]
72{
73 QTest::addColumn<QString>("aString");
74 QTest::addColumn<int>("expected");
75
76 QTest::newRow("positive+value") << "42" << 42;
77 QTest::newRow("negative-value") << "-42" << -42;
78 QTest::newRow("zero") << "0" << 0;
79}
80//! [3]
81
82//! [4]
84{
85 QFETCH(QString, aString);
86 QFETCH(int, expected);
87
88 QCOMPARE(aString.toInt(), expected);
89}
90//! [4]
91
92void testInt()
93{
94// dummy
95int i = 0, j = 0;
96//! [5]
97if (sizeof(int) != 4)
98 QFAIL("This test has not been ported to this platform yet.");
99//! [5]
100
101//! [6]
102QFETCH(QString, myString);
103QCOMPARE(QString("hello").toUpper(), myString);
104//! [6]
105
106//! [7]
107QTEST(QString("hello").toUpper(), "myString");
108//! [7]
109
110//! [8]
111if (!QSqlDatabase::drivers().contains("SQLITE"))
112 QSKIP("This test requires the SQLITE database driver");
113//! [8]
114
115//! [9]
116QEXPECT_FAIL("", "Will fix in the next release", Continue);
117QCOMPARE(i, 42);
118QCOMPARE(j, 43);
119//! [9]
120
121//! [10]
122QEXPECT_FAIL("data27", "Oh my, this is soooo broken", Abort);
123QCOMPARE(i, 42);
124//! [10]
125}
126
127//! [11]
128QTEST_MAIN(TestQString)
129//! [11]
130
132{
133 class MyTestObject : public QObject
134 {
135 };
136//! [18]
137MyTestObject test1;
138QTest::qExec(&test1);
139//! [18]
140}
141
143{
144//! [19]
145QDir dir;
146QTest::ignoreMessage(QtWarningMsg, "QDir::mkdir: Empty or null file name(s)");
147dir.mkdir("");
148//! [19]
149}
150
151//! [20]
153{
154 QTest::addColumn<QString>("aString");
155 QTest::newRow("just.hello") << QString("hello");
156 QTest::newRow("a.null.string") << QString();
157}
158//! [20]
159
161{
162//! [addRow]
163 QTest::addColumn<int>("input");
164 QTest::addColumn<QString>("output");
165 QTest::addRow("%d", 0) << 0 << QString("0");
166 QTest::addRow("%d", 1) << 1 << QString("1");
167//! [addRow]
168}
169
171{
172//! [21]
173 QTest::addColumn<int>("intval");
174 QTest::addColumn<QString>("str");
175 QTest::addColumn<double>("dbl");
176 QTest::newRow("row1") << 1 << "hello" << 1.5;
177//! [21]
178}
179
180//! [22]
182{
183 if (qstrcmp(QTest::currentTestFunction(), "myDatabaseTest") == 0) {
184 // clean up all database connections
186 }
187}
188//! [22]
189
191{
192//! [23]
193using namespace std::chrono_literals;
194QTest::qSleep(250ms);
195//! [23]
196}
197
198//! [27]
199void TestBenchmark::simple()
200{
201 QString str1 = u"This is a test string"_s;
202 QString str2 = u"This is a test string"_s;
203 QCOMPARE(str1.localeAwareCompare(str2), 0);
204 QBENCHMARK {
205 str1.localeAwareCompare(str2);
206 }
207}
208//! [27]
209
211{
212QFile file;
213//! [32]
214bool opened = file.open(QIODevice::WriteOnly);
215QVERIFY(opened);
216//! [32]
217//! [33]
218QVERIFY2(file.open(QIODevice::WriteOnly),
219 qPrintable(QString("open %1: %2")
220 .arg(file.fileName()).arg(file.errorString())));
221//! [33]
222}
223
225{
226//! [34]
227 const int expected[] = {8, 10, 12, 16, 20, 24};
228 QCOMPARE(QFontDatabase::standardSizes(), expected);
229//! [34]
230}
231
233{
234//! [35]
235 #define ARG(...) __VA_ARGS__
236 QCOMPARE(QFontDatabase::standardSizes(), ARG({8, 10, 12, 16, 20, 24}));
237 #undef ARG
238//! [35]
239}
240
242{
243//! [36]
244const auto restoreDefaultLocale = qScopeGuard([prior = QLocale()]() {
245 QLocale::setDefault(prior);
246});
247//! [36]
248QLocale::setDefault(QLocale::c());
249}
250
251void MyTestClass::scopeGuardReport(const QLocale &locale, const QStringList &expected) const
252{
253//! [scope-diagnostic]
254 const QStringList actual = locale.uiLanguages();
255 auto report = qScopeGuard([&actual]() {
256 qDebug("\n\t%ls", qUtf16Printable(actual.join("\n\t"_L1)));
257 });
258 QCOMPARE(actual, expected);
259 report.dismiss();
260//! [scope-diagnostic]
261}
262
263void MyTestClass::defaultTryTimeout()
264{
265 using namespace std::chrono_literals;
266
267//! [rollback defaultTryTimeout]
268 const auto timeoutRollback = QAtomicScopedValueRollback(
269 QTest::defaultTryTimeout, 1s, std::memory_order_relaxed);
270//! [rollback defaultTryTimeout]
271
272//! [get defaultTryTimeout]
273 // Since the atomic itself is all the data, all reads and stores can be relaxed.
274 QCOMPARE(QTest::defaultTryTimeout.load(std::memory_order_relaxed), 1s);
275//! [get defaultTryTimeout]
276}
void scopeGuardReport(const QLocale &locale, const QStringList &expected) const
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]