summaryrefslogtreecommitdiff
path: root/tests/modules/xml/test.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2025-11-15 00:14:29 +0800
committerruki <[email protected]>2025-11-15 00:14:29 +0800
commit3d6be9e48846d619bf389ee9a16157281bafa87b (patch)
treee6e9071fa08b60f82864cc4e97eadaed7c209ba0 /tests/modules/xml/test.lua
parentd1dc6ffca3a29e6e071fa7a16860a579f3ad552c (diff)
fix test
Diffstat (limited to 'tests/modules/xml/test.lua')
-rw-r--r--tests/modules/xml/test.lua41
1 files changed, 29 insertions, 12 deletions
diff --git a/tests/modules/xml/test.lua b/tests/modules/xml/test.lua
index 80ce486f6..271865338 100644
--- a/tests/modules/xml/test.lua
+++ b/tests/modules/xml/test.lua
@@ -1,7 +1,8 @@
import("core.base.xml")
-function test_parse_basic(t)
+function test_decode_basic(t)
local doc = xml.decode([[<?xml version="1.0"?><root id="1"><item>foo</item><item id="2"/></root>]])
+ t:are_equal(doc.kind, "element")
t:are_equal(doc.name, "root")
t:are_equal(doc.attrs.id, "1")
t:are_equal(#doc.children, 2)
@@ -11,7 +12,7 @@ function test_parse_basic(t)
t:are_equal(doc.children[2].attrs.id, "2")
end
-function test_encode(t)
+function test_encode_basic(t)
local doc = xml.new({
name = "root",
attrs = {id = "1"},
@@ -32,22 +33,38 @@ function test_encode(t)
t:are_equal(pretty, expected)
end
-function test_special_nodes(t)
+function test_encode_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")
+function test_decode_special_nodes(t)
+ local doc = xml.decode([=[<root><!--note--><![CDATA[a < b]]><child/></root>]=])
+ t:are_equal(doc.children[1].kind, "comment")
+ t:are_equal(doc.children[1].text, "note")
+ t:are_equal(doc.children[2].kind, "cdata")
+ t:are_equal(doc.children[2].text, "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")
+ local nodes = xml.decode([=[<!DOCTYPE note SYSTEM "note.dtd"><root/>]=])
+ t:are_equal(nodes.kind, "element")
+ t:are_equal(nodes.name, "root")
+ t:are_equal(nodes.prolog[1].kind, "doctype")
+end
+
+function test_load_save(t)
+ local tmpdir = os.tmpdir()
+ local filepath = path.join(tmpdir, "xml_test.xml")
+ local doc = xml.new({
+ name = "root",
+ attrs = {id = "1"},
+ children = {xml.text("hello")}
+ })
+ assert(xml.save(filepath, doc, {pretty = true}))
+ local reloaded = xml.load(filepath)
+ t:are_equal(reloaded.name, "root")
+ t:are_equal(xml.text_of(reloaded), "hello")
+ os.tryrm(filepath)
end