Skip to content

Commit e1411f0

Browse files
LyudmilaKostanyanrestyled-commitsElen777300andy31415
authored
Refactor chip::app::Testing namespace to chip::Testing (#42303)
* Initial commit * Refactor test context usage in various cluster tests to use `chip::Testing` namespace instead of `chip::Test`. * Refactor test cases to use `SetMockNodeConfig` directly from the `Testing` namespace * Refactor ClusterTester.h * Update test files to consistently use `chip::Testing` namespace for test context and cluster tester instances. * Update references from `chip::Test` to `chip::Testing` across multiple files for consistency in test context usage. * Refactor test files to replace all instances of `chip::Test` with `chip::Testing` for improved consistency and clarity in test context usage. * Refactor AppTestContext.h * Refactor test-interaction-model-api.h for improved readability and consistency in formatting. * Refactor MessagingContext.h to improve code readability * Refactor test files to replace all instances of `chip::Test` with `chip::Testing` for improved consistency and clarity in test context usage. * Refactor LoopbackTransport and IOContext classes for improved readability * Refactor test files to consistently use `chip::Testing` namespace, enhancing clarity and uniformity in test context usage across multiple test files. * Refactor ICDConfigurationData and related test access class to consistently use `chip::Testing` namespace, enhancing clarity and uniformity in test context usage. * Update test files to consistently use `chip::Testing` namespace, enhancing clarity and uniformity in test context usage across various components. * Refactor test files to consistently use `chip::Testing` namespace, improving clarity and uniformity in test context usage across DataModelFixtures, TestRead, and TestCodeDrivenDataModelProvider components. * Refactor test files to replace all instances of `chip::Test` with `chip::Testing`, ensuring consistency and clarity in test context usage across various components. * Refactor attribute-storage.cpp to replace `namespace Test` with `namespace chip::Testing`, ensuring consistency in test context usage across the file. * Restyled by whitespace * Restyled by clang-format * Remove unnecessary `using namespace chip::Testing;` from TestRead.cpp, improving clarity in the test context. * Update TestMessagingLayer to use `chip::Testing` namespace for consistency in test context usage. * Restyled by clang-format * Refactor test files to consistently use `DispatchCount` and `GetLastDispatchPath` functions, enhancing clarity and uniformity in test context usage across TestServerClusterShim and TestICDManager components. * Refactor test-ember-api.h to improve namespace formatting and enhance clarity in the test context usage. * Refactor TestExchange to use `Testing::LoopbackMessagingContext` directly, enhancing consistency in test context usage. * Update src/app/util/mock/attribute-storage.cpp Co-authored-by: Elen777300 <[email protected]> * Restyled by clang-format * Refactor TestPASESession to directly use `Testing::LoopbackMessagingContext`, improving consistency in test context usage. * Refactor TestCodegenModelViaMocks to use `Protocols::InteractionModel::Status` directly, improving consistency in status handling within tests. * Refactor MockNodeConfig to improve code consistency by replacing `internal::kDefaultStringSize` with `kDefaultStringSize` and streamline constructor initializations in MockClusterConfig and MockEndpointConfig. * Refactor DataModelFixtures to use `Testing::MockClusterId` and `Testing::MockAttributeId` directly, enhancing consistency in test fixture definitions. * Refactor TestAclEvent to improve namespace usage and enhance consistency by removing redundant `chip::` prefixes, streamlining access to types and functions within the app namespace. * Restyled by clang-format * Remove redundant namespace declaration in attribute-storage.cpp to improve code clarity and maintain consistency in namespace usage. * Refactor test namespaces for consistency across network commissioning and occupancy sensing clusters. Update usage of TestServerClusterContext and ClusterTester to the Testing namespace in relevant test files. * Refactor `DataModelFixtures.h ` * Restyled by clang-format * Remove unused includes in `TestResourceMonitoringCluster.cpp` * Refactor namespace usage in `Group Key Management Cluster` tests * Refactor namespace, initial commit * Refactor: Move `chip::app::Testing` to `chip::Testing` Move Testing namespace from chip::app::Testing to chip::Testing across the codebase. * Restyled by clang-format * Refactor: Remove redundant `using namespace chip::Testing;` from `TestCodegenModelViaMocks.cpp` --------- Co-authored-by: Restyled.io <[email protected]> Co-authored-by: Elen777300 <[email protected]> Co-authored-by: Andrei Litvin <[email protected]>
1 parent 1b71be4 commit e1411f0

File tree

30 files changed

+107
-123
lines changed

30 files changed

+107
-123
lines changed

docs/testing/unit_testing.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,10 @@ own text context class.
279279
- If your test requires access to private/protected members of the underlying
280280
class you're testing, you'll need to create an accessor class that performs
281281
these operations and is friended to the underlying class. Please name the
282-
class `chip::Test::SomethingTestAccess` where `Something` is the name of the
283-
underlying class whose private/protected members you're trying to access.
284-
Then add `friend class chip::Test::SomethingTestAccess;` to the underlying
285-
class. Make sure your test's BUILD.gn file contains
282+
class `chip::Testing::SomethingTestAccess` where `Something` is the name of
283+
the underlying class whose private/protected members you're trying to
284+
access. Then add `friend class chip::Testing::SomethingTestAccess;` to the
285+
underlying class. Make sure your test's BUILD.gn file contains
286286
`sources = [ "SomethingTestAccess.h" ]`. Before creating a new TestAccess
287287
class, check if one already exists. If it does exist but doesn't expose the
288288
member you need, you can add a function to that class to do so. Note that

examples/common/server-cluster-shim/tests/TestServerClusterShim.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ using namespace chip::Testing;
3636
using namespace chip::app;
3737
using namespace chip::app::Clusters;
3838
using namespace chip::app::DataModel;
39-
using namespace chip::app::Testing;
39+
using namespace chip::Testing;
4040
using namespace chip::Protocols::InteractionModel;
4141
using namespace chip::app::Clusters::Globals::Attributes;
4242

src/app/clusters/electrical-energy-measurement-server/tests/TestElectricalEnergyMeasurementCluster.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ TEST_F(TestElectricalEnergyMeasurementCluster, FeatureAttributeTest)
222222
// Test CumulativeEnergyImported (requires kCumulativeEnergy + kImportedEnergy)
223223
{
224224
const ConcreteDataAttributePath path = { paths[0].mEndpointId, paths[0].mClusterId, CumulativeEnergyImported::Id };
225-
chip::app::Testing::ReadOperation readOperation(path);
225+
chip::Testing::ReadOperation readOperation(path);
226226
std::unique_ptr<AttributeValueEncoder> encoder = readOperation.StartEncoding();
227227
auto result = cluster.ReadAttribute(readOperation.GetRequest(), *encoder);
228228
EXPECT_TRUE(result.IsSuccess());
@@ -231,7 +231,7 @@ TEST_F(TestElectricalEnergyMeasurementCluster, FeatureAttributeTest)
231231
// Test CumulativeEnergyExported (requires kCumulativeEnergy + kExportedEnergy)
232232
{
233233
const ConcreteDataAttributePath path = { paths[0].mEndpointId, paths[0].mClusterId, CumulativeEnergyExported::Id };
234-
chip::app::Testing::ReadOperation readOperation(path);
234+
chip::Testing::ReadOperation readOperation(path);
235235
std::unique_ptr<AttributeValueEncoder> encoder = readOperation.StartEncoding();
236236
auto result = cluster.ReadAttribute(readOperation.GetRequest(), *encoder);
237237
EXPECT_TRUE(result.IsSuccess());
@@ -240,7 +240,7 @@ TEST_F(TestElectricalEnergyMeasurementCluster, FeatureAttributeTest)
240240
// Test PeriodicEnergyImported (requires kPeriodicEnergy + kImportedEnergy)
241241
{
242242
const ConcreteDataAttributePath path = { paths[0].mEndpointId, paths[0].mClusterId, PeriodicEnergyImported::Id };
243-
chip::app::Testing::ReadOperation readOperation(path);
243+
chip::Testing::ReadOperation readOperation(path);
244244
std::unique_ptr<AttributeValueEncoder> encoder = readOperation.StartEncoding();
245245
auto result = cluster.ReadAttribute(readOperation.GetRequest(), *encoder);
246246
EXPECT_TRUE(result.IsSuccess());
@@ -249,7 +249,7 @@ TEST_F(TestElectricalEnergyMeasurementCluster, FeatureAttributeTest)
249249
// Test PeriodicEnergyExported (requires kPeriodicEnergy + kExportedEnergy)
250250
{
251251
const ConcreteDataAttributePath path = { paths[0].mEndpointId, paths[0].mClusterId, PeriodicEnergyExported::Id };
252-
chip::app::Testing::ReadOperation readOperation(path);
252+
chip::Testing::ReadOperation readOperation(path);
253253
std::unique_ptr<AttributeValueEncoder> encoder = readOperation.StartEncoding();
254254
auto result = cluster.ReadAttribute(readOperation.GetRequest(), *encoder);
255255
EXPECT_TRUE(result.IsSuccess());
@@ -258,7 +258,7 @@ TEST_F(TestElectricalEnergyMeasurementCluster, FeatureAttributeTest)
258258
// Test CumulativeEnergyReset (requires kCumulativeEnergy)
259259
{
260260
const ConcreteDataAttributePath path = { paths[0].mEndpointId, paths[0].mClusterId, CumulativeEnergyReset::Id };
261-
chip::app::Testing::ReadOperation readOperation(path);
261+
chip::Testing::ReadOperation readOperation(path);
262262
std::unique_ptr<AttributeValueEncoder> encoder = readOperation.StartEncoding();
263263
auto result = cluster.ReadAttribute(readOperation.GetRequest(), *encoder);
264264
EXPECT_TRUE(result.IsSuccess());

src/app/clusters/general-diagnostics-server/tests/TestGeneralDiagnosticsCluster.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ using namespace chip::app;
3939
using namespace chip::app::Clusters;
4040
using namespace chip::app::Clusters::GeneralDiagnostics::Attributes;
4141
using namespace chip::app::DataModel;
42-
using namespace chip::Test;
4342
using namespace chip::Testing;
4443

4544
template <class T>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ TEST_F(TestGroupcastCluster, TestJoinGroupCommand)
115115
cmdData.gracePeriod = MakeOptional(0U);
116116
cmdData.useAuxiliaryACL = MakeOptional(true);
117117

118-
chip::app::Testing::MockCommandHandler cmdHandler;
118+
chip::Testing::MockCommandHandler cmdHandler;
119119
chip::Testing::ClusterTester tester(cluster);
120120
auto result = tester.Invoke(Commands::JoinGroup::Id, cmdData);
121121
ASSERT_TRUE(result.status.has_value());

src/app/clusters/network-commissioning/tests/TestNetworkCommissioningCluster.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ using namespace chip::app::Clusters::NetworkCommissioning::Attributes;
4646

4747
using chip::app::AttributeValueDecoder;
4848
using chip::app::DataModel::AttributeEntry;
49-
using chip::app::Testing::kAdminSubjectDescriptor;
50-
using chip::app::Testing::WriteOperation;
49+
using chip::Testing::kAdminSubjectDescriptor;
50+
using chip::Testing::WriteOperation;
5151

5252
class NoopBreadcrumbTracker : public BreadCrumbTracker
5353
{

src/app/clusters/network-commissioning/tests/TestNetworkCommissioningClusterEthernet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ using namespace chip::app::Clusters::NetworkCommissioning;
4848

4949
using chip::app::AttributeValueDecoder;
5050
using chip::app::DataModel::AttributeEntry;
51-
using chip::app::Testing::kAdminSubjectDescriptor;
52-
using chip::app::Testing::WriteOperation;
51+
using chip::Testing::kAdminSubjectDescriptor;
52+
using chip::Testing::WriteOperation;
5353

5454
class NoopBreadcrumbTracker : public BreadCrumbTracker
5555
{

src/app/clusters/testing/ClusterTester.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,18 @@ class ClusterTester
9292
// Store the read operation in a vector<std::unique_ptr<...>> to ensure its lifetime
9393
// using std::unique_ptr because ReadOperation is non-copyable and non-movable
9494
// vector reallocation is not an issue since we store unique_ptrs
95-
std::unique_ptr<app::Testing::ReadOperation> readOperation =
96-
std::make_unique<app::Testing::ReadOperation>(path.mEndpointId, path.mClusterId, attr_id);
95+
std::unique_ptr<chip::Testing::ReadOperation> readOperation =
96+
std::make_unique<chip::Testing::ReadOperation>(path.mEndpointId, path.mClusterId, attr_id);
9797

9898
mReadOperations.push_back(std::move(readOperation));
99-
app::Testing::ReadOperation & readOperationRef = *mReadOperations.back().get();
99+
chip::Testing::ReadOperation & readOperationRef = *mReadOperations.back().get();
100100

101101
std::unique_ptr<app::AttributeValueEncoder> encoder = readOperationRef.StartEncoding();
102102
app::DataModel::ActionReturnStatus status = mCluster.ReadAttribute(readOperationRef.GetRequest(), *encoder);
103103
VerifyOrReturnError(status.IsSuccess(), status);
104104
ReturnErrorOnFailure(readOperationRef.FinishEncoding());
105105

106-
std::vector<app::Testing::DecodedAttributeData> attributeData;
106+
std::vector<chip::Testing::DecodedAttributeData> attributeData;
107107
ReturnErrorOnFailure(readOperationRef.GetEncodedIBs().Decode(attributeData));
108108
VerifyOrReturnError(attributeData.size() == 1u, CHIP_ERROR_INCORRECT_STATE);
109109

@@ -124,7 +124,7 @@ class ClusterTester
124124
VerifyOrReturnError(paths.size() == 1u, CHIP_ERROR_INCORRECT_STATE);
125125

126126
app::ConcreteAttributePath path(paths[0].mEndpointId, paths[0].mClusterId, attr);
127-
app::Testing::WriteOperation writeOp(path);
127+
chip::Testing::WriteOperation writeOp(path);
128128

129129
// Create a stable object on the stack
130130
Access::SubjectDescriptor subjectDescriptor{ mHandler.GetAccessingFabricIndex() };
@@ -293,9 +293,9 @@ class ClusterTester
293293
// If protocol or test requirements change, this value may need to be increased.
294294
static constexpr size_t kTlvBufferSize = 256;
295295

296-
app::Testing::MockCommandHandler mHandler;
296+
chip::Testing::MockCommandHandler mHandler;
297297
uint8_t mTlvBuffer[kTlvBufferSize];
298-
std::vector<std::unique_ptr<app::Testing::ReadOperation>> mReadOperations;
298+
std::vector<std::unique_ptr<chip::Testing::ReadOperation>> mReadOperations;
299299
};
300300

301301
} // namespace Testing

src/app/clusters/testing/MockCommandHandler.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,32 @@
2626
#include <transport/raw/MessageHeader.h>
2727

2828
namespace chip {
29-
namespace app {
3029
namespace Testing {
3130

32-
CHIP_ERROR MockCommandHandler::FallibleAddStatus(const ConcreteCommandPath & aRequestCommandPath,
31+
CHIP_ERROR MockCommandHandler::FallibleAddStatus(const app::ConcreteCommandPath & aRequestCommandPath,
3332
const Protocols::InteractionModel::ClusterStatusCode & aStatus,
3433
const char * context)
3534
{
3635
mStatuses.emplace_back(StatusRecord{ aRequestCommandPath, aStatus, context });
3736
return CHIP_NO_ERROR;
3837
}
3938

40-
void MockCommandHandler::AddStatus(const ConcreteCommandPath & aRequestCommandPath,
39+
void MockCommandHandler::AddStatus(const app::ConcreteCommandPath & aRequestCommandPath,
4140
const Protocols::InteractionModel::ClusterStatusCode & aStatus, const char * context)
4241
{
4342
CHIP_ERROR err = FallibleAddStatus(aRequestCommandPath, aStatus, context);
4443
VerifyOrDie(err == CHIP_NO_ERROR);
4544
}
4645

47-
CHIP_ERROR MockCommandHandler::AddResponseData(const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
48-
const DataModel::EncodableToTLV & aEncodable)
46+
CHIP_ERROR MockCommandHandler::AddResponseData(const app::ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
47+
const app::DataModel::EncodableToTLV & aEncodable)
4948
{
5049
chip::System::PacketBufferHandle handle = chip::MessagePacketBuffer::New(chip::kMaxAppMessageLen);
5150
VerifyOrReturnError(!handle.IsNull(), CHIP_ERROR_NO_MEMORY);
5251

5352
TLV::TLVWriter baseWriter;
5453
baseWriter.Init(handle->Start(), handle->MaxDataLength());
55-
DataModel::FabricAwareTLVWriter writer(baseWriter, mFabricIndex);
54+
app::DataModel::FabricAwareTLVWriter writer(baseWriter, mFabricIndex);
5655
TLV::TLVType ct;
5756

5857
ReturnErrorOnFailure(writer.mTLVWriter.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, ct));
@@ -64,8 +63,8 @@ CHIP_ERROR MockCommandHandler::AddResponseData(const ConcreteCommandPath & aRequ
6463
return CHIP_NO_ERROR;
6564
}
6665

67-
void MockCommandHandler::AddResponse(const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
68-
const DataModel::EncodableToTLV & aEncodable)
66+
void MockCommandHandler::AddResponse(const app::ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
67+
const app::DataModel::EncodableToTLV & aEncodable)
6968
{
7069
CHIP_ERROR err = AddResponseData(aRequestCommandPath, aResponseCommandId, aEncodable);
7170
VerifyOrDie(err == CHIP_NO_ERROR);
@@ -93,5 +92,4 @@ CHIP_ERROR MockCommandHandler::GetResponseReader(TLV::TLVReader & reader, size_t
9392
}
9493

9594
} // namespace Testing
96-
} // namespace app
9795
} // namespace chip

src/app/clusters/testing/MockCommandHandler.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,48 +32,47 @@
3232
#include <vector>
3333

3434
namespace chip {
35-
namespace app {
3635
namespace Testing {
3736

3837
constexpr FabricIndex kTestFabricIndex = static_cast<FabricIndex>(151);
3938

4039
// Mock class that simulates CommandHandler behavior for unit testing, allowing capture and
4140
// verification of responses and statuses without real network interactions.
42-
class MockCommandHandler : public CommandHandler
41+
class MockCommandHandler : public app::CommandHandler
4342
{
4443
public:
4544
struct ResponseRecord
4645
{
4746
CommandId commandId;
4847
System::PacketBufferHandle encodedData;
49-
ConcreteCommandPath path;
48+
app::ConcreteCommandPath path;
5049
};
5150

5251
struct StatusRecord
5352
{
54-
ConcreteCommandPath path;
53+
app::ConcreteCommandPath path;
5554
Protocols::InteractionModel::ClusterStatusCode status;
5655
const char * context;
5756
};
5857

5958
~MockCommandHandler() override = default;
6059

61-
CHIP_ERROR FallibleAddStatus(const ConcreteCommandPath & aRequestCommandPath,
60+
CHIP_ERROR FallibleAddStatus(const app::ConcreteCommandPath & aRequestCommandPath,
6261
const Protocols::InteractionModel::ClusterStatusCode & aStatus,
6362
const char * context = nullptr) override;
6463

65-
void AddStatus(const ConcreteCommandPath & aRequestCommandPath, const Protocols::InteractionModel::ClusterStatusCode & aStatus,
66-
const char * context = nullptr) override;
64+
void AddStatus(const app::ConcreteCommandPath & aRequestCommandPath,
65+
const Protocols::InteractionModel::ClusterStatusCode & aStatus, const char * context = nullptr) override;
6766

6867
FabricIndex GetAccessingFabricIndex() const override { return mFabricIndex; }
6968

7069
// Encodes and stores response data, returning error if encoding fails (fallible version for robust test handling).
71-
CHIP_ERROR AddResponseData(const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
72-
const DataModel::EncodableToTLV & aEncodable) override;
70+
CHIP_ERROR AddResponseData(const app::ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
71+
const app::DataModel::EncodableToTLV & aEncodable) override;
7372

7473
// Encodes and stores response data, without error return (non-fallible version that assumes successful encoding in tests).
75-
void AddResponse(const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
76-
const DataModel::EncodableToTLV & aEncodable) override;
74+
void AddResponse(const app::ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
75+
const app::DataModel::EncodableToTLV & aEncodable) override;
7776

7877
bool IsTimedInvoke() const override { return false; }
7978
void FlushAcksRightAwayOnSlowCommand() override {}
@@ -133,5 +132,4 @@ class MockCommandHandler : public CommandHandler
133132
};
134133

135134
} // namespace Testing
136-
} // namespace app
137135
} // namespace chip

0 commit comments

Comments
 (0)