From a4bbaf9f27c4833f7b63a9c4dbfaa141a8b3457c Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 19 Dec 2025 13:15:31 +0100 Subject: [PATCH] Fix up the lua tests To be less retarded... --- processor/luahelper-test-xml.lua | 275 +++++++++++++++++++++++-------- processor/luahelper.lua | 58 ++----- 2 files changed, 222 insertions(+), 111 deletions(-) diff --git a/processor/luahelper-test-xml.lua b/processor/luahelper-test-xml.lua index 63bb6f7..9a5fb1e 100644 --- a/processor/luahelper-test-xml.lua +++ b/processor/luahelper-test-xml.lua @@ -1,87 +1,224 @@ +-- Load the helper script 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")) +-- Test helper function +local function assert(condition, message) + if not condition then error("ASSERTION FAILED: " .. (message or "unknown error")) 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 -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("getNumAttr gets numeric attribute", function() + local elem = { _tag = "item", _attr = { damage = "10" } } + local damage = getNumAttr(elem, "damage") + assert(damage == 10, "Should get damage as number") +end) + +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 -print("\n3. Modify numeric attribute:") -modifyNumAttr(sword, "weight", function(val) return val * 1.5 end) -print(" Sword weight modified: " .. sword._attr.weight) +test("modifyNumAttr modifies numeric attribute", function() + local elem = { _tag = "item", _attr = { weight = "5.5" } } + 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 -print("\n4. Filter elements with 'healing' attribute:") -local healingItems = filterElements(testXML, function(elem) - return hasAttr(elem, "healing") +test("filterElements filters by predicate", function() + local testXML = { + _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) -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 +test("visitElements visits all elements", function() + local testXML = { + _tag = "root", + _children = { + { _tag = "item" }, + { _tag = "container", _children = { + { _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) -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("getText gets text content", function() + local elem = { _tag = "item", _text = "Iron Sword" } + local text = getText(elem) + 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 -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")) +test("hasAttr checks attribute existence", function() + local elem = { _tag = "item", _attr = { damage = "10" } } + assert(hasAttr(elem, "damage") == true, "Should have damage") + 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!") diff --git a/processor/luahelper.lua b/processor/luahelper.lua index db7b69c..3aadee1 100644 --- a/processor/luahelper.lua +++ b/processor/luahelper.lua @@ -361,18 +361,16 @@ modified = false --- @return table Array of matching elements function findElements(root, tagName) local results = {} - + local function search(element) - if element._tag == tagName then - table.insert(results, element) - end + if element._tag == tagName then table.insert(results, element) end if element._children then for _, child in ipairs(element._children) do search(child) end end end - + search(root) return results end @@ -409,9 +407,7 @@ end --- @param attrName string Attribute name --- @param value number Numeric value to set function setNumAttr(element, attrName, value) - if not element._attr then - element._attr = {} - end + if not element._attr then element._attr = {} end element._attr[attrName] = tostring(value) end @@ -436,9 +432,7 @@ end function filterElements(root, predicate) local results = {} visitElements(root, function(element) - if predicate(element) then - table.insert(results, element) - end + if predicate(element) then table.insert(results, element) end end) return results end @@ -446,24 +440,18 @@ end --- Get text content of an element --- @param element table XML element --- @return string|nil The text content or nil -function getText(element) - return element._text -end +function getText(element) return element._text end --- Set text content of an element --- @param element table XML element --- @param text string Text content to set -function setText(element, text) - element._text = text -end +function setText(element, text) element._text = text end --- Check if element has an attribute --- @param element table XML element --- @param attrName string Attribute name --- @return boolean True if attribute exists -function hasAttr(element, attrName) - return element._attr and element._attr[attrName] ~= nil -end +function hasAttr(element, attrName) return element._attr and element._attr[attrName] ~= nil end --- Get attribute value as string --- @param element table XML element @@ -479,9 +467,7 @@ end --- @param attrName string Attribute name --- @param value any Value to set (will be converted to string) function setAttr(element, attrName, value) - if not element._attr then - element._attr = {} - end + if not element._attr then element._attr = {} end element._attr[attrName] = tostring(value) end @@ -492,9 +478,7 @@ end function findFirstElement(parent, tagName) if not parent._children then return nil end for _, child in ipairs(parent._children) do - if child._tag == tagName then - return child - end + if child._tag == tagName then return child end end return nil end @@ -503,9 +487,7 @@ end --- @param parent table The parent XML element --- @param child table The child element to add function addChild(parent, child) - if not parent._children then - parent._children = {} - end + if not parent._children then parent._children = {} end table.insert(parent._children, child) end @@ -536,9 +518,7 @@ function getChildren(parent, tagName) local results = {} if not parent._children then return results end for _, child in ipairs(parent._children) do - if child._tag == tagName then - table.insert(results, child) - end + if child._tag == tagName then table.insert(results, child) end end return results end @@ -551,9 +531,7 @@ function countChildren(parent, tagName) if not parent._children then return 0 end local count = 0 for _, child in ipairs(parent._children) do - if child._tag == tagName then - count = count + 1 - end + if child._tag == tagName then count = count + 1 end end return count end @@ -584,9 +562,7 @@ end function findInJSON(data, predicate) local results = {} visitJSON(data, function(value, key, parent) - if predicate(value, key, parent) then - table.insert(results, value) - end + if predicate(value, key, parent) then table.insert(results, value) end end) return results end @@ -598,9 +574,7 @@ end function modifyJSONNumbers(data, predicate, modifier) visitJSON(data, function(value, key, parent) if type(value) == "number" and predicate(value, key, parent) then - if parent and key then - parent[key] = modifier(value) - end + if parent and key then parent[key] = modifier(value) end end end) -end \ No newline at end of file +end