package main import ( "regexp" "strings" "testing" ) // Helper function to normalize whitespace for comparison func normalizeWhitespace(s string) string { // Replace all whitespace with a single space re := regexp.MustCompile(`\s+`) return re.ReplaceAllString(strings.TrimSpace(s), " ") } func TestSimpleValueMultiplication(t *testing.T) { fileContents := ` 100 ` expected := ` 150 ` // Create a regex pattern with the (?s) flag for multiline matching regex := regexp.MustCompile(`(?s)(\d+)`) luaExpr := `*1.5` luaScript := buildLuaScript(luaExpr) modifiedContent, err := process(fileContents, regex, luaScript) if err != nil { t.Fatalf("Error processing file: %v", err) } // Compare normalized content normalizedModified := normalizeWhitespace(modifiedContent) normalizedExpected := normalizeWhitespace(expected) if normalizedModified != normalizedExpected { t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified) } } func TestShorthandNotation(t *testing.T) { fileContents := ` 100 ` expected := ` 150 ` regex := regexp.MustCompile(`(?s)(\d+)`) luaExpr := `v1 * 1.5` luaScript := buildLuaScript(luaExpr) 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) } } func TestArrayNotation(t *testing.T) { fileContents := ` 100 ` expected := ` 150 ` regex := regexp.MustCompile(`(?s)(\d+)`) luaExpr := `v[1] * 1.5` luaScript := buildLuaScript(luaExpr) 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) } } func TestMultipleMatches(t *testing.T) { fileContents := ` 100 200 300 ` expected := ` 150 300 450 ` regex := regexp.MustCompile(`(?s)(\d+)`) luaExpr := `*1.5` luaScript := buildLuaScript(luaExpr) 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) } } func TestMultipleCaptureGroups(t *testing.T) { fileContents := ` 10 5 ` expected := ` 50 5 ` // Use (?s) flag to match across multiple lines regex := regexp.MustCompile(`(?s)(\d+).*?(\d+)`) luaExpr := `v1 * v2` luaScript := buildLuaScript(luaExpr) // Verify the regex matches before processing matches := regex.FindStringSubmatch(fileContents) if len(matches) <= 1 { t.Fatalf("Regex didn't match any capture groups in test input: %v", fileContents) } t.Logf("Matches: %v", matches) 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) } } func TestModifyingMultipleValues(t *testing.T) { fileContents := ` 50 3 2 ` expected := ` 75 5 1 ` regex := regexp.MustCompile(`(?s)(\d+).*?(\d+).*?(\d+)`) luaExpr := `v[1] = v[1] * v[2] / v[3]; v[2] = min(v[2] * 2, 5); v[3] = max(1, v[3] / 2)` luaScript := buildLuaScript(luaExpr) // Verify the regex matches before processing matches := regex.FindStringSubmatch(fileContents) if len(matches) <= 1 { t.Fatalf("Regex didn't match any capture groups in test input: %v", fileContents) } t.Logf("Matches: %v", matches) 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) } } func TestDecimalValues(t *testing.T) { fileContents := ` 10.5 2.5 ` expected := ` 26.25 2.5 ` regex := regexp.MustCompile(`(?s)([0-9.]+).*?([0-9.]+)`) luaExpr := `v1 * v2` luaScript := buildLuaScript(luaExpr) 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) } } func TestLuaMathFunctions(t *testing.T) { fileContents := ` 16 ` expected := ` 4 ` regex := regexp.MustCompile(`(?s)(\d+)`) luaExpr := `math.sqrt(v1)` luaScript := buildLuaScript(luaExpr) 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) } }