Fix up the lua tests
To be less retarded...
This commit is contained in:
@@ -1,87 +1,224 @@
|
|||||||
|
-- Load the helper script
|
||||||
dofile("luahelper.lua")
|
dofile("luahelper.lua")
|
||||||
-- Test cases for XML helper functions
|
|
||||||
|
|
||||||
-- Mock XML structure for testing
|
-- Test helper function
|
||||||
local testXML = {
|
local function assert(condition, message)
|
||||||
_tag = "root",
|
if not condition then error("ASSERTION FAILED: " .. (message or "unknown error")) end
|
||||||
_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
|
end
|
||||||
|
|
||||||
|
local function test(name, fn)
|
||||||
|
local ok, err = pcall(fn)
|
||||||
|
if ok then
|
||||||
|
print("PASS: " .. name)
|
||||||
|
else
|
||||||
|
print("FAIL: " .. name .. " - " .. tostring(err))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Test findElements
|
||||||
|
test("findElements finds all matching elements recursively", function()
|
||||||
|
local testXML = {
|
||||||
|
_tag = "root",
|
||||||
|
_children = {
|
||||||
|
{ _tag = "item", _attr = { name = "sword" } },
|
||||||
|
{ _tag = "item", _attr = { name = "shield" } },
|
||||||
|
{
|
||||||
|
_tag = "container",
|
||||||
|
_children = {
|
||||||
|
{ _tag = "item", _attr = { name = "potion" } },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
local items = findElements(testXML, "item")
|
||||||
|
assert(#items == 3, "Should find 3 items total (recursive)")
|
||||||
|
assert(items[1]._attr.name == "sword", "First item should be sword")
|
||||||
|
assert(items[3]._attr.name == "potion", "Third item should be potion (from nested)")
|
||||||
|
end)
|
||||||
|
|
||||||
-- Test getNumAttr and setNumAttr
|
-- Test getNumAttr and setNumAttr
|
||||||
print("\n2. Get/Set numeric attributes:")
|
test("getNumAttr gets numeric attribute", function()
|
||||||
local sword = items[1]
|
local elem = { _tag = "item", _attr = { damage = "10" } }
|
||||||
local damage = getNumAttr(sword, "damage")
|
local damage = getNumAttr(elem, "damage")
|
||||||
print(" Sword damage: " .. tostring(damage))
|
assert(damage == 10, "Should get damage as number")
|
||||||
setNumAttr(sword, "damage", damage * 2)
|
end)
|
||||||
print(" After doubling: " .. sword._attr.damage)
|
|
||||||
|
test("getNumAttr returns nil for missing attribute", function()
|
||||||
|
local elem = { _tag = "item", _attr = {} }
|
||||||
|
local damage = getNumAttr(elem, "damage")
|
||||||
|
assert(damage == nil, "Should return nil for missing attribute")
|
||||||
|
end)
|
||||||
|
|
||||||
|
test("setNumAttr sets numeric attribute", function()
|
||||||
|
local elem = { _tag = "item", _attr = {} }
|
||||||
|
setNumAttr(elem, "damage", 20)
|
||||||
|
assert(elem._attr.damage == "20", "Should set damage as string")
|
||||||
|
end)
|
||||||
|
|
||||||
-- Test modifyNumAttr
|
-- Test modifyNumAttr
|
||||||
print("\n3. Modify numeric attribute:")
|
test("modifyNumAttr modifies numeric attribute", function()
|
||||||
modifyNumAttr(sword, "weight", function(val) return val * 1.5 end)
|
local elem = { _tag = "item", _attr = { weight = "5.5" } }
|
||||||
print(" Sword weight modified: " .. sword._attr.weight)
|
local modified = modifyNumAttr(elem, "weight", function(val) return val * 2 end)
|
||||||
|
assert(modified == true, "Should return true when modified")
|
||||||
|
assert(elem._attr.weight == "11.0", "Should double weight")
|
||||||
|
end)
|
||||||
|
|
||||||
|
test("modifyNumAttr returns false for missing attribute", function()
|
||||||
|
local elem = { _tag = "item", _attr = {} }
|
||||||
|
local modified = modifyNumAttr(elem, "weight", function(val) return val * 2 end)
|
||||||
|
assert(modified == false, "Should return false when attribute missing")
|
||||||
|
end)
|
||||||
|
|
||||||
-- Test filterElements
|
-- Test filterElements
|
||||||
print("\n4. Filter elements with 'healing' attribute:")
|
test("filterElements filters by predicate", function()
|
||||||
local healingItems = filterElements(testXML, function(elem)
|
local testXML = {
|
||||||
return hasAttr(elem, "healing")
|
_tag = "root",
|
||||||
|
_children = {
|
||||||
|
{ _tag = "item", _attr = { healing = "20" } },
|
||||||
|
{ _tag = "item", _attr = { damage = "10" } },
|
||||||
|
{ _tag = "item", _attr = { healing = "50" } },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
local healingItems = filterElements(testXML, function(elem) return hasAttr(elem, "healing") end)
|
||||||
|
assert(#healingItems == 2, "Should find 2 healing items")
|
||||||
end)
|
end)
|
||||||
print(" Found " .. #healingItems .. " healing items")
|
|
||||||
|
|
||||||
-- Test visitElements
|
-- Test visitElements
|
||||||
print("\n5. Visit all elements:")
|
test("visitElements visits all elements", function()
|
||||||
local count = 0
|
local testXML = {
|
||||||
visitElements(testXML, function(elem, depth, path)
|
_tag = "root",
|
||||||
count = count + 1
|
_children = {
|
||||||
if depth <= 2 then
|
{ _tag = "item" },
|
||||||
print(" " .. string.rep(" ", depth) .. elem._tag .. " @ " .. path)
|
{ _tag = "container", _children = {
|
||||||
end
|
{ _tag = "item" },
|
||||||
|
} },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
local count = 0
|
||||||
|
visitElements(testXML, function(elem) count = count + 1 end)
|
||||||
|
assert(count == 4, "Should visit 4 elements (root + 2 items + container)")
|
||||||
end)
|
end)
|
||||||
print(" Total elements visited: " .. count)
|
|
||||||
|
|
||||||
-- Test getText and setText
|
-- Test getText and setText
|
||||||
print("\n6. Text content:")
|
test("getText gets text content", function()
|
||||||
print(" Original text: " .. (getText(sword) or "none"))
|
local elem = { _tag = "item", _text = "Iron Sword" }
|
||||||
setText(sword, "Modified Sword")
|
local text = getText(elem)
|
||||||
print(" New text: " .. getText(sword))
|
assert(text == "Iron Sword", "Should get text content")
|
||||||
|
end)
|
||||||
|
|
||||||
|
test("setText sets text content", function()
|
||||||
|
local elem = { _tag = "item" }
|
||||||
|
setText(elem, "New Text")
|
||||||
|
assert(elem._text == "New Text", "Should set text content")
|
||||||
|
end)
|
||||||
|
|
||||||
-- Test hasAttr and getAttr
|
-- Test hasAttr and getAttr
|
||||||
print("\n7. Check attributes:")
|
test("hasAttr checks attribute existence", function()
|
||||||
print(" Sword has 'damage': " .. tostring(hasAttr(sword, "damage")))
|
local elem = { _tag = "item", _attr = { damage = "10" } }
|
||||||
print(" Sword has 'magic': " .. tostring(hasAttr(sword, "magic")))
|
assert(hasAttr(elem, "damage") == true, "Should have damage")
|
||||||
print(" Sword name: " .. (getAttr(sword, "name") or "none"))
|
assert(hasAttr(elem, "magic") == false, "Should not have magic")
|
||||||
|
end)
|
||||||
|
|
||||||
print("\n=== Tests Complete ===")
|
test("getAttr gets attribute value", function()
|
||||||
|
local elem = { _tag = "item", _attr = { name = "sword" } }
|
||||||
|
assert(getAttr(elem, "name") == "sword", "Should get name attribute")
|
||||||
|
assert(getAttr(elem, "missing") == nil, "Should return nil for missing")
|
||||||
|
end)
|
||||||
|
|
||||||
|
test("setAttr sets attribute value", function()
|
||||||
|
local elem = { _tag = "item" }
|
||||||
|
setAttr(elem, "name", "sword")
|
||||||
|
assert(elem._attr.name == "sword", "Should set attribute")
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- Test findFirstElement
|
||||||
|
test("findFirstElement finds first direct child", function()
|
||||||
|
local parent = {
|
||||||
|
_tag = "root",
|
||||||
|
_children = {
|
||||||
|
{ _tag = "item", _attr = { id = "1" } },
|
||||||
|
{ _tag = "item", _attr = { id = "2" } },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
local first = findFirstElement(parent, "item")
|
||||||
|
assert(first._attr.id == "1", "Should find first item")
|
||||||
|
end)
|
||||||
|
|
||||||
|
test("findFirstElement returns nil when not found", function()
|
||||||
|
local parent = { _tag = "root", _children = {} }
|
||||||
|
local result = findFirstElement(parent, "item")
|
||||||
|
assert(result == nil, "Should return nil when not found")
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- Test getChildren
|
||||||
|
test("getChildren gets all direct children with tag", function()
|
||||||
|
local parent = {
|
||||||
|
_tag = "root",
|
||||||
|
_children = {
|
||||||
|
{ _tag = "item", _attr = { id = "1" } },
|
||||||
|
{ _tag = "config" },
|
||||||
|
{ _tag = "item", _attr = { id = "2" } },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
local items = getChildren(parent, "item")
|
||||||
|
assert(#items == 2, "Should get 2 items")
|
||||||
|
assert(items[1]._attr.id == "1", "First should have id=1")
|
||||||
|
assert(items[2]._attr.id == "2", "Second should have id=2")
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- Test countChildren
|
||||||
|
test("countChildren counts direct children with tag", function()
|
||||||
|
local parent = {
|
||||||
|
_tag = "root",
|
||||||
|
_children = {
|
||||||
|
{ _tag = "item" },
|
||||||
|
{ _tag = "config" },
|
||||||
|
{ _tag = "item" },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
assert(countChildren(parent, "item") == 2, "Should count 2 items")
|
||||||
|
assert(countChildren(parent, "config") == 1, "Should count 1 config")
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- Test addChild
|
||||||
|
test("addChild adds child element", function()
|
||||||
|
local parent = { _tag = "root", _children = {} }
|
||||||
|
addChild(parent, { _tag = "item" })
|
||||||
|
assert(#parent._children == 1, "Should have 1 child")
|
||||||
|
assert(parent._children[1]._tag == "item", "Child should be item")
|
||||||
|
end)
|
||||||
|
|
||||||
|
test("addChild creates children array if needed", function()
|
||||||
|
local parent = { _tag = "root" }
|
||||||
|
addChild(parent, { _tag = "item" })
|
||||||
|
assert(parent._children ~= nil, "Should create _children")
|
||||||
|
assert(#parent._children == 1, "Should have 1 child")
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- Test removeChildren
|
||||||
|
test("removeChildren removes all matching children", function()
|
||||||
|
local parent = {
|
||||||
|
_tag = "root",
|
||||||
|
_children = {
|
||||||
|
{ _tag = "item" },
|
||||||
|
{ _tag = "config" },
|
||||||
|
{ _tag = "item" },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
local removed = removeChildren(parent, "item")
|
||||||
|
assert(removed == 2, "Should remove 2 items")
|
||||||
|
assert(#parent._children == 1, "Should have 1 child left")
|
||||||
|
assert(parent._children[1]._tag == "config", "Remaining should be config")
|
||||||
|
end)
|
||||||
|
|
||||||
|
test("removeChildren returns 0 when none found", function()
|
||||||
|
local parent = {
|
||||||
|
_tag = "root",
|
||||||
|
_children = { { _tag = "item" } },
|
||||||
|
}
|
||||||
|
local removed = removeChildren(parent, "config")
|
||||||
|
assert(removed == 0, "Should remove 0")
|
||||||
|
assert(#parent._children == 1, "Should still have 1 child")
|
||||||
|
end)
|
||||||
|
|
||||||
|
print("\nAll tests completed!")
|
||||||
|
|||||||
@@ -361,18 +361,16 @@ modified = false
|
|||||||
--- @return table Array of matching elements
|
--- @return table Array of matching elements
|
||||||
function findElements(root, tagName)
|
function findElements(root, tagName)
|
||||||
local results = {}
|
local results = {}
|
||||||
|
|
||||||
local function search(element)
|
local function search(element)
|
||||||
if element._tag == tagName then
|
if element._tag == tagName then table.insert(results, element) end
|
||||||
table.insert(results, element)
|
|
||||||
end
|
|
||||||
if element._children then
|
if element._children then
|
||||||
for _, child in ipairs(element._children) do
|
for _, child in ipairs(element._children) do
|
||||||
search(child)
|
search(child)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
search(root)
|
search(root)
|
||||||
return results
|
return results
|
||||||
end
|
end
|
||||||
@@ -409,9 +407,7 @@ end
|
|||||||
--- @param attrName string Attribute name
|
--- @param attrName string Attribute name
|
||||||
--- @param value number Numeric value to set
|
--- @param value number Numeric value to set
|
||||||
function setNumAttr(element, attrName, value)
|
function setNumAttr(element, attrName, value)
|
||||||
if not element._attr then
|
if not element._attr then element._attr = {} end
|
||||||
element._attr = {}
|
|
||||||
end
|
|
||||||
element._attr[attrName] = tostring(value)
|
element._attr[attrName] = tostring(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -436,9 +432,7 @@ end
|
|||||||
function filterElements(root, predicate)
|
function filterElements(root, predicate)
|
||||||
local results = {}
|
local results = {}
|
||||||
visitElements(root, function(element)
|
visitElements(root, function(element)
|
||||||
if predicate(element) then
|
if predicate(element) then table.insert(results, element) end
|
||||||
table.insert(results, element)
|
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
return results
|
return results
|
||||||
end
|
end
|
||||||
@@ -446,24 +440,18 @@ end
|
|||||||
--- Get text content of an element
|
--- Get text content of an element
|
||||||
--- @param element table XML element
|
--- @param element table XML element
|
||||||
--- @return string|nil The text content or nil
|
--- @return string|nil The text content or nil
|
||||||
function getText(element)
|
function getText(element) return element._text end
|
||||||
return element._text
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Set text content of an element
|
--- Set text content of an element
|
||||||
--- @param element table XML element
|
--- @param element table XML element
|
||||||
--- @param text string Text content to set
|
--- @param text string Text content to set
|
||||||
function setText(element, text)
|
function setText(element, text) element._text = text end
|
||||||
element._text = text
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Check if element has an attribute
|
--- Check if element has an attribute
|
||||||
--- @param element table XML element
|
--- @param element table XML element
|
||||||
--- @param attrName string Attribute name
|
--- @param attrName string Attribute name
|
||||||
--- @return boolean True if attribute exists
|
--- @return boolean True if attribute exists
|
||||||
function hasAttr(element, attrName)
|
function hasAttr(element, attrName) return element._attr and element._attr[attrName] ~= nil end
|
||||||
return element._attr and element._attr[attrName] ~= nil
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Get attribute value as string
|
--- Get attribute value as string
|
||||||
--- @param element table XML element
|
--- @param element table XML element
|
||||||
@@ -479,9 +467,7 @@ end
|
|||||||
--- @param attrName string Attribute name
|
--- @param attrName string Attribute name
|
||||||
--- @param value any Value to set (will be converted to string)
|
--- @param value any Value to set (will be converted to string)
|
||||||
function setAttr(element, attrName, value)
|
function setAttr(element, attrName, value)
|
||||||
if not element._attr then
|
if not element._attr then element._attr = {} end
|
||||||
element._attr = {}
|
|
||||||
end
|
|
||||||
element._attr[attrName] = tostring(value)
|
element._attr[attrName] = tostring(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -492,9 +478,7 @@ end
|
|||||||
function findFirstElement(parent, tagName)
|
function findFirstElement(parent, tagName)
|
||||||
if not parent._children then return nil end
|
if not parent._children then return nil end
|
||||||
for _, child in ipairs(parent._children) do
|
for _, child in ipairs(parent._children) do
|
||||||
if child._tag == tagName then
|
if child._tag == tagName then return child end
|
||||||
return child
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
@@ -503,9 +487,7 @@ end
|
|||||||
--- @param parent table The parent XML element
|
--- @param parent table The parent XML element
|
||||||
--- @param child table The child element to add
|
--- @param child table The child element to add
|
||||||
function addChild(parent, child)
|
function addChild(parent, child)
|
||||||
if not parent._children then
|
if not parent._children then parent._children = {} end
|
||||||
parent._children = {}
|
|
||||||
end
|
|
||||||
table.insert(parent._children, child)
|
table.insert(parent._children, child)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -536,9 +518,7 @@ function getChildren(parent, tagName)
|
|||||||
local results = {}
|
local results = {}
|
||||||
if not parent._children then return results end
|
if not parent._children then return results end
|
||||||
for _, child in ipairs(parent._children) do
|
for _, child in ipairs(parent._children) do
|
||||||
if child._tag == tagName then
|
if child._tag == tagName then table.insert(results, child) end
|
||||||
table.insert(results, child)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
return results
|
return results
|
||||||
end
|
end
|
||||||
@@ -551,9 +531,7 @@ function countChildren(parent, tagName)
|
|||||||
if not parent._children then return 0 end
|
if not parent._children then return 0 end
|
||||||
local count = 0
|
local count = 0
|
||||||
for _, child in ipairs(parent._children) do
|
for _, child in ipairs(parent._children) do
|
||||||
if child._tag == tagName then
|
if child._tag == tagName then count = count + 1 end
|
||||||
count = count + 1
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
return count
|
return count
|
||||||
end
|
end
|
||||||
@@ -584,9 +562,7 @@ end
|
|||||||
function findInJSON(data, predicate)
|
function findInJSON(data, predicate)
|
||||||
local results = {}
|
local results = {}
|
||||||
visitJSON(data, function(value, key, parent)
|
visitJSON(data, function(value, key, parent)
|
||||||
if predicate(value, key, parent) then
|
if predicate(value, key, parent) then table.insert(results, value) end
|
||||||
table.insert(results, value)
|
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
return results
|
return results
|
||||||
end
|
end
|
||||||
@@ -598,9 +574,7 @@ end
|
|||||||
function modifyJSONNumbers(data, predicate, modifier)
|
function modifyJSONNumbers(data, predicate, modifier)
|
||||||
visitJSON(data, function(value, key, parent)
|
visitJSON(data, function(value, key, parent)
|
||||||
if type(value) == "number" and predicate(value, key, parent) then
|
if type(value) == "number" and predicate(value, key, parent) then
|
||||||
if parent and key then
|
if parent and key then parent[key] = modifier(value) end
|
||||||
parent[key] = modifier(value)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user