From d7f88a2f21f72b80c46ccdb6aec37414c36a180d Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 22 Mar 2025 02:36:54 +0100 Subject: [PATCH] Implement = operator --- main.go | 7 ++++++- main_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index d40b069..a4cb1f8 100644 --- a/main.go +++ b/main.go @@ -37,8 +37,9 @@ func main() { fmt.Fprintf(os.Stderr, "Example: %s \"(\\d+),(\\d+)\" \"v1 * 1.5 * v2\" data.xml\n", os.Args[0]) fmt.Fprintf(os.Stderr, " or simplified: %s \"(\\d+),(\\d+)\" \"v1 * 1.5 * v2\" data.xml\n", os.Args[0]) fmt.Fprintf(os.Stderr, " or even simpler: %s \"(\\d+)\" \"*1.5\" data.xml\n", os.Args[0]) + fmt.Fprintf(os.Stderr, " or direct assignment: %s \"(\\d+)\" \"=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) diff --git a/main_test.go b/main_test.go index 19e3ccb..d3bced2 100644 --- a/main_test.go +++ b/main_test.go @@ -303,6 +303,40 @@ func TestLuaMathFunctions(t *testing.T) { } } +func TestDirectAssignment(t *testing.T) { + fileContents := ` + + + 100 + + + ` + expected := ` + + + 0 + + + ` + + regex := regexp.MustCompile(`(?s)(\d+)`) + 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