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 <ivan.solovev@qt.io>
This commit is contained in:
Rym Bouabid 2024-02-13 18:22:06 +01:00
parent 14b0580482
commit 22ebe86f15
5 changed files with 39 additions and 13 deletions

View File

@ -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

View File

@ -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;
}
/*!

View File

@ -5,6 +5,7 @@
#ifndef QPROCESS_H
#define QPROCESS_H
#include <QtCore/qcompare.h>
#include <QtCore/qiodevice.h>
#include <QtCore/qstringlist.h>
#include <QtCore/qshareddata.h>
@ -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<QProcessEnvironmentPrivate> d;

View File

@ -14,4 +14,6 @@ endif()
qt_internal_add_test(tst_qprocessenvironment
SOURCES
tst_qprocessenvironment.cpp
LIBRARIES
Qt::TestPrivate
)

View File

@ -2,6 +2,7 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QtTest/private/qcomparisontesthelper_p.h>
#include <QObject>
#include <QProcessEnvironment>
@ -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<QProcessEnvironment>();
}
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()