@@ -34,7 +34,8 @@ namespace Testing {
3434// /
3535// / Parameters:
3636// / cluster - The cluster interface to test.
37- // / expected - An initializer list of expected attribute entries (may be empty for only globals)
37+ // / expected - initializer_list or any other iterable of expected attribute entries supporting `std::size(container)` syntax.
38+ // / May be empty for global attributes only.
3839// /
3940// / @note This function will assert (die) if `cluster.GetPaths()` does not return exactly one path.
4041// /
@@ -43,16 +44,39 @@ namespace Testing {
4344// / ClusterImpl cluster(kTestEndpointId, ....);
4445// / ASSERT_TRUE(IsAttributesListEqualTo(cluster, { Attributes::SomeAttribute::kMetadataEntry }));
4546// / ```
46- bool IsAttributesListEqualTo (app::ServerClusterInterface & cluster,
47- std::initializer_list<const app::DataModel::AttributeEntry> expected);
4847
49- // / Overload of IsAttributesListEqualTo that accepts a standard vector of attribute entries.
50- // /
51- // / Parameters:
52- // / cluster - The cluster interface to test.
53- // / expected - A vector containing the expected attribute entries (must include all
54- // / non-global, non-optional attributes).
55- bool IsAttributesListEqualTo (app::ServerClusterInterface & cluster, const std::vector<app::DataModel::AttributeEntry> & expected);
48+ template <class T >
49+ bool IsAttributesListEqualTo (app::ServerClusterInterface & cluster, const T & expected)
50+ {
51+ VerifyOrDie (cluster.GetPaths ().size () == 1 );
52+ auto path = cluster.GetPaths ()[0 ];
53+ ReadOnlyBufferBuilder<app::DataModel::AttributeEntry> attributesBuilder;
54+ if (CHIP_ERROR err = cluster.Attributes (path, attributesBuilder); err != CHIP_NO_ERROR )
55+ {
56+ ChipLogError (Test, " Failed to get attributes list from cluster. Error: %" CHIP_ERROR_FORMAT , err.Format ());
57+ return false ;
58+ }
59+
60+ ReadOnlyBufferBuilder<app::DataModel::AttributeEntry> expectedBuilder;
61+
62+ SuccessOrDie (expectedBuilder.EnsureAppendCapacity (std::size (expected)));
63+ for (const auto & entry : expected)
64+ {
65+ SuccessOrDie (expectedBuilder.Append (entry));
66+ }
67+
68+ SuccessOrDie (expectedBuilder.AppendElements (app::DefaultServerCluster::GlobalAttributes ()));
69+
70+ return EqualAttributeSets (attributesBuilder.TakeBuffer (), expectedBuilder.TakeBuffer ());
71+ }
72+
73+ // Overload for std::initializer_list to not get "template argument deduction failed" when calling `IsAttributesListEqualTo(cluster,
74+ // {...})`
75+ template <typename T = const app::DataModel::AttributeEntry>
76+ bool IsAttributesListEqualTo (app::ServerClusterInterface & cluster, std::initializer_list<T> expected)
77+ {
78+ return IsAttributesListEqualTo<std::initializer_list<T>>(cluster, expected);
79+ }
5680
5781// / Compares the accepted commands of a cluster against an expected set.
5882// /
@@ -61,7 +85,8 @@ bool IsAttributesListEqualTo(app::ServerClusterInterface & cluster, const std::v
6185// /
6286// / Parameters:
6387// / cluster - The cluster interface to test.
64- // / expected - An initializer list of expected accepted command entries. May be empty.
88+ // / expected - initializer_list or any other iterable of expected accepted command entries supporting `std::size(container)`
89+ // / syntax. May be empty.
6590// /
6691// / @note This function will assert (die) if `cluster.GetPaths()` does not return exactly one path.
6792// /
@@ -71,8 +96,36 @@ bool IsAttributesListEqualTo(app::ServerClusterInterface & cluster, const std::v
7196// / ClusterImpl cluster(kTestEndpointId, ...);
7297// / ASSERT_TRUE(IsAcceptedCommandsListEqualTo(cluster, { Commands::SomeCommand::kMetadataEntry }));
7398// / ```
74- bool IsAcceptedCommandsListEqualTo (app::ServerClusterInterface & cluster,
75- std::initializer_list<const app::DataModel::AcceptedCommandEntry> expected);
99+ template <class T >
100+ bool IsAcceptedCommandsListEqualTo (app::ServerClusterInterface & cluster, const T & expected)
101+ {
102+ VerifyOrDie (cluster.GetPaths ().size () == 1 );
103+ auto path = cluster.GetPaths ()[0 ];
104+ ReadOnlyBufferBuilder<app::DataModel::AcceptedCommandEntry> commandsBuilder;
105+ if (CHIP_ERROR err = cluster.AcceptedCommands (path, commandsBuilder); err != CHIP_NO_ERROR )
106+ {
107+ ChipLogError (Test, " Failed to get accepted commands list from cluster. Error: %" CHIP_ERROR_FORMAT , err.Format ());
108+ return false ;
109+ }
110+
111+ ReadOnlyBufferBuilder<app::DataModel::AcceptedCommandEntry> expectedBuilder;
112+
113+ SuccessOrDie (expectedBuilder.EnsureAppendCapacity (std::size (expected)));
114+ for (const auto & entry : expected)
115+ {
116+ SuccessOrDie (expectedBuilder.Append (entry));
117+ }
118+
119+ return EqualAcceptedCommandSets (commandsBuilder.TakeBuffer (), expectedBuilder.TakeBuffer ());
120+ }
121+
122+ // Overload for std::initializer_list to not get "template argument deduction failed" when calling
123+ // `IsAcceptedCommandsListEqualTo(cluster, {...})`
124+ template <typename T = const app::DataModel::AcceptedCommandEntry>
125+ bool IsAcceptedCommandsListEqualTo (app::ServerClusterInterface & cluster, std::initializer_list<T> expected)
126+ {
127+ return IsAcceptedCommandsListEqualTo<std::initializer_list<T>>(cluster, expected);
128+ }
76129
77130// / Compares the generated commands of a cluster against an expected set.
78131// /
@@ -81,7 +134,8 @@ bool IsAcceptedCommandsListEqualTo(app::ServerClusterInterface & cluster,
81134// /
82135// / Parameters:
83136// / cluster - The cluster interface to test.
84- // / expected - An initializer list of expected generated command entries. May be empty.
137+ // / expected - initializer_list or any other iterable of expected generated command entries supporting `std::size(container)`
138+ // / syntax. May be empty.
85139// /
86140// / @note This function will assert (die) if `cluster.GetPaths()` does not return exactly one path.
87141// /
@@ -90,7 +144,36 @@ bool IsAcceptedCommandsListEqualTo(app::ServerClusterInterface & cluster,
90144// / ClusterImpl cluster(kTestEndpointId, ...);
91145// / ASSERT_TRUE(IsGeneratedCommandsListEqualTo(cluster, { Commands::SomeCommandResponse::kMetadataEntry }));
92146// / ```
93- bool IsGeneratedCommandsListEqualTo (app::ServerClusterInterface & cluster, std::initializer_list<const CommandId> expected);
147+ template <class T >
148+ bool IsGeneratedCommandsListEqualTo (app::ServerClusterInterface & cluster, const T & expected)
149+ {
150+ VerifyOrDie (cluster.GetPaths ().size () == 1 );
151+ auto path = cluster.GetPaths ()[0 ];
152+ ReadOnlyBufferBuilder<CommandId> commandsBuilder;
153+ if (CHIP_ERROR err = cluster.GeneratedCommands (path, commandsBuilder); err != CHIP_NO_ERROR )
154+ {
155+ ChipLogError (Test, " Failed to get generated commands list from cluster. Error: %" CHIP_ERROR_FORMAT , err.Format ());
156+ return false ;
157+ }
158+
159+ ReadOnlyBufferBuilder<CommandId> expectedBuilder;
160+
161+ SuccessOrDie (expectedBuilder.EnsureAppendCapacity (std::size (expected)));
162+ for (const auto & entry : expected)
163+ {
164+ SuccessOrDie (expectedBuilder.Append (entry));
165+ }
166+
167+ return EqualGeneratedCommandSets (commandsBuilder.TakeBuffer (), expectedBuilder.TakeBuffer ());
168+ }
169+
170+ // Overload for std::initializer_list to not get "template argument deduction failed" when calling
171+ // `IsGeneratedCommandsListEqualTo(cluster, {...})`
172+ template <typename T = const CommandId>
173+ bool IsGeneratedCommandsListEqualTo (app::ServerClusterInterface & cluster, std::initializer_list<T> expected)
174+ {
175+ return IsGeneratedCommandsListEqualTo<std::initializer_list<T>>(cluster, expected);
176+ }
94177
95178} // namespace Testing
96179} // namespace chip
0 commit comments