| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 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 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | |
| 30 | #include <QtTest/QtTest> |
| 31 | |
| 32 | /* We expect these headers to be available. */ |
| 33 | #include <QtXmlPatterns/QSourceLocation> |
| 34 | #include <QtXmlPatterns/qsourcelocation.h> |
| 35 | #include <QSourceLocation> |
| 36 | #include <qsourcelocation.h> |
| 37 | |
| 38 | /*! |
| 39 | \class tst_QSourceLocation |
| 40 | \internal |
| 41 | \since 4.4 |
| 42 | \brief Tests QSourceLocation |
| 43 | |
| 44 | */ |
| 45 | class tst_QSourceLocation : public QObject |
| 46 | { |
| 47 | Q_OBJECT |
| 48 | |
| 49 | private Q_SLOTS: |
| 50 | void isNull() const; |
| 51 | void defaultConstructor() const; |
| 52 | void valueConstructor() const; |
| 53 | void valueConstructorDefaultArguments() const; |
| 54 | void copyConstructor() const; |
| 55 | void assignmentOperator() const; |
| 56 | void equalnessOperator() const; |
| 57 | void equalnessOperator_data() const; |
| 58 | void defaultValues() const; |
| 59 | void constCorrectness() const; |
| 60 | void objectSize() const; |
| 61 | void setLine() const; |
| 62 | void setColumn() const; |
| 63 | void setUri() const; |
| 64 | void withinQVariant() const; |
| 65 | void debugStream() const; |
| 66 | void debugStream_data() const; |
| 67 | void withQHash() const; |
| 68 | }; |
| 69 | |
| 70 | /*! |
| 71 | We allocate a couple to catch reference counting bugs. |
| 72 | */ |
| 73 | void tst_QSourceLocation::defaultConstructor() const |
| 74 | { |
| 75 | QSourceLocation def1; |
| 76 | QSourceLocation def2; |
| 77 | QSourceLocation def3; |
| 78 | } |
| 79 | |
| 80 | void tst_QSourceLocation::copyConstructor() const |
| 81 | { |
| 82 | { |
| 83 | QSourceLocation def; |
| 84 | QSourceLocation copy(def); |
| 85 | |
| 86 | QCOMPARE(def.line(), qint64(-1)); |
| 87 | QCOMPARE(def.column(), qint64(-1)); |
| 88 | QCOMPARE(def.uri(), QUrl()); |
| 89 | } |
| 90 | |
| 91 | { |
| 92 | QSourceLocation val; |
| 93 | val.setLine(5); |
| 94 | val.setColumn(600); |
| 95 | val.setUri(QUrl(QLatin1String("http://example.com/" ))); |
| 96 | |
| 97 | QSourceLocation copy(val); |
| 98 | QCOMPARE(copy.line(), qint64(5)); |
| 99 | QCOMPARE(copy.column(), qint64(600)); |
| 100 | QCOMPARE(copy.uri(), QUrl(QLatin1String("http://example.com/" ))); |
| 101 | } |
| 102 | |
| 103 | { |
| 104 | /* Construct from a const object. */ |
| 105 | const QSourceLocation val; |
| 106 | const QSourceLocation val2(val); |
| 107 | QCOMPARE(val, val2); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void tst_QSourceLocation::valueConstructor() const |
| 112 | { |
| 113 | const QSourceLocation sl(QUrl(QLatin1String("http://example.com/" )), 5, 4); |
| 114 | |
| 115 | QCOMPARE(sl.uri(), QUrl(QLatin1String("http://example.com/" ))); |
| 116 | QCOMPARE(sl.line(), qint64(5)); |
| 117 | QCOMPARE(sl.column(), qint64(4)); |
| 118 | } |
| 119 | |
| 120 | void tst_QSourceLocation::valueConstructorDefaultArguments() const |
| 121 | { |
| 122 | /* The line and column arguments are optional. */ |
| 123 | const QSourceLocation sl(QUrl(QLatin1String("http://example.com/" ))); |
| 124 | |
| 125 | QCOMPARE(sl.uri(), QUrl(QLatin1String("http://example.com/" ))); |
| 126 | QCOMPARE(sl.line(), qint64(-1)); |
| 127 | QCOMPARE(sl.column(), qint64(-1)); |
| 128 | } |
| 129 | |
| 130 | void tst_QSourceLocation::assignmentOperator() const |
| 131 | { |
| 132 | /* Assign to self. */ |
| 133 | { |
| 134 | QSourceLocation def; |
| 135 | |
| 136 | def = def; |
| 137 | |
| 138 | QVERIFY(def.isNull()); |
| 139 | QCOMPARE(def.line(), qint64(-1)); |
| 140 | QCOMPARE(def.column(), qint64(-1)); |
| 141 | QCOMPARE(def.uri(), QUrl()); |
| 142 | } |
| 143 | |
| 144 | /* Assign to default constructed object. */ |
| 145 | { |
| 146 | QSourceLocation val; |
| 147 | val.setLine(3); |
| 148 | val.setColumn(4); |
| 149 | val.setUri(QUrl(QLatin1String("http://example.com/2" ))); |
| 150 | |
| 151 | QSourceLocation assigned; |
| 152 | assigned = val; |
| 153 | |
| 154 | QCOMPARE(assigned.line(), qint64(3)); |
| 155 | QCOMPARE(assigned.column(), qint64(4)); |
| 156 | QCOMPARE(assigned.uri(), QUrl(QLatin1String("http://example.com/2" ))); |
| 157 | } |
| 158 | |
| 159 | /* Assign to modified object. */ |
| 160 | { |
| 161 | QSourceLocation val; |
| 162 | val.setLine(3); |
| 163 | val.setColumn(4); |
| 164 | val.setUri(QUrl(QLatin1String("http://example.com/2" ))); |
| 165 | |
| 166 | QSourceLocation assigned; |
| 167 | assigned.setLine(700); |
| 168 | assigned.setColumn(4000); |
| 169 | assigned.setUri(QUrl(QLatin1String("http://example.com/3" ))); |
| 170 | |
| 171 | assigned = val; |
| 172 | |
| 173 | QCOMPARE(assigned.line(), qint64(3)); |
| 174 | QCOMPARE(assigned.column(), qint64(4)); |
| 175 | QCOMPARE(assigned.uri(), QUrl(QLatin1String("http://example.com/2" ))); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /*! |
| 180 | This includes operator!=() |
| 181 | */ |
| 182 | void tst_QSourceLocation::equalnessOperator() const |
| 183 | { |
| 184 | QFETCH(QSourceLocation, v1); |
| 185 | QFETCH(QSourceLocation, v2); |
| 186 | QFETCH(bool, True); |
| 187 | |
| 188 | QCOMPARE(v1 == v2, True); |
| 189 | QCOMPARE(v1 != v2, !True); |
| 190 | } |
| 191 | |
| 192 | void tst_QSourceLocation::equalnessOperator_data() const |
| 193 | { |
| 194 | QTest::addColumn<QSourceLocation>(name: "v1" ); |
| 195 | QTest::addColumn<QSourceLocation>(name: "v2" ); |
| 196 | QTest::addColumn<bool>(name: "True" ); |
| 197 | |
| 198 | { |
| 199 | QTest::newRow(dataTag: "Default constructed values" ) |
| 200 | << QSourceLocation() |
| 201 | << QSourceLocation() |
| 202 | << true; |
| 203 | } |
| 204 | |
| 205 | { |
| 206 | QSourceLocation modified; |
| 207 | modified.setColumn(4); |
| 208 | |
| 209 | QTest::newRow(dataTag: "Default constructed, against column-modified" ) |
| 210 | << QSourceLocation() |
| 211 | << modified |
| 212 | << false; |
| 213 | } |
| 214 | |
| 215 | { |
| 216 | QSourceLocation modified; |
| 217 | modified.setLine(5); |
| 218 | |
| 219 | QTest::newRow(dataTag: "Default constructed, against line-modified" ) |
| 220 | << QSourceLocation() |
| 221 | << modified |
| 222 | << false; |
| 223 | } |
| 224 | |
| 225 | { |
| 226 | QSourceLocation modified; |
| 227 | modified.setUri(QUrl(QLatin1String("http://example.com/" ))); |
| 228 | |
| 229 | QTest::newRow(dataTag: "Default constructed, against line-modified" ) |
| 230 | << QSourceLocation() |
| 231 | << modified |
| 232 | << false; |
| 233 | } |
| 234 | |
| 235 | { |
| 236 | QSourceLocation modified; |
| 237 | modified.setUri(QUrl(QLatin1String("http://example.com/" ))); |
| 238 | modified.setLine(5); |
| 239 | modified.setColumn(4); |
| 240 | |
| 241 | QTest::newRow(dataTag: "Default constructed, against all-modified" ) |
| 242 | << QSourceLocation() |
| 243 | << modified |
| 244 | << false; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void tst_QSourceLocation::defaultValues() const |
| 249 | { |
| 250 | QSourceLocation def; |
| 251 | |
| 252 | QCOMPARE(def.line(), qint64(-1)); |
| 253 | QCOMPARE(def.column(), qint64(-1)); |
| 254 | QCOMPARE(def.uri(), QUrl()); |
| 255 | } |
| 256 | |
| 257 | /*! |
| 258 | Call functions that must be const. |
| 259 | */ |
| 260 | void tst_QSourceLocation::constCorrectness() const |
| 261 | { |
| 262 | const QSourceLocation def; |
| 263 | |
| 264 | def.line(); |
| 265 | def.column(); |
| 266 | def.uri(); |
| 267 | def.isNull(); |
| 268 | |
| 269 | const QSourceLocation def2; |
| 270 | |
| 271 | /* Equalness operator. */ |
| 272 | QVERIFY(def == def2); |
| 273 | QCOMPARE(def, def2); |
| 274 | |
| 275 | /* Inverse equalness operator. */ |
| 276 | QVERIFY(!(def != def2)); |
| 277 | } |
| 278 | |
| 279 | void tst_QSourceLocation::objectSize() const |
| 280 | { |
| 281 | /* We can't compare the two values. QSourceLocation is 24 on some Mac OS X, while |
| 282 | * the other operand evaluates to 20. */ |
| 283 | QVERIFY(sizeof(QSourceLocation) >= sizeof(QUrl) + sizeof(qint64) * 2); |
| 284 | } |
| 285 | |
| 286 | void tst_QSourceLocation::isNull() const |
| 287 | { |
| 288 | { |
| 289 | QSourceLocation def; |
| 290 | QVERIFY(def.isNull()); |
| 291 | |
| 292 | def.setColumn(4); |
| 293 | QVERIFY(def.isNull()); |
| 294 | } |
| 295 | |
| 296 | { |
| 297 | QSourceLocation def2; |
| 298 | def2.setLine(4); |
| 299 | QVERIFY(def2.isNull()); |
| 300 | } |
| 301 | |
| 302 | { |
| 303 | QSourceLocation def3; |
| 304 | def3.setUri(QUrl(QLatin1String("http://example.com/" ))); |
| 305 | QVERIFY(!def3.isNull()); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void tst_QSourceLocation::setLine() const |
| 310 | { |
| 311 | QSourceLocation sl; |
| 312 | sl.setLine(8); |
| 313 | QCOMPARE(sl.line(), qint64(8)); |
| 314 | } |
| 315 | |
| 316 | void tst_QSourceLocation::setColumn() const |
| 317 | { |
| 318 | QSourceLocation sl; |
| 319 | sl.setColumn(5); |
| 320 | QCOMPARE(sl.column(), qint64(5)); |
| 321 | } |
| 322 | |
| 323 | void tst_QSourceLocation::setUri() const |
| 324 | { |
| 325 | QSourceLocation sl; |
| 326 | sl.setUri(QUrl(QLatin1String("http://example.com/" ))); |
| 327 | QCOMPARE(sl.uri(), QUrl(QLatin1String("http://example.com/" ))); |
| 328 | } |
| 329 | |
| 330 | void tst_QSourceLocation::withinQVariant() const |
| 331 | { |
| 332 | QSourceLocation val; |
| 333 | const auto variant = QVariant::fromValue(value: val); |
| 334 | QSourceLocation val2(qvariant_cast<QSourceLocation>(v: variant)); |
| 335 | } |
| 336 | |
| 337 | void tst_QSourceLocation::debugStream() const |
| 338 | { |
| 339 | QFETCH(QSourceLocation, location); |
| 340 | QFETCH(QString, expected); |
| 341 | |
| 342 | QString actual; |
| 343 | QDebug stream(&actual); |
| 344 | |
| 345 | #ifndef QT_NO_DEBUG_STREAM |
| 346 | stream << location; |
| 347 | QCOMPARE(actual, expected); |
| 348 | #endif |
| 349 | } |
| 350 | |
| 351 | void tst_QSourceLocation::debugStream_data() const |
| 352 | { |
| 353 | QTest::addColumn<QSourceLocation>(name: "location" ); |
| 354 | QTest::addColumn<QString>(name: "expected" ); |
| 355 | |
| 356 | { |
| 357 | QTest::newRow(dataTag: "Default constructed instance" ) |
| 358 | << QSourceLocation() |
| 359 | << QString::fromLatin1(str: "QSourceLocation( QUrl(\"\") , line: -1 , column: -1 ) " ); |
| 360 | } |
| 361 | |
| 362 | { |
| 363 | QSourceLocation location(QUrl(QLatin1String("http://example.com/" )), 4, 5); |
| 364 | QTest::newRow(dataTag: "Properties set" ) |
| 365 | << location |
| 366 | << QString::fromLatin1(str: "QSourceLocation( QUrl(\"http://example.com/\") , line: 4 , column: 5 ) " ); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void tst_QSourceLocation::withQHash() const |
| 371 | { |
| 372 | QCOMPARE(qHash(QSourceLocation()), qHash(QSourceLocation())); |
| 373 | } |
| 374 | |
| 375 | QTEST_MAIN(tst_QSourceLocation) |
| 376 | |
| 377 | #include "tst_qsourcelocation.moc" |
| 378 | |
| 379 | // vim: et:ts=4:sw=4:sts=4 |
| 380 | |