Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
10
using
namespace
Qt::StringLiterals;
11
12
// dummy
13
class
TestBenchmark
:
public
QObject
14
{
15
Q_OBJECT
16
private
slots
:
17
void
simple
();
18
};
19
20
// dummy
21
class
MyTestClass
:
public
QObject
22
{
23
public
:
24
void
cleanup
();
25
void
addSingleStringRows
();
26
void
addMultStringRows
();
27
void
addDataRow
();
28
void
scopeGuardReport
(
const
QLocale &locale,
const
QStringList &expected)
const
;
29
30
private
Q_SLOTS
:
31
void
initTestCase
();
32
void
defaultTryTimeout();
33
};
34
35
void
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
46
void
closeAllDatabases
()
47
{
48
};
49
50
class
TestQString
:
public
QObject
51
{
52
public
:
53
void
toInt_data
();
54
void
toInt
();
55
void
toUpper
();
56
void
Compare
();
57
};
58
59
void
wrapInFunction
()
60
{
61
//! [1]
62
QVERIFY2(QFileInfo(
"file.txt"
).exists(),
"file.txt does not exist."
);
63
//! [1]
64
65
//! [2]
66
QCOMPARE(QString(
"hello"
).toUpper(), QString(
"HELLO"
));
67
//! [2]
68
}
69
70
//! [3]
71
void
TestQString
::
toInt_data
()
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]
83
void
TestQString
::
toInt
()
84
{
85
QFETCH(QString, aString);
86
QFETCH(
int
, expected);
87
88
QCOMPARE(aString.toInt(), expected);
89
}
90
//! [4]
91
92
void
testInt
()
93
{
94
// dummy
95
int
i = 0, j = 0;
96
//! [5]
97
if
(
sizeof
(
int
) != 4)
98
QFAIL(
"This test has not been ported to this platform yet."
);
99
//! [5]
100
101
//! [6]
102
QFETCH(QString, myString);
103
QCOMPARE(QString(
"hello"
).toUpper(), myString);
104
//! [6]
105
106
//! [7]
107
QTEST(QString(
"hello"
).toUpper(),
"myString"
);
108
//! [7]
109
110
//! [8]
111
if
(!QSqlDatabase::drivers().contains(
"SQLITE"
))
112
QSKIP(
"This test requires the SQLITE database driver"
);
113
//! [8]
114
115
//! [9]
116
QEXPECT_FAIL(
""
,
"Will fix in the next release"
, Continue);
117
QCOMPARE(i, 42);
118
QCOMPARE(j, 43);
119
//! [9]
120
121
//! [10]
122
QEXPECT_FAIL(
"data27"
,
"Oh my, this is soooo broken"
, Abort);
123
QCOMPARE(i, 42);
124
//! [10]
125
}
126
127
//! [11]
128
QTEST_MAIN(TestQString)
129
//! [11]
130
131
void
testObject
()
132
{
133
class
MyTestObject :
public
QObject
134
{
135
};
136
//! [18]
137
MyTestObject test1;
138
QTest::qExec(&test1);
139
//! [18]
140
}
141
142
void
tstQDir
()
143
{
144
//! [19]
145
QDir dir;
146
QTest::ignoreMessage(QtWarningMsg,
"QDir::mkdir: Empty or null file name(s)"
);
147
dir.mkdir(
""
);
148
//! [19]
149
}
150
151
//! [20]
152
void
MyTestClass
::
addSingleStringRows
()
153
{
154
QTest::addColumn<QString>(
"aString"
);
155
QTest::newRow(
"just.hello"
) << QString(
"hello"
);
156
QTest::newRow(
"a.null.string"
) << QString();
157
}
158
//! [20]
159
160
void
MyTestClass
::
addMultStringRows
()
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
170
void
MyTestClass
::
addDataRow
()
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]
181
void
MyTestClass
::
cleanup
()
182
{
183
if
(qstrcmp(QTest::currentTestFunction(),
"myDatabaseTest"
) == 0) {
184
// clean up all database connections
185
closeAllDatabases
(
)
;
186
}
187
}
188
//! [22]
189
190
void
quarterSecondSleep
()
191
{
192
//! [23]
193
using
namespace
std::chrono_literals;
194
QTest::qSleep(250ms);
195
//! [23]
196
}
197
198
//! [27]
199
void
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
210
void
verifyString
()
211
{
212
QFile file;
213
//! [32]
214
bool
opened = file.open(QIODevice::WriteOnly);
215
QVERIFY(opened);
216
//! [32]
217
//! [33]
218
QVERIFY2(file.open(QIODevice::WriteOnly),
219
qPrintable(QString(
"open %1: %2"
)
220
.arg(file.fileName()).arg(file.errorString())));
221
//! [33]
222
}
223
224
void
compareListToArray
()
225
{
226
//! [34]
227
const
int
expected[] = {8, 10, 12, 16, 20, 24};
228
QCOMPARE(QFontDatabase::standardSizes(), expected);
229
//! [34]
230
}
231
232
void
compareListToInitializerList
()
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
241
void
withRestoredDefaultLocale
()
242
{
243
//! [36]
244
const
auto
restoreDefaultLocale = qScopeGuard([prior = QLocale()]() {
245
QLocale::setDefault(prior);
246
});
247
//! [36]
248
QLocale::setDefault(QLocale::c());
249
}
250
251
void
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
263
void
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
}
MyTestClass
Definition
src_qtestlib_qtestcase.cpp:22
MyTestClass::scopeGuardReport
void scopeGuardReport(const QLocale &locale, const QStringList &expected) const
Definition
src_qtestlib_qtestcase.cpp:251
MyTestClass::addDataRow
void addDataRow()
Definition
src_qtestlib_qtestcase.cpp:170
MyTestClass::addSingleStringRows
void addSingleStringRows()
[20]
Definition
src_qtestlib_qtestcase.cpp:152
MyTestClass::cleanup
void cleanup()
[22]
Definition
src_qtestlib_qtestcase.cpp:181
MyTestClass::addMultStringRows
void addMultStringRows()
[20]
Definition
src_qtestlib_qtestcase.cpp:160
TestBenchmark
Definition
src_qtestlib_qtestcase.cpp:14
TestQString
Definition
src_qtestlib_qtestcase.cpp:51
TestQString::toInt
void toInt()
[3]
Definition
src_qtestlib_qtestcase.cpp:83
TestQString::toInt_data
void toInt_data()
[3]
Definition
src_qtestlib_qtestcase.cpp:71
TestQString::toUpper
void toUpper()
[0]
Definition
doc_src_qtestlib.cpp:44
TestQString::Compare
void Compare()
[8]
Definition
doc_src_qtestlib.cpp:51
wrapInFunction
void wrapInFunction()
[16]
Definition
src_corelib_io_qdatastream.cpp:6
compareListToInitializerList
void compareListToInitializerList()
Definition
src_qtestlib_qtestcase.cpp:232
closeAllDatabases
void closeAllDatabases()
Definition
src_qtestlib_qtestcase.cpp:46
quarterSecondSleep
void quarterSecondSleep()
[22]
Definition
src_qtestlib_qtestcase.cpp:190
ARG
#define ARG(...)
tstQDir
void tstQDir()
Definition
src_qtestlib_qtestcase.cpp:142
testObject
void testObject()
[11]
Definition
src_qtestlib_qtestcase.cpp:131
compareListToArray
void compareListToArray()
Definition
src_qtestlib_qtestcase.cpp:224
withRestoredDefaultLocale
void withRestoredDefaultLocale()
Definition
src_qtestlib_qtestcase.cpp:241
verifyString
void verifyString()
[27]
Definition
src_qtestlib_qtestcase.cpp:210
testInt
void testInt()
[4]
Definition
src_qtestlib_qtestcase.cpp:92
qtbase
src
testlib
doc
snippets
code
src_qtestlib_qtestcase.cpp
Generated on
for Qt by
1.18.0