Hallucinate some xml helper functions in lua

This commit is contained in:
2025-12-19 11:42:39 +01:00
parent fff8869aff
commit f1ea0f9156
2 changed files with 268 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
dofile("luahelper.lua")
-- Test cases for XML helper functions
-- Mock XML structure for testing
local testXML = {
_tag = "root",
_attr = {version = "1.0"},
_children = {
{
_tag = "item",
_attr = {name = "sword", damage = "10", weight = "5.5"},
_text = "Iron Sword"
},
{
_tag = "item",
_attr = {name = "shield", defense = "8"},
_children = {
{
_tag = "material",
_attr = {type = "steel", durability = "100"}
}
}
},
{
_tag = "container",
_children = {
{_tag = "item", _attr = {name = "potion", healing = "20"}},
{_tag = "item", _attr = {name = "scroll"}}
}
}
}
}
print("=== XML Helper Functions Tests ===\n")
-- Test findElements
print("1. Find all 'item' elements:")
local items = findElements(testXML, "item")
print(" Found " .. #items .. " items")
for i, item in ipairs(items) do
print(" - " .. (item._attr.name or "unnamed"))
end
-- Test getNumAttr and setNumAttr
print("\n2. Get/Set numeric attributes:")
local sword = items[1]
local damage = getNumAttr(sword, "damage")
print(" Sword damage: " .. tostring(damage))
setNumAttr(sword, "damage", damage * 2)
print(" After doubling: " .. sword._attr.damage)
-- Test modifyNumAttr
print("\n3. Modify numeric attribute:")
modifyNumAttr(sword, "weight", function(val) return val * 1.5 end)
print(" Sword weight modified: " .. sword._attr.weight)
-- Test filterElements
print("\n4. Filter elements with 'healing' attribute:")
local healingItems = filterElements(testXML, function(elem)
return hasAttr(elem, "healing")
end)
print(" Found " .. #healingItems .. " healing items")
-- Test visitElements
print("\n5. Visit all elements:")
local count = 0
visitElements(testXML, function(elem, depth, path)
count = count + 1
if depth <= 2 then
print(" " .. string.rep(" ", depth) .. elem._tag .. " @ " .. path)
end
end)
print(" Total elements visited: " .. count)
-- Test getText and setText
print("\n6. Text content:")
print(" Original text: " .. (getText(sword) or "none"))
setText(sword, "Modified Sword")
print(" New text: " .. getText(sword))
-- Test hasAttr and getAttr
print("\n7. Check attributes:")
print(" Sword has 'damage': " .. tostring(hasAttr(sword, "damage")))
print(" Sword has 'magic': " .. tostring(hasAttr(sword, "magic")))
print(" Sword name: " .. (getAttr(sword, "name") or "none"))
print("\n=== Tests Complete ===")