From 1cd25faa7a5b456434781adec0262e28694d923e Mon Sep 17 00:00:00 2001 From: clockblocker Date: Fri, 27 Nov 2026 09:15:58 +0100 Subject: [PATCH] Fix XML get_field_from_array index increment and write() indent forwarding --- include/rfl/xml/Reader.hpp | 4 +- include/rfl/xml/write.hpp | 2 +- tests/xml/test_array_and_indent_fix.cpp | 50 +++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 tests/xml/test_array_and_indent_fix.cpp diff --git a/include/rfl/xml/Reader.hpp b/include/rfl/xml/Reader.hpp index 6ef06975b..c3eb1f6d7 100644 --- a/include/rfl/xml/Reader.hpp +++ b/include/rfl/xml/Reader.hpp @@ -64,12 +64,12 @@ struct Reader { const size_t _idx, const InputArrayType& _arr) const noexcept { const auto name = _arr.node_.name(); size_t i = 0; - for (auto node = _arr.node_; node; node = node.next_sibling(name)) { + for (auto node = _arr.node_; node; node = node.next_sibling(name), ++i) { if (i == _idx) { return InputVarType(node); } } - return error("Index " + std::to_string(_idx) + " of of bounds."); + return error("Index " + std::to_string(_idx) + " out of bounds."); } /// Retrieves a field from an XML object by name. diff --git a/include/rfl/xml/write.hpp b/include/rfl/xml/write.hpp index c71d937a6..51841e172 100644 --- a/include/rfl/xml/write.hpp +++ b/include/rfl/xml/write.hpp @@ -87,7 +87,7 @@ template std::string write(const auto& _obj, const std::string& _indent = " ") { std::stringstream stream; - write<_root, Ps...>(_obj, stream); + write<_root, Ps...>(_obj, stream, _indent); return stream.str(); } diff --git a/tests/xml/test_array_and_indent_fix.cpp b/tests/xml/test_array_and_indent_fix.cpp new file mode 100644 index 000000000..de468f196 --- /dev/null +++ b/tests/xml/test_array_and_indent_fix.cpp @@ -0,0 +1,50 @@ +#include + +#include +#include +#include +#include + +namespace test_xml_array_and_indent_fix { + +struct Person { + std::string name; + int age; +}; + +TEST(xml, test_get_field_from_array_index) { + pugi::xml_document doc; + ASSERT_TRUE(doc.load_string("abc")); + + const rfl::xml::Reader reader; + const auto arr = + reader.to_array(rfl::xml::Reader::InputVarType(doc.child("root").child("item"))); + ASSERT_TRUE(arr.has_value()); + + const auto f0 = reader.get_field_from_array(0, *arr); + const auto f1 = reader.get_field_from_array(1, *arr); + const auto f2 = reader.get_field_from_array(2, *arr); + const auto f3 = reader.get_field_from_array(3, *arr); + + ASSERT_TRUE(f0.has_value()); + ASSERT_TRUE(f1.has_value()); + ASSERT_TRUE(f2.has_value()); + EXPECT_FALSE(f3.has_value()); + + EXPECT_EQ(reader.to_basic_type(*f0).value(), "a"); + EXPECT_EQ(reader.to_basic_type(*f1).value(), "b"); + EXPECT_EQ(reader.to_basic_type(*f2).value(), "c"); + EXPECT_NE(f3.error().what().find("out of bounds"), std::string::npos); +} + +TEST(xml, test_write_forwards_indent) { + const Person p{.name = "Homer", .age = 45}; + const std::string two_spaces = rfl::xml::write(p, " "); + const std::string compact = rfl::xml::write(p, ""); + + EXPECT_NE(two_spaces.find("\n Homer"), std::string::npos) + << two_spaces; + EXPECT_EQ(compact.find(" "), std::string::npos) << compact; +} + +} // namespace test_xml_array_and_indent_fix