Implement = operator

This commit is contained in:
2025-03-22 02:36:54 +01:00
parent 4694a5b9aa
commit d7f88a2f21
2 changed files with 40 additions and 1 deletions

View File

@@ -37,8 +37,9 @@ func main() {
fmt.Fprintf(os.Stderr, "Example: %s \"<value>(\\d+)</value>,(\\d+)\" \"v1 * 1.5 * v2\" data.xml\n", os.Args[0])
fmt.Fprintf(os.Stderr, " or simplified: %s \"<value>(\\d+)</value>,(\\d+)\" \"v1 * 1.5 * v2\" data.xml\n", os.Args[0])
fmt.Fprintf(os.Stderr, " or even simpler: %s \"<value>(\\d+)</value>\" \"*1.5\" data.xml\n", os.Args[0])
fmt.Fprintf(os.Stderr, " or direct assignment: %s \"<value>(\\d+)</value>\" \"=0\" data.xml\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nNote: v1, v2, etc. are used to refer to capture groups.\n")
fmt.Fprintf(os.Stderr, " If expression starts with an operator like *, /, +, -, etc., v1 is automatically prepended\n")
fmt.Fprintf(os.Stderr, " If expression starts with an operator like *, /, +, -, =, etc., v1 is automatically prepended\n")
}
flag.Parse()
@@ -89,6 +90,10 @@ func buildLuaScript(luaExpr string) string {
strings.HasPrefix(luaExpr, "^") || strings.HasPrefix(luaExpr, "%") {
luaExpr = "v1" + luaExpr
log.Printf("Expression modified to: %s", luaExpr)
} else if strings.HasPrefix(luaExpr, "=") {
// Handle direct assignment with = operator
luaExpr = "v1 " + luaExpr
log.Printf("Expression modified to: %s", luaExpr)
}
// Replace shorthand v1, v2, etc. with their direct variable names (no need for array notation)

View File

@@ -303,6 +303,40 @@ func TestLuaMathFunctions(t *testing.T) {
}
}
func TestDirectAssignment(t *testing.T) {
fileContents := `
<config>
<item>
<value>100</value>
</item>
</config>
`
expected := `
<config>
<item>
<value>0</value>
</item>
</config>
`
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
luaExpr := `=0`
luaScript := buildLuaScript(luaExpr)
t.Logf("Lua script: %s", luaScript) // Log the generated script for debugging
modifiedContent, err := process(fileContents, regex, luaScript)
if err != nil {
t.Fatalf("Error processing file: %v", err)
}
normalizedModified := normalizeWhitespace(modifiedContent)
normalizedExpected := normalizeWhitespace(expected)
if normalizedModified != normalizedExpected {
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
}
}
// Test with actual files
func TestProcessingSampleFiles(t *testing.T) {
// Read test files