Remove some unused shit and write tests for coverage

This commit is contained in:
2025-12-19 12:12:42 +01:00
parent 1df0263a42
commit da5b621cb6
19 changed files with 1892 additions and 390 deletions

View File

@@ -29,7 +29,7 @@ type XMLElement struct {
// XMLAttribute represents an attribute with its position in the source
type XMLAttribute struct {
Value string
Value string
ValueStart int64
ValueEnd int64
}
@@ -57,7 +57,7 @@ func parseXMLWithPositions(content string) (*XMLElement, error) {
// Find the actual start position of this element by searching for "<tagname"
tagSearchPattern := "<" + t.Name.Local
startPos := int64(strings.LastIndex(content[:offset], tagSearchPattern))
element := &XMLElement{
Tag: t.Name.Local,
Attributes: make(map[string]XMLAttribute),
@@ -69,7 +69,7 @@ func parseXMLWithPositions(content string) (*XMLElement, error) {
if len(t.Attr) > 0 {
tagEnd := offset
tagSection := content[startPos:tagEnd]
for _, attr := range t.Attr {
// Find attribute in the tag section: attrname="value"
attrPattern := attr.Name.Local + `="`
@@ -102,7 +102,7 @@ func parseXMLWithPositions(content string) (*XMLElement, error) {
if len(stack) > 0 && text != "" {
current := stack[len(stack)-1]
current.Text = text
// The text content is between lastPos (after >) and offset (before </)
// Search for the trimmed text within the raw content
textInContent := content[lastPos:offset]
@@ -127,34 +127,6 @@ func parseXMLWithPositions(content string) (*XMLElement, error) {
return root, nil
}
// xmlElementToMap converts XMLElement to a map for comparison
func xmlElementToMap(elem *XMLElement) map[string]interface{} {
result := make(map[string]interface{})
result["_tag"] = elem.Tag
if len(elem.Attributes) > 0 {
attrs := make(map[string]interface{})
for k, v := range elem.Attributes {
attrs[k] = v.Value
}
result["_attr"] = attrs
}
if elem.Text != "" {
result["_text"] = elem.Text
}
if len(elem.Children) > 0 {
children := make([]interface{}, len(elem.Children))
for i, child := range elem.Children {
children[i] = xmlElementToMap(child)
}
result["_children"] = children
}
return result
}
// XMLChange represents a detected difference between original and modified XML structures
type XMLChange struct {
Type string // "text", "attribute", "add_element", "remove_element"
@@ -276,22 +248,6 @@ func findXMLChanges(original, modified *XMLElement, path string) []XMLChange {
}
}
// Handle completely new tag types
for tag, modChildren := range modChildMap {
if !processedTags[tag] {
for i, child := range modChildren {
childPath := fmt.Sprintf("%s/%s[%d]", path, tag, i)
xmlText := serializeXMLElement(child, " ")
changes = append(changes, XMLChange{
Type: "add_element",
Path: childPath,
InsertText: xmlText,
StartPos: original.EndPos - int64(len(original.Tag)+3),
})
}
}
}
return changes
}
@@ -395,14 +351,6 @@ func applyXMLChanges(changes []XMLChange) []utils.ReplaceCommand {
return commands
}
// modifyXMLElement applies modifications to an XMLElement based on a modification function
func modifyXMLElement(elem *XMLElement, modifyFunc func(*XMLElement)) *XMLElement {
// Deep copy the element
copied := deepCopyXMLElement(elem)
modifyFunc(copied)
return copied
}
// deepCopyXMLElement creates a deep copy of an XMLElement
func deepCopyXMLElement(elem *XMLElement) *XMLElement {
if elem == nil {
@@ -410,12 +358,12 @@ func deepCopyXMLElement(elem *XMLElement) *XMLElement {
}
copied := &XMLElement{
Tag: elem.Tag,
Text: elem.Text,
StartPos: elem.StartPos,
EndPos: elem.EndPos,
TextStart: elem.TextStart,
TextEnd: elem.TextEnd,
Tag: elem.Tag,
Text: elem.Text,
StartPos: elem.StartPos,
EndPos: elem.EndPos,
TextStart: elem.TextStart,
TextEnd: elem.TextEnd,
Attributes: make(map[string]XMLAttribute),
Children: make([]*XMLElement, len(elem.Children)),
}
@@ -534,10 +482,6 @@ func xmlElementToLuaTable(L *lua.LState, elem *XMLElement) *lua.LTable {
table.RawSetString("_attr", attrs)
}
if elem.Text != "" {
table.RawSetString("_text", lua.LString(elem.Text))
}
if len(elem.Children) > 0 {
children := L.CreateTable(len(elem.Children), 0)
for i, child := range elem.Children {
@@ -551,11 +495,6 @@ func xmlElementToLuaTable(L *lua.LState, elem *XMLElement) *lua.LTable {
// luaTableToXMLElement applies Lua table modifications back to XMLElement
func luaTableToXMLElement(L *lua.LState, table *lua.LTable, elem *XMLElement) {
// Update text
if textVal := table.RawGetString("_text"); textVal.Type() == lua.LTString {
elem.Text = string(textVal.(lua.LString))
}
// Update attributes
if attrVal := table.RawGetString("_attr"); attrVal.Type() == lua.LTTable {
attrTable := attrVal.(*lua.LTable)
@@ -574,7 +513,7 @@ func luaTableToXMLElement(L *lua.LState, table *lua.LTable, elem *XMLElement) {
if childrenVal := table.RawGetString("_children"); childrenVal.Type() == lua.LTTable {
childrenTable := childrenVal.(*lua.LTable)
newChildren := []*XMLElement{}
// Iterate over array indices
for i := 1; ; i++ {
childVal := childrenTable.RawGetInt(i)