Skip to content

Commit 573511d

Browse files
Delete Concrete(Read|Data)AttributePath equality operator (#32131)
* Delete Concrete(Read|Data)AttributePath equality operator Right now these default to using the ConcreteAttributePath equality operator, which is misleading because it will return true for objects which are not actually equal. Replace this with the MatchesConcreteAttributePath function. A new equality operator for these classes will be introduced in a follow-up PR. I'm introducing this change separately to ensure that CI catches any current usage of the equality operator. * Restyled by clang-format * Add unit tests * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 39b2382 commit 573511d

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

src/app/ConcreteAttributePath.h

+10
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ struct ConcreteReadAttributePath : public ConcreteAttributePath
8787
mListIndex.SetValue(aListIndex);
8888
}
8989

90+
bool operator==(const ConcreteReadAttributePath & aOther) const = delete;
91+
bool operator!=(const ConcreteReadAttributePath & aOther) const = delete;
92+
bool operator<(const ConcreteReadAttributePath & aOther) const = delete;
93+
9094
Optional<uint16_t> mListIndex;
9195
};
9296

@@ -138,6 +142,12 @@ struct ConcreteDataAttributePath : public ConcreteAttributePath
138142
ChipLogValueMEI(mClusterId), ChipLogValueMEI(mAttributeId));
139143
}
140144

145+
bool MatchesConcreteAttributePath(const ConcreteAttributePath & aOther) { return ConcreteAttributePath::operator==(aOther); }
146+
147+
bool operator==(const ConcreteDataAttributePath & aOther) const = delete;
148+
bool operator!=(const ConcreteDataAttributePath & aOther) const = delete;
149+
bool operator<(const ConcreteDataAttributePath & aOther) const = delete;
150+
141151
//
142152
// This index is only valid if `mListOp` is set to a list item operation, i.e
143153
// ReplaceItem, DeleteItem or AppendItem. Otherwise, it is to be ignored.

src/app/ReadClient.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,8 @@ CHIP_ERROR ReadClient::ProcessAttributeReportIBs(TLV::TLVReader & aAttributeRepo
799799
attributePath.mListOp = ConcreteDataAttributePath::ListOperation::ReplaceAll;
800800
}
801801

802-
if (attributePath ==
803-
ConcreteDataAttributePath(kRootEndpointId, Clusters::IcdManagement::Id,
804-
Clusters::IcdManagement::Attributes::OperatingMode::Id))
802+
if (attributePath.MatchesConcreteAttributePath(ConcreteAttributePath(
803+
kRootEndpointId, Clusters::IcdManagement::Id, Clusters::IcdManagement::Attributes::OperatingMode::Id)))
805804
{
806805
PeerType peerType;
807806
TLV::TLVReader operatingModeTlvReader;

src/app/tests/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ chip_test_suite_using_nltest("tests") {
135135
"TestClusterInfo.cpp",
136136
"TestCommandInteraction.cpp",
137137
"TestCommandPathParams.cpp",
138+
"TestConcreteAttributePath.cpp",
138139
"TestDataModelSerialization.cpp",
139140
"TestDefaultOTARequestorStorage.cpp",
140141
"TestEventLoggingNoUTCTime.cpp",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include <app/ConcreteAttributePath.h>
20+
#include <lib/support/UnitTestRegistration.h>
21+
#include <nlunit-test.h>
22+
23+
using namespace chip;
24+
using namespace chip::app;
25+
26+
namespace {
27+
28+
void TestConcreteAttributePathEqualityDefaultConstructor(nlTestSuite * aSuite, void * aContext)
29+
{
30+
ConcreteAttributePath path_one;
31+
ConcreteAttributePath path_two;
32+
NL_TEST_ASSERT(aSuite, path_one == path_two);
33+
}
34+
35+
void TestConcreteAttributePathEquality(nlTestSuite * aSuite, void * aContext)
36+
{
37+
ConcreteAttributePath path_one(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3);
38+
ConcreteAttributePath path_two(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3);
39+
NL_TEST_ASSERT(aSuite, path_one == path_two);
40+
}
41+
42+
void TestConcreteAttributePathInequalityDifferentAttributeId(nlTestSuite * aSuite, void * aContext)
43+
{
44+
ConcreteAttributePath path_one(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3);
45+
ConcreteAttributePath path_two(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/4);
46+
NL_TEST_ASSERT(aSuite, path_one != path_two);
47+
}
48+
49+
void TestConcreteDataAttributePathMatchesConcreteAttributePathEquality(nlTestSuite * aSuite, void * aContext)
50+
{
51+
ConcreteAttributePath path(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3);
52+
ConcreteDataAttributePath data_path(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3);
53+
ConcreteDataAttributePath data_path_with_version(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3,
54+
/*aDataVersion=*/MakeOptional(4U));
55+
ConcreteDataAttributePath data_path_with_list(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3,
56+
/*aListOp=*/ConcreteDataAttributePath::ListOperation::ReplaceAll,
57+
/*aListIndex=*/5U);
58+
59+
NL_TEST_ASSERT(aSuite, data_path.MatchesConcreteAttributePath(path));
60+
NL_TEST_ASSERT(aSuite, data_path_with_version.MatchesConcreteAttributePath(path));
61+
NL_TEST_ASSERT(aSuite, data_path_with_list.MatchesConcreteAttributePath(path));
62+
}
63+
64+
void TestConcreteDataAttributePathMatchesConcreteAttributePathInequality(nlTestSuite * aSuite, void * aContext)
65+
{
66+
ConcreteAttributePath path(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3);
67+
ConcreteDataAttributePath data_path(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/4);
68+
69+
NL_TEST_ASSERT(aSuite, !data_path.MatchesConcreteAttributePath(path));
70+
}
71+
72+
const nlTest sTests[] = {
73+
NL_TEST_DEF("TestConcreteAttributePathEqualityDefaultConstructor", TestConcreteAttributePathEqualityDefaultConstructor),
74+
NL_TEST_DEF("TestConcreteAttributePathEquality", TestConcreteAttributePathEquality),
75+
NL_TEST_DEF("TestConcreteAttributePathInequalityDifferentAttributeId", TestConcreteAttributePathInequalityDifferentAttributeId),
76+
NL_TEST_DEF("TestConcreteDataAttributePathMatchesConcreteAttributePathEquality",
77+
TestConcreteDataAttributePathMatchesConcreteAttributePathEquality),
78+
NL_TEST_DEF("TestConcreteDataAttributePathMatchesConcreteAttributePathInequality",
79+
TestConcreteDataAttributePathMatchesConcreteAttributePathInequality),
80+
NL_TEST_SENTINEL()
81+
};
82+
83+
} // anonymous namespace
84+
85+
int TestConcreteAttributePath()
86+
{
87+
nlTestSuite theSuite = { "ConcreteAttributePath", &sTests[0], nullptr, nullptr };
88+
89+
nlTestRunner(&theSuite, nullptr);
90+
91+
return (nlTestRunnerStats(&theSuite));
92+
}
93+
94+
CHIP_REGISTER_TEST_SUITE(TestConcreteAttributePath)

0 commit comments

Comments
 (0)