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