Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/rfl/xml/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion include/rfl/xml/write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ template <internal::StringLiteral _root = internal::StringLiteral(""),
class... Ps>
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();
}

Expand Down
50 changes: 50 additions & 0 deletions tests/xml/test_array_and_indent_fix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <gtest/gtest.h>

#include <pugixml.hpp>
#include <rfl.hpp>
#include <rfl/xml.hpp>
#include <string>

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("<root><item>a</item><item>b</item><item>c</item></root>"));

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<std::string>(*f0).value(), "a");
EXPECT_EQ(reader.to_basic_type<std::string>(*f1).value(), "b");
EXPECT_EQ(reader.to_basic_type<std::string>(*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 <name>Homer</name>"), std::string::npos)
<< two_spaces;
EXPECT_EQ(compact.find(" <name>"), std::string::npos) << compact;
}

} // namespace test_xml_array_and_indent_fix