QtBase  v6.3.1
src_qtestlib_qtestcase.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 #include <QTest>
51 #include <QSqlDatabase>
52 #include <QFontDatabase>
53 
54 #include <initializer_list>
55 
56 // dummy
57 class TestBenchmark : public QObject
58 {
59  Q_OBJECT
60 private slots:
61  void simple();
62 };
63 
64 // dummy
65 class MyTestClass : public QObject
66 {
67  public:
68  void cleanup();
69  void addSingleStringRows();
70  void addMultStringRows();
71  void addDataRow();
72 };
73 // dummy
75 {
76 };
77 
78 class TestQString : public QObject
79 {
80  public:
81  void toInt_data();
82  void toInt();
83  void toUpper();
84  void Compare();
85 };
86 
88 {
90 QVERIFY2(QFileInfo("file.txt").exists(), "file.txt does not exist.");
92 
94 QCOMPARE(QString("hello").toUpper(), QString("HELLO"));
96 }
97 
100 {
101  QTest::addColumn<QString>("aString");
102  QTest::addColumn<int>("expected");
103 
104  QTest::newRow("positive value") << "42" << 42;
105  QTest::newRow("negative value") << "-42" << -42;
106  QTest::newRow("zero") << "0" << 0;
107 }
109 
112 {
113  QFETCH(QString, aString);
114  QFETCH(int, expected);
115 
116  QCOMPARE(aString.toInt(), expected);
117 }
119 
120 void testInt()
121 {
122 // dummy
123 int i = 0, j = 0;
125 if (sizeof(int) != 4)
126  QFAIL("This test has not been ported to this platform yet.");
128 
130 QFETCH(QString, myString);
131 QCOMPARE(QString("hello").toUpper(), myString);
133 
135 QTEST(QString("hello").toUpper(), "myString");
137 
139 if (!QSqlDatabase::drivers().contains("SQLITE"))
140  QSKIP("This test requires the SQLITE database driver");
142 
144 QEXPECT_FAIL("", "Will fix in the next release", Continue);
145 QCOMPARE(i, 42);
146 QCOMPARE(j, 43);
148 
150 QEXPECT_FAIL("data27", "Oh my, this is soooo broken", Abort);
151 QCOMPARE(i, 42);
153 }
154 
158 
160 {
161  class MyTestObject : public QObject
162  {
163  };
165 MyTestObject test1;
166 QTest::qExec(&test1);
168 }
169 
170 void tstQDir()
171 {
173 QDir dir;
174 QTest::ignoreMessage(QtWarningMsg, "QDir::mkdir: Empty or null file name(s)");
175 dir.mkdir("");
177 }
178 
181 {
182  QTest::addColumn<QString>("aString");
183  QTest::newRow("just hello") << QString("hello");
184  QTest::newRow("a null string") << QString();
185 }
187 
189 {
191  QTest::addColumn<int>("input");
192  QTest::addColumn<QString>("output");
193  QTest::addRow("%d", 0) << 0 << QString("0");
194  QTest::addRow("%d", 1) << 1 << QString("1");
196 }
197 
199 {
201  QTest::addColumn<int>("intval");
202  QTest::addColumn<QString>("str");
203  QTest::addColumn<double>("dbl");
204  QTest::newRow("row1") << 1 << "hello" << 1.5;
206 }
207 
210 {
211  if (qstrcmp(QTest::currentTestFunction(), "myDatabaseTest") == 0) {
212  // clean up all database connections
214  }
215 }
217 
218 void mySleep()
219 {
221 QTest::qSleep(250);
223 }
224 
226 void TestBenchmark::simple()
227 {
228  QString str1 = QLatin1String("This is a test string");
229  QString str2 = QLatin1String("This is a test string");
230  QCOMPARE(str1.localeAwareCompare(str2), 0);
231  QBENCHMARK {
232  str1.localeAwareCompare(str2);
233  }
234 }
236 
238 {
239 QFile file;
241 bool opened = file.open(QIODevice::WriteOnly);
242 QVERIFY(opened);
246  qPrintable(QString("open %1: %2")
247  .arg(file.fileName()).arg(file.errorString())));
249 }
250 
252 {
254  const int expected[] = {8, 10, 12, 16, 20, 24};
257 }
258 
260 {
262  #define ARG(...) __VA_ARGS__
263  QCOMPARE(QFontDatabase::standardSizes(), ARG({8, 10, 12, 16, 20, 24}));
264  #undef ARG
266 }
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
void addSingleStringRows()
[20]
void addMultStringRows()
[20]
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:55
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:94
bool open(OpenMode flags) override
Definition: qfile.cpp:897
QString fileName() const override
Definition: qfile.cpp:302
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:57
static QList< int > standardSizes()
QString errorString() const
Definition: qiodevice.cpp:2169
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
static QStringList drivers()
The QString class provides a Unicode character string.
Definition: qstring.h:388
int localeAwareCompare(const QString &s) const
Definition: qstring.cpp:6403
void toUpper()
void Compare()
[8]
set contains("Julia")
QCOMPARE(spy.count(), 1)
Q_CORE_EXPORT void qSleep(int ms)
Q_TESTLIB_EXPORT QTestData & newRow(const char *dataTag)
Definition: qtestcase.cpp:2658
Q_TESTLIB_EXPORT const char * currentTestFunction()
Definition: qtestcase.cpp:2749
Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message)
Definition: qtestcase.cpp:2292
Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc=0, char **argv=nullptr)
Definition: qtestcase.cpp:2019
Q_TESTLIB_EXPORT QTestData & addRow(const char *format,...) Q_ATTRIBUTE_FORMAT_PRINTF(1
Definition: qtestcase.cpp:2690
#define QString()
Definition: parse-defines.h:51
void
Definition: png.h:1080
#define QBENCHMARK
Definition: qbenchmark.h:76
Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2)
@ QtWarningMsg
Definition: qlogging.h:62
SSL_CTX int(*) void arg)
#define QTEST_MAIN(TestObject)
Definition: qtest.h:664
#define QSKIP(statement,...)
Definition: qtestcase.h:222
#define QFETCH(Type, name)
Definition: qtestcase.h:230
#define QEXPECT_FAIL(dataIndex, comment, mode)
Definition: qtestcase.h:224
#define QFAIL(message)
Definition: qtestcase.h:70
#define QTEST(actual, testElement)
Definition: qtestcase.h:236
#define QVERIFY(statement)
Definition: qtestcase.h:64
#define QVERIFY2(statement, description)
Definition: qtestcase.h:76
#define Q_OBJECT
Definition: qtmetamacros.h:158
#define slots
Definition: qtmetamacros.h:76
QFile file
[0]
QString dir
[11]
void mySleep()
[22]
void compareListToInitializerList()
void closeAllDatabases()
#define ARG(...)
void tstQDir()
void testObject()
[11]
void wrapInFunction()
void compareListToArray()
void verifyString()
[27]
void testInt()
[4]