diff options
| author | ruki <[email protected]> | 2025-11-15 00:48:55 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-11-15 00:48:55 +0800 |
| commit | 29fbfdef99861931d81578eac3434f452efddb8e (patch) | |
| tree | aee48deac545d5d3fb8e1916c0e1c923e6afb40b /tests/modules/xml/test.lua | |
| parent | 100900c957fa0db180ccaf5f1f157a78e9db665d (diff) | |
add xml module
Diffstat (limited to 'tests/modules/xml/test.lua')
| -rw-r--r-- | tests/modules/xml/test.lua | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/modules/xml/test.lua b/tests/modules/xml/test.lua new file mode 100644 index 000000000..eb01aad86 --- /dev/null +++ b/tests/modules/xml/test.lua @@ -0,0 +1,29 @@ +import("core.base.xml") + +function test_parse_basic(t) + local doc = xml.decode([[<?xml version="1.0"?><root id="1"><item>foo</item><item id="2"/></root>]]) + t:are_equal(doc.name, "root") + t:are_equal(doc.attrs.id, "1") + 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[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 compact = xml.encode(doc) + t:are_equal(compact, '<root id="1"><item>foo</item><item id="2"/></root>') + local pretty = xml.encode(doc, {pretty = true, indent = 2}) + local expected = table.concat({ + '<root id="1">', + ' <item>foo</item>', + ' <item id="2"/>', + '</root>' + }, "\n") + t:are_equal(pretty, expected) +end + |
