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
7#include <initializer_list>
8
9using namespace Qt::StringLiterals;
10
11// dummy
12class TestBenchmark : public QObject
13{
14 Q_OBJECT
15private slots:
16 void simple();
17};
18
19// dummy
20class MyTestClass : public QObject
21{
22 public:
23 void cleanup();
24 void addSingleStringRows();
25 void addMultStringRows();
26 void addDataRow();
27};
28// dummy
29void closeAllDatabases()
30{
31};
32
33class TestQString : public QObject
34{
35 public:
36 void toInt_data();
37 void toInt();
38 void toUpper();
39 void Compare();
40};
41
42void wrapInFunction()
43{
44//! [1]
45QVERIFY2(QFileInfo("file.txt").exists(), "file.txt does not exist.");
46//! [1]
47
48//! [2]
49QCOMPARE(QString("hello").toUpper(), QString("HELLO"));
50//! [2]
51}
52
53//! [3]
54void TestQString::toInt_data()
55{
56 QTest::addColumn<QString>(name: "aString");
57 QTest::addColumn<int>(name: "expected");
58
59 QTest::newRow(dataTag: "positive+value") << "42" << 42;
60 QTest::newRow(dataTag: "negative-value") << "-42" << -42;
61 QTest::newRow(dataTag: "zero") << "0" << 0;
62}
63//! [3]
64
65//! [4]
66void TestQString::toInt()
67{
68 QFETCH(QString, aString);
69 QFETCH(int, expected);
70
71 QCOMPARE(aString.toInt(), expected);
72}
73//! [4]
74
75void testInt()
76{
77// dummy
78int i = 0, j = 0;
79//! [5]
80if (sizeof(int) != 4)
81 QFAIL("This test has not been ported to this platform yet.");
82//! [5]
83
84//! [6]
85QFETCH(QString, myString);
86QCOMPARE(QString("hello").toUpper(), myString);
87//! [6]
88
89//! [7]
90QTEST(QString("hello").toUpper(), "myString");
91//! [7]
92
93//! [8]
94if (!QSqlDatabase::drivers().contains(t: "SQLITE"))
95 QSKIP("This test requires the SQLITE database driver");
96//! [8]
97
98//! [9]
99QEXPECT_FAIL("", "Will fix in the next release", Continue);
100QCOMPARE(i, 42);
101QCOMPARE(j, 43);
102//! [9]
103
104//! [10]
105QEXPECT_FAIL("data27", "Oh my, this is soooo broken", Abort);
106QCOMPARE(i, 42);
107//! [10]
108}
109
110//! [11]
111QTEST_MAIN(TestQString)
112//! [11]
113
114void testObject()
115{
116 class MyTestObject : public QObject
117 {
118 };
119//! [18]
120MyTestObject test1;
121QTest::qExec(testObject: &test1);
122//! [18]
123}
124
125void tstQDir()
126{
127//! [19]
128QDir dir;
129QTest::ignoreMessage(type: QtWarningMsg, message: "QDir::mkdir: Empty or null file name(s)");
130dir.mkdir(dirName: "");
131//! [19]
132}
133
134//! [20]
135void MyTestClass::addSingleStringRows()
136{
137 QTest::addColumn<QString>(name: "aString");
138 QTest::newRow(dataTag: "just.hello") << QString("hello");
139 QTest::newRow(dataTag: "a.null.string") << QString();
140}
141//! [20]
142
143void MyTestClass::addMultStringRows()
144{
145//! [addRow]
146 QTest::addColumn<int>(name: "input");
147 QTest::addColumn<QString>(name: "output");
148 QTest::addRow(format: "%d", 0) << 0 << QString("0");
149 QTest::addRow(format: "%d", 1) << 1 << QString("1");
150//! [addRow]
151}
152
153void MyTestClass::addDataRow()
154{
155//! [21]
156 QTest::addColumn<int>(name: "intval");
157 QTest::addColumn<QString>(name: "str");
158 QTest::addColumn<double>(name: "dbl");
159 QTest::newRow(dataTag: "row1") << 1 << "hello" << 1.5;
160//! [21]
161}
162
163//! [22]
164void MyTestClass::cleanup()
165{
166 if (qstrcmp(str1: QTest::currentTestFunction(), str2: "myDatabaseTest") == 0) {
167 // clean up all database connections
168 closeAllDatabases();
169 }
170}
171//! [22]
172
173void mySleep()
174{
175//! [23]
176QTest::qSleep(ms: 250);
177//! [23]
178}
179
180//! [27]
181void TestBenchmark::simple()
182{
183 QString str1 = u"This is a test string"_s;
184 QString str2 = u"This is a test string"_s;
185 QCOMPARE(str1.localeAwareCompare(str2), 0);
186 QBENCHMARK {
187 str1.localeAwareCompare(s: str2);
188 }
189}
190//! [27]
191
192void verifyString()
193{
194QFile file;
195//! [32]
196bool opened = file.open(flags: QIODevice::WriteOnly);
197QVERIFY(opened);
198//! [32]
199//! [33]
200QVERIFY2(file.open(QIODevice::WriteOnly),
201 qPrintable(QString("open %1: %2")
202 .arg(file.fileName()).arg(file.errorString())));
203//! [33]
204}
205
206void compareListToArray()
207{
208//! [34]
209 const int expected[] = {8, 10, 12, 16, 20, 24};
210 QCOMPARE(QFontDatabase::standardSizes(), expected);
211//! [34]
212}
213
214void compareListToInitializerList()
215{
216//! [35]
217 #define ARG(...) __VA_ARGS__
218 QCOMPARE(QFontDatabase::standardSizes(), ARG({8, 10, 12, 16, 20, 24}));
219 #undef ARG
220//! [35]
221}
222

source code of qtbase/src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp