Skip to content

Commit eca4000

Browse files
Small fixes to cluster unit testing infrastructure (project-chip#41882)
* Add error logging to `Is*ListEqualTo` functions * Add `GetNextGeneratedEvent` to class `Cluster` and make `Invoke` fully deduce template parameters This was removed from PR project-chip#41603 by mistake * Restyled by clang-format * Update `ClusterTester::Invoke` usage in `TestGroupcastCluster.cpp` * Update comment for `ClusterTester::Invoke` * Add license notice and `#pragma once` in some files. * Use initializer list for `IsGeneratedCommandsListEqualTo` --------- Co-authored-by: Restyled.io <[email protected]>
1 parent d886e3a commit eca4000

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

src/app/clusters/groupcast/tests/TestGroupcastCluster.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ TEST_F(TestGroupcastCluster, TestJoinGroupCommand)
117117

118118
chip::app::Testing::MockCommandHandler cmdHandler;
119119
chip::Test::ClusterTester tester(cluster);
120-
auto result =
121-
tester.Invoke<Commands::JoinGroup::Type::ResponseType, Commands::JoinGroup::Type>(Commands::JoinGroup::Id, cmdData);
120+
auto result = tester.Invoke(Commands::JoinGroup::Id, cmdData);
122121
ASSERT_TRUE(result.status.has_value());
123122
EXPECT_EQ(result.status.value().GetStatusCode().GetStatus(), // NOLINT(bugprone-unchecked-optional-access)
124123
Protocols::InteractionModel::Status::Failure); // Currently expect Failure as JoinGroup command returns

src/app/clusters/testing/AttributeTesting.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
#pragma once
1718
#include <app/data-model-provider/MetadataTypes.h>
1819
#include <lib/support/Span.h>
1920

src/app/clusters/testing/ClusterTester.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
#pragma once
1718
#include <app/AttributeValueDecoder.h>
1819
#include <app/AttributeValueEncoder.h>
1920
#include <app/CommandHandler.h>
@@ -144,12 +145,11 @@ class ClusterTester
144145
};
145146

146147
// Invoke a command and return the decoded result.
147-
// The `RequestType`, `ResponseType` type-parameters must be of the correct type for the command being invoked.
148-
// Use `app::Clusters::<ClusterName>::Commands::<CommandName>::Type` for the `RequestType` type-parameter to be spec compliant
149-
// Use `app::Clusters::<ClusterName>::Commands::<CommandName>::Type::ResponseType` for the `ResponseType` type-parameter to be
150-
// spec compliant Will construct the command path using the first path returned by `GetPaths()` on the cluster.
148+
// The `request` parameter must be of the correct type for the command being invoked.
149+
// Use `app::Clusters::<ClusterName>::Commands::<CommandName>::Type` for the `request` parameter to be spec compliant
150+
// Will construct the command path using the first path returned by `GetPaths()` on the cluster.
151151
// @returns `CHIP_ERROR_INCORRECT_STATE` if `GetPaths()` doesn't return a list with one path.
152-
template <typename ResponseType, typename RequestType>
152+
template <typename RequestType, typename ResponseType = typename RequestType::ResponseType>
153153
[[nodiscard]] InvokeResult<ResponseType> Invoke(chip::CommandId commandId, const RequestType & request)
154154
{
155155
InvokeResult<ResponseType> result;
@@ -198,6 +198,12 @@ class ClusterTester
198198
return result;
199199
}
200200

201+
// Returns the next generated event from the event generator in the test server cluster context
202+
std::optional<LogOnlyEvents::EventInformation> GetNextGeneratedEvent()
203+
{
204+
return mTestServerClusterContext.EventsGenerator().GetNextEvent();
205+
}
206+
201207
private:
202208
bool VerifyClusterPathsValid()
203209
{

src/app/clusters/testing/ValidateGlobalAttributes.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ bool IsAttributesListEqualTo(app::ServerClusterInterface & cluster,
2323
VerifyOrDie(cluster.GetPaths().size() == 1);
2424
auto path = cluster.GetPaths()[0];
2525
ReadOnlyBufferBuilder<app::DataModel::AttributeEntry> attributesBuilder;
26-
if (cluster.Attributes(path, attributesBuilder) != CHIP_NO_ERROR)
26+
if (CHIP_ERROR err = cluster.Attributes(path, attributesBuilder); err != CHIP_NO_ERROR)
2727
{
28+
ChipLogError(Test, "Failed to get attributes list from cluster. Error: %" CHIP_ERROR_FORMAT, err.Format());
2829
return false;
2930
}
3031

@@ -47,8 +48,9 @@ bool IsAcceptedCommandsListEqualTo(app::ServerClusterInterface & cluster,
4748
VerifyOrDie(cluster.GetPaths().size() == 1);
4849
auto path = cluster.GetPaths()[0];
4950
ReadOnlyBufferBuilder<app::DataModel::AcceptedCommandEntry> commandsBuilder;
50-
if (cluster.AcceptedCommands(path, commandsBuilder) != CHIP_NO_ERROR)
51+
if (CHIP_ERROR err = cluster.AcceptedCommands(path, commandsBuilder); err != CHIP_NO_ERROR)
5152
{
53+
ChipLogError(Test, "Failed to get accepted commands list from cluster. Error: %" CHIP_ERROR_FORMAT, err.Format());
5254
return false;
5355
}
5456

@@ -63,16 +65,26 @@ bool IsAcceptedCommandsListEqualTo(app::ServerClusterInterface & cluster,
6365
return EqualAcceptedCommandSets(commandsBuilder.TakeBuffer(), expectedBuilder.TakeBuffer());
6466
}
6567

66-
bool IsGeneratedCommandsListEqualTo(app::ServerClusterInterface & cluster, Span<CommandId> expected)
68+
bool IsGeneratedCommandsListEqualTo(app::ServerClusterInterface & cluster, std::initializer_list<const CommandId> expected)
6769
{
6870
VerifyOrDie(cluster.GetPaths().size() == 1);
6971
auto path = cluster.GetPaths()[0];
7072
ReadOnlyBufferBuilder<CommandId> commandsBuilder;
71-
if (cluster.GeneratedCommands(path, commandsBuilder) != CHIP_NO_ERROR)
73+
if (CHIP_ERROR err = cluster.GeneratedCommands(path, commandsBuilder); err != CHIP_NO_ERROR)
7274
{
75+
ChipLogError(Test, "Failed to get generated commands list from cluster. Error: %" CHIP_ERROR_FORMAT, err.Format());
7376
return false;
7477
}
75-
return EqualGeneratedCommandSets(commandsBuilder.TakeBuffer(), expected);
78+
79+
ReadOnlyBufferBuilder<CommandId> expectedBuilder;
80+
81+
SuccessOrDie(expectedBuilder.EnsureAppendCapacity(expected.size()));
82+
for (const auto & entry : expected)
83+
{
84+
SuccessOrDie(expectedBuilder.Append(entry));
85+
}
86+
87+
return EqualGeneratedCommandSets(commandsBuilder.TakeBuffer(), expectedBuilder.TakeBuffer());
7688
}
7789

7890
} // namespace chip::Testing

src/app/clusters/testing/ValidateGlobalAttributes.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
#pragma once
1+
/*
2+
* Copyright (c) 2025 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
216

17+
#pragma once
318
#include "AttributeTesting.h"
419

520
#include <app/server-cluster/DefaultServerCluster.h>
@@ -66,7 +81,7 @@ bool IsAcceptedCommandsListEqualTo(app::ServerClusterInterface & cluster,
6681
/// ClusterImpl cluster(kTestEndpointId, ...);
6782
/// ASSERT_TRUE(IsGeneratedCommandsListEqualTo(cluster, { Commands::SomeCommandResponse::kMetadataEntry }));
6883
/// ```
69-
bool IsGeneratedCommandsListEqualTo(app::ServerClusterInterface & cluster, Span<CommandId> expected);
84+
bool IsGeneratedCommandsListEqualTo(app::ServerClusterInterface & cluster, std::initializer_list<const CommandId> expected);
7085

7186
} // namespace Testing
7287
} // namespace chip

0 commit comments

Comments
 (0)