From 22ebe86f1511f7f06031df1da030b78c1e8092c9 Mon Sep 17 00:00:00 2001 From: Rym Bouabid Date: Tue, 13 Feb 2024 18:22:06 +0100 Subject: [PATCH] QProcessEnvironment: Use new comparison helper macros QProcessEnvironment 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 QT_TEST_ALL_EQUALITY_OPS macro in unit-tests. Use new \compares command in the documentation to describe the comparison operators provided by QProcessEnvironment. Task-number: QTBUG-120303 Change-Id: I4c57f6cfb9589e82a37eea6993e079212b34cecd Reviewed-by: Ivan Solovev --- src/corelib/compat/removed_api.cpp | 9 +++++++++ src/corelib/io/qprocess.cpp | 16 ++++++++++------ src/corelib/io/qprocess.h | 8 +++++++- .../io/qprocessenvironment/CMakeLists.txt | 2 ++ .../tst_qprocessenvironment.cpp | 17 +++++++++++------ 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index 0553d32807f..405f22e3d91 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -938,6 +938,15 @@ bool QFileInfo::operator==(const QFileInfo &fileinfo) const return comparesEqual(*this, fileinfo); } +#if QT_CONFIG(processenvironment) +#include "qprocess.h" // inlined API + +bool QProcessEnvironment::operator==(const QProcessEnvironment &other) const +{ + return comparesEqual(*this, other); +} +#endif // QT_CONFIG(processenvironment) + // #include "qotherheader.h" // // implement removed functions from qotherheader.h // order sections alphabetically to reduce chances of merge conflicts diff --git a/src/corelib/io/qprocess.cpp b/src/corelib/io/qprocess.cpp index 5f37e47944d..ad6cf46bbfc 100644 --- a/src/corelib/io/qprocess.cpp +++ b/src/corelib/io/qprocess.cpp @@ -35,6 +35,8 @@ QT_BEGIN_NAMESPACE \reentrant \since 4.6 + \compares equality + A process's environment is composed of a set of key=value pairs known as environment variables. The QProcessEnvironment class wraps that concept and allows easy manipulation of those variables. It's meant to be used @@ -184,15 +186,17 @@ QProcessEnvironment &QProcessEnvironment::operator=(const QProcessEnvironment &o */ /*! - \fn bool QProcessEnvironment::operator !=(const QProcessEnvironment &other) const + \fn bool QProcessEnvironment::operator!=(const QProcessEnvironment &lhs, const QProcessEnvironment &rhs) - Returns \c true if this and the \a other QProcessEnvironment objects are different. + Returns \c true if the process environment objects \a lhs and \a rhs are different. \sa operator==() */ /*! - Returns \c true if this and the \a other QProcessEnvironment objects are equal. + \fn bool QProcessEnvironment::operator==(const QProcessEnvironment &lhs, const QProcessEnvironment &rhs) + + Returns \c true if the process environment objects \a lhs and \a rhs are equal. Two QProcessEnvironment objects are considered equal if they have the same set of key=value pairs. The comparison of keys is done case-sensitive on @@ -200,12 +204,12 @@ QProcessEnvironment &QProcessEnvironment::operator=(const QProcessEnvironment &o \sa operator!=(), contains() */ -bool QProcessEnvironment::operator==(const QProcessEnvironment &other) const +bool comparesEqual(const QProcessEnvironment &lhs, const QProcessEnvironment &rhs) { - if (d == other.d) + if (lhs.d == rhs.d) return true; - return d && other.d && d->vars == other.d->vars; + return lhs.d && rhs.d && lhs.d->vars == rhs.d->vars; } /*! diff --git a/src/corelib/io/qprocess.h b/src/corelib/io/qprocess.h index dc576ebc049..34724a6794c 100644 --- a/src/corelib/io/qprocess.h +++ b/src/corelib/io/qprocess.h @@ -5,6 +5,7 @@ #ifndef QPROCESS_H #define QPROCESS_H +#include #include #include #include @@ -41,9 +42,11 @@ public: void swap(QProcessEnvironment &other) noexcept { d.swap(other.d); } +#if QT_CORE_REMOVED_SINCE(6, 8) bool operator==(const QProcessEnvironment &other) const; inline bool operator!=(const QProcessEnvironment &other) const - { return !(*this == other); } + { return !operator==(other); } +#endif bool isEmpty() const; [[nodiscard]] bool inheritsFromParent() const; @@ -63,6 +66,9 @@ public: static QProcessEnvironment systemEnvironment(); private: + friend Q_CORE_EXPORT bool comparesEqual(const QProcessEnvironment &lhs, + const QProcessEnvironment &rhs); + Q_DECLARE_EQUALITY_COMPARABLE(QProcessEnvironment) friend class QProcessPrivate; friend class QProcessEnvironmentPrivate; QSharedDataPointer d; diff --git a/tests/auto/corelib/io/qprocessenvironment/CMakeLists.txt b/tests/auto/corelib/io/qprocessenvironment/CMakeLists.txt index 37e8029e231..02c69090313 100644 --- a/tests/auto/corelib/io/qprocessenvironment/CMakeLists.txt +++ b/tests/auto/corelib/io/qprocessenvironment/CMakeLists.txt @@ -14,4 +14,6 @@ endif() qt_internal_add_test(tst_qprocessenvironment SOURCES tst_qprocessenvironment.cpp + LIBRARIES + Qt::TestPrivate ) diff --git a/tests/auto/corelib/io/qprocessenvironment/tst_qprocessenvironment.cpp b/tests/auto/corelib/io/qprocessenvironment/tst_qprocessenvironment.cpp index 27a1ec0bbbe..6a2a3daaa2c 100644 --- a/tests/auto/corelib/io/qprocessenvironment/tst_qprocessenvironment.cpp +++ b/tests/auto/corelib/io/qprocessenvironment/tst_qprocessenvironment.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include +#include #include #include @@ -9,6 +10,7 @@ class tst_QProcessEnvironment: public QObject { Q_OBJECT private slots: + void compareCompiles(); void operator_eq(); void clearAndIsEmpty(); void clearAndInheritsFromParent(); @@ -23,6 +25,11 @@ private slots: void putenv(); }; +void tst_QProcessEnvironment::compareCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile(); +} + void tst_QProcessEnvironment::operator_eq() { QProcessEnvironment e1; @@ -35,8 +42,7 @@ void tst_QProcessEnvironment::operator_eq() QCOMPARE(e1, e2); auto parentEnv = QProcessEnvironment(QProcessEnvironment::InheritFromParent); - QVERIFY(parentEnv != e2); - QVERIFY(e2 != parentEnv); + QT_TEST_EQUALITY_OPS(parentEnv, e2, false); e1.clear(); QCOMPARE(e1, e2); @@ -45,16 +51,15 @@ void tst_QProcessEnvironment::operator_eq() QCOMPARE(e1, e2); e1.insert("FOO", "bar"); - QVERIFY(e1 != e2); + QT_TEST_EQUALITY_OPS(e1, e2, false); e2.insert("FOO", "bar"); QCOMPARE(e1, e2); e2.insert("FOO", "baz"); - QVERIFY(e1 != e2); + QT_TEST_EQUALITY_OPS(e1, e2, false); - QVERIFY(e2 != parentEnv); - QVERIFY(parentEnv != e2); + QT_TEST_EQUALITY_OPS(e2, parentEnv, false); } void tst_QProcessEnvironment::clearAndIsEmpty()