Merge pull request #994 from MihailRis/fix-xml.tostring

fix xml.tostring
This commit is contained in:
MihailRis 2026-09-27 16:54:11 +03:00 • committed by GitHub
commit 00825f485c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 47 additions and 1 deletions

33
dev/tests/parsing.lua Normal file
View file

@ -0,0 +1,33 @@
local src = "<test><sub>test</sub></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)

View file

@ -30,7 +30,7 @@ static int push_xml(lua::State* L, const xml::xmlelement& elem) {
}
static std::unique_ptr<xml::xmlelement> 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<xml::xmlelement> 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<xml::xmlelement>("#");
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);
}

View file

@ -22,3 +22,9 @@ TEST(XML, VCM) {
throw err;
}
}
TEST(XML, TWO_WAYS) {
auto src = "<test><sub>test</sub></test>";
auto document = xml::parse("<test>", src);
ASSERT_EQ(src, xml::stringify(*document->getRoot(), false));
}