diff options
| author | Rym Bouabid <rym.bouabid@qt.io> | 2024-02-09 17:37:09 +0100 |
|---|---|---|
| committer | Rym Bouabid <rym.bouabid@qt.io> | 2024-02-13 15:58:42 +0100 |
| commit | 39505c86cc1837d2e22786f855fd5577a01023c5 (patch) | |
| tree | 6017c21f39cfef82ff2429d7abd43ceb9d5d2e0e | |
| parent | 3828b30951e3bdaa8227d0ade14725de04593671 (diff) | |
QDir: Use new comparison helper macros
QDir had operator==() and operator!=() defined as public member
functions, so use QT_CORE_REMOVED_SINCE and removed_api.cpp to get
rid of these methods and replace them with a hidden friend.
Use QTestPrivate::testEqualityOperators() helper function in unit-tests.
Task-number: QTBUG-120303
Change-Id: I86c2ba18b8b114efd9f62fc2fd628bc9065b04b2
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
| -rw-r--r-- | src/corelib/compat/removed_api.cpp | 8 | ||||
| -rw-r--r-- | src/corelib/io/qdir.cpp | 25 | ||||
| -rw-r--r-- | src/corelib/io/qdir.h | 5 | ||||
| -rw-r--r-- | tests/auto/corelib/io/qdir/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | tests/auto/corelib/io/qdir/tst_qdir.cpp | 20 |
5 files changed, 40 insertions, 19 deletions
diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index ddfdcb4911d..2328d2235f2 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -915,11 +915,17 @@ QUrl QUrl::fromEncoded(const QByteArray &input, ParsingMode mode) #endif // QT_CORE_REMOVED_SINCE(6, 7) - #if QT_CORE_REMOVED_SINCE(6, 8) #include "qdatastream.h" // inlined API +#include "qdir.h" // inlined API + +bool QDir::operator==(const QDir &dir) const +{ + return comparesEqual(*this, dir); +} + // #include "qotherheader.h" // // implement removed functions from qotherheader.h // order sections alphabetically to reduce chances of merge conflicts diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index 3ad804b9e88..d0264c6d3a5 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -1806,7 +1806,9 @@ bool QDir::makeAbsolute() } /*! - Returns \c true if directory \a dir and this directory have the same + \fn bool QDir::operator==(const QDir &lhs, const QDir &rhs) + + Returns \c true if directory \a lhs and directory \a rhs have the same path and their sort and filter settings are the same; otherwise returns \c false. @@ -1814,10 +1816,10 @@ bool QDir::makeAbsolute() \snippet code/src_corelib_io_qdir.cpp 10 */ -bool QDir::operator==(const QDir &dir) const +bool comparesEqual(const QDir &lhs, const QDir &rhs) { - Q_D(const QDir); - const QDirPrivate *other = dir.d_ptr.constData(); + const QDirPrivate *d = lhs.d_ptr.constData(); + const QDirPrivate *other = rhs.d_ptr.constData(); if (d == other) return true; @@ -1841,13 +1843,13 @@ bool QDir::operator==(const QDir &dir) const if (d->dirEntry.filePath() == other->dirEntry.filePath()) return true; - if (exists()) { - if (!dir.exists()) + if (lhs.exists()) { + if (!rhs.exists()) return false; //can't be equal if only one exists // Both exist, fallback to expensive canonical path computation - return canonicalPath().compare(dir.canonicalPath(), sensitive) == 0; + return lhs.canonicalPath().compare(rhs.canonicalPath(), sensitive) == 0; } else { - if (dir.exists()) + if (rhs.exists()) return false; //can't be equal if only one exists // Neither exists, compare absolute paths rather than canonical (which would be empty strings) QString thisFilePath = d->resolveAbsoluteEntry(); @@ -1877,11 +1879,10 @@ QDir &QDir::operator=(const QDir &dir) */ /*! - \fn bool QDir::operator!=(const QDir &dir) const + \fn bool QDir::operator!=(const QDir &lhs, const QDir &rhs) - Returns \c true if directory \a dir and this directory have different - paths or different sort or filter settings; otherwise returns - false. + Returns \c true if directory \a lhs and directory \a rhs have different + paths or different sort or filter settings; otherwise returns \c false. Example: diff --git a/src/corelib/io/qdir.h b/src/corelib/io/qdir.h index 7d5e940e846..92871062c81 100644 --- a/src/corelib/io/qdir.h +++ b/src/corelib/io/qdir.h @@ -4,6 +4,7 @@ #ifndef QDIR_H #define QDIR_H +#include <QtCore/qcompare.h> #include <QtCore/qstring.h> #include <QtCore/qfile.h> #include <QtCore/qfileinfo.h> @@ -185,8 +186,10 @@ public: inline bool isAbsolute() const { return !isRelative(); } bool makeAbsolute(); +#if QT_CORE_REMOVED_SINCE(6, 8) bool operator==(const QDir &dir) const; inline bool operator!=(const QDir &dir) const { return !operator==(dir); } +#endif bool remove(const QString &fileName); bool rename(const QString &oldName, const QString &newName); @@ -237,6 +240,8 @@ protected: QSharedDataPointer<QDirPrivate> d_ptr; private: + friend Q_CORE_EXPORT bool comparesEqual(const QDir &lhs, const QDir &rhs); + Q_DECLARE_EQUALITY_COMPARABLE(QDir) friend class QDirIterator; // Q_DECLARE_PRIVATE equivalent for shared data pointers QDirPrivate *d_func(); diff --git a/tests/auto/corelib/io/qdir/CMakeLists.txt b/tests/auto/corelib/io/qdir/CMakeLists.txt index cf8987c4e6c..4032d7ac19f 100644 --- a/tests/auto/corelib/io/qdir/CMakeLists.txt +++ b/tests/auto/corelib/io/qdir/CMakeLists.txt @@ -25,6 +25,7 @@ qt_internal_add_test(tst_qdir tst_qdir.cpp LIBRARIES Qt::CorePrivate + Qt::TestPrivate TESTDATA ${test_data} ) diff --git a/tests/auto/corelib/io/qdir/tst_qdir.cpp b/tests/auto/corelib/io/qdir/tst_qdir.cpp index bc91337f1d1..9db1f3f8068 100644 --- a/tests/auto/corelib/io/qdir/tst_qdir.cpp +++ b/tests/auto/corelib/io/qdir/tst_qdir.cpp @@ -3,6 +3,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> +#include <QtTest/private/qcomparisontesthelper_p.h> #include <QTemporaryFile> #if QT_CONFIG(process) #include <QProcess> @@ -126,6 +127,7 @@ private slots: void normalizePathSegments(); #endif + void compareCompiles(); void compare(); void QDir_default(); @@ -701,18 +703,24 @@ void tst_QDir::QDir_default() QCOMPARE(dir.absolutePath(), QDir::currentPath()); } -void tst_QDir::compare() +void tst_QDir::compareCompiles() { - // operator== - - // Not using QCOMPARE to test result of QDir::operator== + QTestPrivate::testEqualityOperatorsCompile<QDir>(); +} +void tst_QDir::compare() +{ QDir dir; dir.makeAbsolute(); - QVERIFY(dir == QDir::currentPath()); + QTestPrivate::testEqualityOperators(dir, QDir::currentPath(), true); + if (QTest::currentTestFailed()) + return; QCOMPARE(QDir(), QDir(QDir::currentPath())); - QVERIFY(QDir("../") == QDir(QDir::currentPath() + "/..")); + + QTestPrivate::testEqualityOperators(QDir("../"), QDir(QDir::currentPath() + "/.."), true); + if (QTest::currentTestFailed()) + return; } static QStringList filterLinks(QStringList &&list) |
