Implement = operator
This commit is contained in:
7
main.go
7
main.go
@@ -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, "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 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 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, "\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()
|
flag.Parse()
|
||||||
@@ -89,6 +90,10 @@ func buildLuaScript(luaExpr string) string {
|
|||||||
strings.HasPrefix(luaExpr, "^") || strings.HasPrefix(luaExpr, "%") {
|
strings.HasPrefix(luaExpr, "^") || strings.HasPrefix(luaExpr, "%") {
|
||||||
luaExpr = "v1" + luaExpr
|
luaExpr = "v1" + luaExpr
|
||||||
log.Printf("Expression modified to: %s", 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)
|
// Replace shorthand v1, v2, etc. with their direct variable names (no need for array notation)
|
||||||
|
34
main_test.go
34
main_test.go
@@ -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
|
// Test with actual files
|
||||||
func TestProcessingSampleFiles(t *testing.T) {
|
func TestProcessingSampleFiles(t *testing.T) {
|
||||||
// Read test files
|
// Read test files
|
||||||
|
Reference in New Issue
Block a user