From 61af506f4c9990bf39ef8e8803df56daae25d451 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 27 Sep 2026 16:14:14 +0300 Subject: [PATCH] fix xml.tostring --- dev/tests/parsing.lua | 33 +++++++++++++++++++++++++ src/logic/scripting/lua/libs/libxml.cpp | 9 ++++++- test/coders/xml.cpp | 6 +++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 dev/tests/parsing.lua diff --git a/dev/tests/parsing.lua b/dev/tests/parsing.lua new file mode 100644 index 000000000..3b1a4840d --- /dev/null +++ b/dev/tests/parsing.lua @@ -0,0 +1,33 @@ +local src = "test" +local parsed = xml.parse(src) +asserts.equals(src, xml.tostring(parsed, false)) + +local src = "{\"test\": \"test\", \"sub\": {\"test\": \"test\"}}" +local parsed = json.parse(src) +asserts.equals("test", parsed.test) +asserts.equals("test", parsed.sub.test) + +-- toml +local src = [[ +[section] +key = "value" +# comment +key2 = "other value" +number = 42 +pi = 3.14159 +enabled = true +items = ["a", "b", "c"] +table = { x = 1, y = 2 } +]] +local parsed = toml.parse(src) + +asserts.equals("value", parsed.section.key) +asserts.equals("other value", parsed.section.key2) +asserts.equals(42, parsed.section.number) +asserts.equals(3.14159, parsed.section.pi) +asserts.equals(true, parsed.section.enabled) +asserts.equals("a", parsed.section.items[1]) +asserts.equals("b", parsed.section.items[2]) +asserts.equals("c", parsed.section.items[3]) +asserts.equals(1, parsed.section.table.x) +asserts.equals(2, parsed.section.table.y) diff --git a/src/logic/scripting/lua/libs/libxml.cpp b/src/logic/scripting/lua/libs/libxml.cpp index 4aa11f3dd..575a849cb 100644 --- a/src/logic/scripting/lua/libs/libxml.cpp +++ b/src/logic/scripting/lua/libs/libxml.cpp @@ -30,7 +30,7 @@ static int push_xml(lua::State* L, const xml::xmlelement& elem) { } static std::unique_ptr toxml(lua::State* L) { - lua::getfield(L, TAG_ATTR); + lua::requirefield(L, TAG_ATTR); auto tag = lua::require_lstring(L, -1); lua::pop(L); @@ -39,6 +39,13 @@ static std::unique_ptr toxml(lua::State* L) { for (int i = 0; i < length; i++) { lua::rawgeti(L, i + 1); + if (lua::isstring(L, -1)) { + auto textElement = std::make_unique("#"); + textElement->set("#", std::string(lua::require_lstring(L, -1))); + elem->add(std::move(textElement)); + lua::pop(L); + continue; + } elem->add(toxml(L)); lua::pop(L); } diff --git a/test/coders/xml.cpp b/test/coders/xml.cpp index 1357a4492..a394d40a6 100644 --- a/test/coders/xml.cpp +++ b/test/coders/xml.cpp @@ -22,3 +22,9 @@ TEST(XML, VCM) { throw err; } } + +TEST(XML, TWO_WAYS) { + auto src = "test"; + auto document = xml::parse("", src); + ASSERT_EQ(src, xml::stringify(*document->getRoot(), false)); +}