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