Rework shit to use v12345 instead of v[1]

Why did I even do that...
This commit is contained in:
2025-03-22 02:30:30 +01:00
parent 6b5bfa700a
commit 4694a5b9aa
3 changed files with 137 additions and 55 deletions

View File

@@ -97,7 +97,7 @@ func TestArrayNotation(t *testing.T) {
`
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
luaExpr := `v[1] * 1.5`
luaExpr := `v1 * 1.5`
luaScript := buildLuaScript(luaExpr)
modifiedContent, err := process(fileContents, regex, luaScript)
@@ -215,7 +215,7 @@ func TestModifyingMultipleValues(t *testing.T) {
`
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>.*?<multiplier>(\d+)</multiplier>.*?<divider>(\d+)</divider>`)
luaExpr := `v[1] = v[1] * v[2] / v[3]; v[2] = min(v[2] * 2, 5); v[3] = max(1, v[3] / 2)`
luaExpr := `v1 = v1 * v2 / v3; v2 = min(v2 * 2, 5); v3 = max(1, v3 / 2)`
luaScript := buildLuaScript(luaExpr)
// Verify the regex matches before processing
@@ -327,7 +327,7 @@ func TestProcessingSampleFiles(t *testing.T) {
name: "Complex file - multiply values by multiplier and divide by divider",
fileContent: string(complexFile),
regexPattern: `(?s)<value>(\d+)</value>.*?<multiplier>(\d+)</multiplier>.*?<divider>(\d+)</divider>`,
luaExpr: `v[1] * v[2] / v[3]`,
luaExpr: `v1 * v2 / v3`,
expectedFunc: func(content string) string {
// Replace values manually for verification
r := regexp.MustCompile(`(?s)<item>\s*<value>150</value>\s*<multiplier>2</multiplier>\s*<divider>4</divider>\s*</item>`)
@@ -421,7 +421,7 @@ func TestFileOperations(t *testing.T) {
// Configure test
regexPattern := `(?s)<value>(\d+)</value>.*?<multiplier>(\d+)</multiplier>.*?<divider>(\d+)</divider>`
luaExpr := `v[1] * v[2] / v[3]`
luaExpr := `v1 * v2 / v3`
// Execute test
regex := regexp.MustCompile(regexPattern)
@@ -517,3 +517,76 @@ func TestFileOperations(t *testing.T) {
}
})
}
func TestHigherVariableIndices(t *testing.T) {
fileContents := `
<config>
<item>
<value1>10</value1>
<value2>20</value2>
<value3>30</value3>
<value4>40</value4>
<value5>50</value5>
<value11>110</value11>
</item>
</config>
`
// Test using v3, v4, v5 in the expression
t.Run("Using v3-v5 variables", func(t *testing.T) {
regex := regexp.MustCompile(`(?s)<value1>(\d+)</value1>.*?<value2>(\d+)</value2>.*?<value3>(\d+)</value3>.*?<value4>(\d+)</value4>.*?<value5>(\d+)</value5>`)
luaExpr := `v1 + v2 * v3 / v4 - v5`
luaScript := buildLuaScript(luaExpr)
// Expected: 10 + 20 * 30 / 40 - 50 = 10 + 15 - 50 = -25
modifiedContent, err := process(fileContents, regex, luaScript)
if err != nil {
t.Fatalf("Error processing with v3-v5: %v", err)
}
// The result should replace the first value
if !strings.Contains(modifiedContent, "<value1>-25</value1>") {
t.Fatalf("Failed to process v3-v5 correctly. Expected <value1>-25</value1>, got: %s", modifiedContent)
}
})
// Test using v11 (double digit index)
// For double digit indexes, we need to capture it as the second variable (v2)
t.Run("Using v11 variable", func(t *testing.T) {
regex := regexp.MustCompile(`(?s)<value1>(\d+)</value1>.*?<value11>(\d+)</value11>`)
luaExpr := `v1 * v2`
luaScript := buildLuaScript(luaExpr)
// Expected: 10 * 110 = 1100
modifiedContent, err := process(fileContents, regex, luaScript)
if err != nil {
t.Fatalf("Error processing with v11: %v", err)
}
// The result should replace the first value
if !strings.Contains(modifiedContent, "<value1>1100</value1>") {
t.Fatalf("Failed to process v11 correctly. Expected <value1>1100</value1>, got: %s", modifiedContent)
}
})
// Test using v0 (zero index)
t.Run("Using v0 variable", func(t *testing.T) {
// For this test, we'll capture the tag content and manipulate it
regex := regexp.MustCompile(`(?s)<value1>(\d+)</value1>`)
luaExpr := `tonumber(v1) * 2`
luaScript := buildLuaScript(luaExpr)
// This should double the value
modifiedContent, err := process(fileContents, regex, luaScript)
if err != nil {
t.Fatalf("Error processing with v0: %v", err)
}
// Should replace <value1>10</value1> with <value1>20</value1>
if !strings.Contains(modifiedContent, "<value1>20</value1>") {
t.Fatalf("Failed to process test correctly. Expected <value1>20</value1>, got: %s", modifiedContent)
}
})
}