]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#4638] Document MapElement::get(int) master
authorAndrei Pavel <andrei@isc.org>
Thu, 6 Aug 2026 11:42:30 +0000 (14:42 +0300)
committerAndrei Pavel <andrei@isc.org>
Thu, 6 Aug 2026 11:42:30 +0000 (14:42 +0300)
src/lib/cc/data.h
src/lib/cc/tests/data_unittests.cc

index aa30818d8407bb985e05cd09d5eff665e61c1982..df50c36288f83b123711d75f61890b5b2965b82e 100644 (file)
@@ -981,9 +981,10 @@ public:
     /// @brief Get the i-th element in the map.
     ///
     /// Useful when required to iterate with an index.
     /// @brief Get the i-th element in the map.
     ///
     /// Useful when required to iterate with an index.
+    /// Returns a string holding the key.
     ///
     /// @param i the position of the element you want to return
     ///
     /// @param i the position of the element you want to return
-    /// @return the element at position i
+    /// @return the key element at position i
     ConstElementPtr get(int const i) const override {
         auto it(m.begin());
         std::advance(it, i);
     ConstElementPtr get(int const i) const override {
         auto it(m.begin());
         std::advance(it, i);
index d44443552ef92024f89834aacb85b06a149849c9..c7ff083593c71ac4d791d363437369d44377a7d1 100644 (file)
@@ -873,6 +873,31 @@ TEST(Element, mapElementRemoveOutOfRange) {
     EXPECT_FALSE(el->get("a"));
 }
 
     EXPECT_FALSE(el->get("a"));
 }
 
+// Verifies that MapElement::get(int) returns the key at the index.
+TEST(Element, mapElementGetByIndex) {
+    ElementPtr el = Element::fromJSON(R"({"alpha": 1, "beta": 2})");
+    ASSERT_EQ(2, static_cast<int>(el->size()));
+
+    // Ordered map: index 0 is key "alpha", index 1 is key "beta".
+    ConstElementPtr g0 = el->get(0);
+    ASSERT_TRUE(g0);
+    EXPECT_EQ(Element::string, g0->getType());
+    EXPECT_EQ("alpha", g0->stringValue());
+
+    ConstElementPtr g1 = el->get(1);
+    ASSERT_TRUE(g1);
+    EXPECT_EQ(Element::string, g1->getType());
+    EXPECT_EQ("beta", g1->stringValue());
+
+    // Mixed value types: string then nested map.
+    el = Element::fromJSON(R"({"name": "foo", "value2": {"number": 42}})");
+    ASSERT_EQ(2, static_cast<int>(el->size()));
+    EXPECT_EQ(Element::string, el->get(0)->getType());
+    EXPECT_EQ("name", el->get(0)->stringValue());
+    EXPECT_EQ(Element::string, el->get(1)->getType());
+    EXPECT_EQ("value2", el->get(1)->stringValue());
+}
+
 TEST(Element, toAndFromWire) {
     // Wire format is now plain JSON.
     EXPECT_EQ("1", Element::create(1)->toWire());
 TEST(Element, toAndFromWire) {
     // Wire format is now plain JSON.
     EXPECT_EQ("1", Element::create(1)->toWire());