Fix up the lua tests

To be less retarded...
This commit is contained in:
2025-12-19 13:15:31 +01:00
parent 419a8118fc
commit a4bbaf9f27
2 changed files with 222 additions and 111 deletions

View File

@@ -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
end