Rework how we handle state and calling and returning

This commit is contained in:
2025-03-22 03:20:32 +01:00
parent d5965d3b2d
commit 16b6156cd3
2 changed files with 140 additions and 97 deletions

View File

@@ -65,7 +65,7 @@ func TestShorthandNotation(t *testing.T) {
`
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
luaExpr := `v1 * 1.5`
luaExpr := `v1 = v1 * 1.5` // Use direct assignment syntax
luaScript := buildLuaScript(luaExpr)
modifiedContent, err := process(fileContents, regex, luaScript)
@@ -97,7 +97,7 @@ func TestArrayNotation(t *testing.T) {
`
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
luaExpr := `v1 * 1.5`
luaExpr := `v1 = v1 * 1.5` // Use direct assignment syntax
luaScript := buildLuaScript(luaExpr)
modifiedContent, err := process(fileContents, regex, luaScript)
@@ -172,7 +172,7 @@ func TestMultipleCaptureGroups(t *testing.T) {
// Use (?s) flag to match across multiple lines
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>.*?<multiplier>(\d+)</multiplier>`)
luaExpr := `v1 * v2`
luaExpr := `v1 = v1 * v2` // Use direct assignment syntax
luaScript := buildLuaScript(luaExpr)
// Verify the regex matches before processing
@@ -256,7 +256,7 @@ func TestDecimalValues(t *testing.T) {
`
regex := regexp.MustCompile(`(?s)<value>([0-9.]+)</value>.*?<multiplier>([0-9.]+)</multiplier>`)
luaExpr := `v1 * v2`
luaExpr := `v1 = v1 * v2` // Use direct assignment syntax
luaScript := buildLuaScript(luaExpr)
modifiedContent, err := process(fileContents, regex, luaScript)
@@ -288,7 +288,7 @@ func TestLuaMathFunctions(t *testing.T) {
`
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
luaExpr := `math.sqrt(v1)`
luaExpr := `v1 = math.sqrt(v1)` // Use direct assignment syntax
luaScript := buildLuaScript(luaExpr)
modifiedContent, err := process(fileContents, regex, luaScript)
@@ -361,7 +361,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: `v1 * v2 / v3`,
luaExpr: `v1 = 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>`)
@@ -377,7 +377,7 @@ func TestProcessingSampleFiles(t *testing.T) {
name: "Test data - simple multiplication",
fileContent: string(testDataFile),
regexPattern: `(?s)<test id="simple">.*?<value>(\d+)</value>.*?</test>`,
luaExpr: `*1.5`,
luaExpr: `v1 = v1 * 1.5`,
expectedFunc: func(content string) string {
r := regexp.MustCompile(`<value>100</value>`)
return r.ReplaceAllString(content, "<value>150</value>")
@@ -387,7 +387,7 @@ func TestProcessingSampleFiles(t *testing.T) {
name: "Test data - multiple capture groups",
fileContent: string(testDataFile),
regexPattern: `(?s)<test id="multi">.*?<value>(\d+)</value>.*?<multiplier>(\d+)</multiplier>.*?<divider>(\d+)</divider>.*?</test>`,
luaExpr: `v1 * v2 / v3`,
luaExpr: `v1 = v1 * v2 / v3`,
expectedFunc: func(content string) string {
r := regexp.MustCompile(`<value>50</value>`)
return r.ReplaceAllString(content, "<value>75</value>")
@@ -397,7 +397,7 @@ func TestProcessingSampleFiles(t *testing.T) {
name: "Test data - decimal values",
fileContent: string(testDataFile),
regexPattern: `(?s)<test id="decimal">.*?<value>([0-9.]+)</value>.*?<multiplier>([0-9.]+)</multiplier>.*?</test>`,
luaExpr: `v1 * v2`,
luaExpr: `v1 = v1 * v2`,
expectedFunc: func(content string) string {
r := regexp.MustCompile(`<value>10.5</value>`)
return r.ReplaceAllString(content, "<value>26.25</value>")
@@ -455,7 +455,7 @@ func TestFileOperations(t *testing.T) {
// Configure test
regexPattern := `(?s)<value>(\d+)</value>.*?<multiplier>(\d+)</multiplier>.*?<divider>(\d+)</divider>`
luaExpr := `v1 * v2 / v3`
luaExpr := `v1 = v1 * v2 / v3` // Use direct assignment
// Execute test
regex := regexp.MustCompile(regexPattern)
@@ -492,7 +492,7 @@ func TestFileOperations(t *testing.T) {
// Configure test for simple value
regexPattern := `(?s)<test id="simple">.*?<value>(\d+)</value>.*?</test>`
luaExpr := `*1.5`
luaExpr := `v1 = v1 * 1.5` // Use direct assignment
// Execute test
regex := regexp.MustCompile(regexPattern)
@@ -527,7 +527,7 @@ func TestFileOperations(t *testing.T) {
// Configure test for decimal values
regexPattern := `(?s)<test id="decimal">.*?<value>([0-9.]+)</value>.*?<multiplier>([0-9.]+)</multiplier>.*?</test>`
luaExpr := `v1 * v2`
luaExpr := `v1 = v1 * v2` // Use direct assignment
// Execute test
regex := regexp.MustCompile(regexPattern)
@@ -569,7 +569,7 @@ func TestHigherVariableIndices(t *testing.T) {
// 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`
luaExpr := `v1 = v1 + v2 * v3 / v4 - v5`
luaScript := buildLuaScript(luaExpr)
// Expected: 10 + 20 * 30 / 40 - 50 = 10 + 15 - 50 = -25
@@ -589,7 +589,7 @@ func TestHigherVariableIndices(t *testing.T) {
// 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`
luaExpr := `v1 = v1 * v2`
luaScript := buildLuaScript(luaExpr)
// Expected: 10 * 110 = 1100
@@ -609,7 +609,7 @@ func TestHigherVariableIndices(t *testing.T) {
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`
luaExpr := `v1 = tonumber(v1) * 2`
luaScript := buildLuaScript(luaExpr)
// This should double the value
@@ -624,3 +624,88 @@ func TestHigherVariableIndices(t *testing.T) {
}
})
}
func TestMultiStatementExpression(t *testing.T) {
fileContents := `
<config>
<item>
<value1>100</value1>
<value2>200</value2>
</item>
</config>
`
expected := `
<config>
<item>
<value1>0</value1>
<value2>0</value2>
</item>
</config>
`
regex := regexp.MustCompile(`(?s)<value1>(\d+)</value1>.*?<value2>(\d+)</value2>`)
luaExpr := `v1=0 v2=0` // Multiple statements without semicolons
luaScript := buildLuaScript(luaExpr)
t.Logf("Generated Lua script: %s", luaScript)
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 TestComplexLuaScripts(t *testing.T) {
fileContents := `
<config>
<item>
<value1>100</value1>
<value2>200</value2>
<value3>50</value3>
</item>
</config>
`
expected := `
<config>
<item>
<value1>300</value1>
<value2>0</value2>
<value3>150</value3>
</item>
</config>
`
regex := regexp.MustCompile(`(?s)<value1>(\d+)</value1>.*?<value2>(\d+)</value2>.*?<value3>(\d+)</value3>`)
luaExpr := `
local sum = v1 + v2
if sum > 250 then
v1 = sum
v2 = 0
v3 = v3 * 3
else
v1 = 0
v2 = sum
v3 = v3 * 2
end
`
luaScript := buildLuaScript(luaExpr)
t.Logf("Generated Lua script: %s", luaScript)
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)
}
}