Skip to content

Commit 080c3fd

Browse files
committed
Container/MeshNodePtr: contains
1 parent 2fcb534 commit 080c3fd

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

modules/Container/MeshNetwork.mpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,16 @@ export namespace CppUtils::Container
265265
return Branch{m_node, key, {}};
266266
}
267267

268+
[[nodiscard]] auto contains(const Key& key) const -> bool
269+
{
270+
if (not m_node)
271+
return false;
272+
273+
auto sharedLocker = m_node->sharedAccess();
274+
const auto& branches = sharedLocker->value.branches;
275+
return branches.contains(key);
276+
}
277+
268278
[[nodiscard]] auto operator->(this auto&& self)
269279
{
270280
return self.m_node.get();

tests/Container/MeshNetwork.mpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,21 @@ export namespace CppUtils::UnitTest::Container::MeshNetwork
424424
suite.expectEqual(expectedValue.value(), "updated");
425425
});
426426

427+
suite.addTest("Contains", [&] {
428+
auto root = StringMeshNodePtr::makeRoot("root");
429+
auto child = StringMeshNodePtr::make("child");
430+
root["Branch"] >> child;
431+
432+
suite.expect(root.contains("Branch"));
433+
suite.expect(not root.contains("NonExistentBranch"));
434+
});
435+
427436
suite.addTest("Get node", [&] {
428437
auto root = StringMeshNodePtr::makeRoot("root");
429438
auto child = StringMeshNodePtr::make("child");
430-
root["Children"] >> child;
439+
root["Branch"] >> child;
431440

432-
auto childExpected = root["Children"][0];
441+
auto childExpected = root["Branch"][0];
433442

434443
suite.expect(childExpected.has_value());
435444
suite.expectEqual(childExpected.value().getValue().value(), "child");
@@ -438,7 +447,7 @@ export namespace CppUtils::UnitTest::Container::MeshNetwork
438447
suite.addTest("Get node: Non-existent branch", [&] {
439448
auto root = StringMeshNodePtr::makeRoot("root");
440449
auto child = StringMeshNodePtr::make("child");
441-
root["Children"] >> child;
450+
root["Branch"] >> child;
442451

443452
auto invalidKeyExpected = root["NonExistentBranch"][0];
444453

@@ -449,9 +458,9 @@ export namespace CppUtils::UnitTest::Container::MeshNetwork
449458
suite.addTest("Get node: Invalid index", [&] {
450459
auto root = StringMeshNodePtr::makeRoot("root");
451460
auto child = StringMeshNodePtr::make("child");
452-
root["Children"] >> child;
461+
root["Branch"] >> child;
453462

454-
auto invalidIndexExpected = root["Children"][1];
463+
auto invalidIndexExpected = root["Branch"][1];
455464

456465
suite.expect(not invalidIndexExpected.has_value());
457466
suite.expectEqual(invalidIndexExpected.error(), "Branch index out of bounds"sv);

0 commit comments

Comments
 (0)