summaryrefslogtreecommitdiff
path: root/tests/modules/xml/test.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2025-11-15 00:54:20 +0800
committerruki <[email protected]>2025-11-15 00:54:20 +0800
commitd6b3bc59cad34b6279f3df8a2066505ca82a2733 (patch)
treefc5b93ceb41573e14c96c54cc99e1d9f9ec7c11f /tests/modules/xml/test.lua
parent29fbfdef99861931d81578eac3434f452efddb8e (diff)
improve xml module
Diffstat (limited to 'tests/modules/xml/test.lua')
-rw-r--r--tests/modules/xml/test.lua30
1 files changed, 27 insertions, 3 deletions
diff --git a/tests/modules/xml/test.lua b/tests/modules/xml/test.lua
index eb01aad86..80ce486f6 100644
--- a/tests/modules/xml/test.lua
+++ b/tests/modules/xml/test.lua
@@ -7,13 +7,18 @@ function test_parse_basic(t)
t:are_equal(#doc.children, 2)
t:are_equal(doc.children[1].name, "item")
t:are_equal(xml.text_of(doc.children[1]), "foo")
+ t:are_equal(doc.children[1].attrs, nil)
t:are_equal(doc.children[2].attrs.id, "2")
end
function test_encode(t)
- local doc = xml.new("root", {id = "1"}, {
- xml.new("item", {}, {xml.text("foo")}),
- xml.new("item", {id = "2"})
+ local doc = xml.new({
+ name = "root",
+ attrs = {id = "1"},
+ children = {
+ xml.new({name = "item", children = {xml.text("foo")}}),
+ xml.new({name = "item", attrs = {id = "2"}})
+ }
})
local compact = xml.encode(doc)
t:are_equal(compact, '<root id="1"><item>foo</item><item id="2"/></root>')
@@ -27,3 +32,22 @@ function test_encode(t)
t:are_equal(pretty, expected)
end
+function test_special_nodes(t)
+ t:are_equal(xml.encode(xml.comment("note")), "<!--note-->")
+ t:are_equal(xml.encode(xml.cdata("a < b")), "<![CDATA[a < b]]>")
+ t:are_equal(xml.encode(xml.doctype("note SYSTEM \"note.dtd\"")), "<!DOCTYPE note SYSTEM \"note.dtd\">")
+ t:are_equal(xml.encode(xml.empty("br")), "<br/>")
+end
+
+function test_parse_special_nodes(t)
+ local doc = xml.decode([[<root><!--note--><![CDATA[a < b]]><child/></root>]])
+ t:are_equal(doc.children[1].name, "__comment__")
+ t:are_equal(doc.children[1].children[1], "note")
+ t:are_equal(doc.children[2].name, "__cdata__")
+ t:are_equal(doc.children[2].children[1], "a < b")
+ t:are_equal(doc.children[3].name, "child")
+ local nodes = xml.decode([[<!DOCTYPE note SYSTEM "note.dtd"><root/>]])
+ t:are_equal(nodes[1].name, "__doctype__")
+ t:are_equal(nodes[2].name, "root")
+end
+