712 lines
20 KiB
Go
712 lines
20 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"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 := `
|
|
<config>
|
|
<item>
|
|
<value>100</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>150</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
|
|
// Create a regex pattern with the (?s) flag for multiline matching
|
|
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
|
luaExpr := `*1.5`
|
|
luaScript := buildLuaScript(luaExpr)
|
|
|
|
modifiedContent, _, _, err := process(fileContents, regex, luaScript, "test.xml", luaExpr)
|
|
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 := `
|
|
<config>
|
|
<item>
|
|
<value>100</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>150</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
|
luaExpr := `v1 * 1.5` // Use direct assignment syntax
|
|
luaScript := buildLuaScript(luaExpr)
|
|
|
|
modifiedContent, _, _, err := process(fileContents, regex, luaScript, "test.xml", luaExpr )
|
|
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 := `
|
|
<config>
|
|
<item>
|
|
<value>100</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>150</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
|
luaExpr := `v1 = v1 * 1.5` // Use direct assignment syntax
|
|
luaScript := buildLuaScript(luaExpr)
|
|
|
|
modifiedContent, _, _, err := process(fileContents, regex, luaScript, "test.xml", luaExpr)
|
|
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 := `
|
|
<config>
|
|
<item>
|
|
<value>100</value>
|
|
</item>
|
|
<item>
|
|
<value>200</value>
|
|
</item>
|
|
<item> <value>300</value> </item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>150</value>
|
|
</item>
|
|
<item>
|
|
<value>300</value>
|
|
</item>
|
|
<item> <value>450</value> </item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
|
luaExpr := `*1.5`
|
|
luaScript := buildLuaScript(luaExpr)
|
|
|
|
modifiedContent, _, _, err := process(fileContents, regex, luaScript, "test.xml", luaExpr)
|
|
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 := `
|
|
<config>
|
|
<item>
|
|
<value>10</value>
|
|
<multiplier>5</multiplier>
|
|
</item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>50</value>
|
|
<multiplier>5</multiplier>
|
|
</item>
|
|
</config>
|
|
`
|
|
|
|
// Use (?s) flag to match across multiple lines
|
|
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>.*?<multiplier>(\d+)</multiplier>`)
|
|
luaExpr := `v1 = v1 * v2` // Use direct assignment syntax
|
|
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, "test.xml", luaExpr)
|
|
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 := `
|
|
<config>
|
|
<item>
|
|
<value>50</value>
|
|
<multiplier>3</multiplier>
|
|
<divider>2</divider>
|
|
</item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>75</value>
|
|
<multiplier>5</multiplier>
|
|
<divider>1</divider>
|
|
</item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>.*?<multiplier>(\d+)</multiplier>.*?<divider>(\d+)</divider>`)
|
|
luaExpr := `v1 = v1 * v2 / v3; v2 = min(v2 * 2, 5); v3 = max(1, v3 / 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, "test.xml", luaExpr)
|
|
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 := `
|
|
<config>
|
|
<item>
|
|
<value>10.5</value>
|
|
<multiplier>2.5</multiplier>
|
|
</item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>26.25</value>
|
|
<multiplier>2.5</multiplier>
|
|
</item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`(?s)<value>([0-9.]+)</value>.*?<multiplier>([0-9.]+)</multiplier>`)
|
|
luaExpr := `v1 = v1 * v2` // Use direct assignment syntax
|
|
luaScript := buildLuaScript(luaExpr)
|
|
|
|
modifiedContent, _, _, err := process(fileContents, regex, luaScript, "test.xml", luaExpr )
|
|
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 := `
|
|
<config>
|
|
<item>
|
|
<value>16</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>4</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
|
luaExpr := `v1 = math.sqrt(v1)` // Use direct assignment syntax
|
|
luaScript := buildLuaScript(luaExpr)
|
|
|
|
modifiedContent, _, _, err := process(fileContents, regex, luaScript, "test.xml", luaExpr)
|
|
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 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, "test.xml", luaExpr)
|
|
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
|
|
complexFile, err := os.ReadFile("test_complex.xml")
|
|
if err != nil {
|
|
t.Fatalf("Error reading test_complex.xml: %v", err)
|
|
}
|
|
|
|
testDataFile, err := os.ReadFile("test_data.xml")
|
|
if err != nil {
|
|
t.Fatalf("Error reading test_data.xml: %v", err)
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
fileContent string
|
|
regexPattern string
|
|
luaExpr string
|
|
expectedFunc func(string) string // Function to generate expected output
|
|
}{
|
|
{
|
|
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 = 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>`)
|
|
content = r.ReplaceAllString(content, "<item>\n <value>75</value>\n <multiplier>2</multiplier>\n <divider>4</divider>\n </item>")
|
|
|
|
r = regexp.MustCompile(`(?s)<item>\s*<value>300</value>\s*<multiplier>3</multiplier>\s*<divider>2</divider>\s*</item>`)
|
|
content = r.ReplaceAllString(content, "<item>\n <value>450</value>\n <multiplier>3</multiplier>\n <divider>2</divider>\n </item>")
|
|
|
|
return content
|
|
},
|
|
},
|
|
{
|
|
name: "Test data - simple multiplication",
|
|
fileContent: string(testDataFile),
|
|
regexPattern: `(?s)<test id="simple">.*?<value>(\d+)</value>.*?</test>`,
|
|
luaExpr: `v1 = v1 * 1.5`,
|
|
expectedFunc: func(content string) string {
|
|
r := regexp.MustCompile(`<value>100</value>`)
|
|
return r.ReplaceAllString(content, "<value>150</value>")
|
|
},
|
|
},
|
|
{
|
|
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 = v1 * v2 / v3`,
|
|
expectedFunc: func(content string) string {
|
|
r := regexp.MustCompile(`<value>50</value>`)
|
|
return r.ReplaceAllString(content, "<value>75</value>")
|
|
},
|
|
},
|
|
{
|
|
name: "Test data - decimal values",
|
|
fileContent: string(testDataFile),
|
|
regexPattern: `(?s)<test id="decimal">.*?<value>([0-9.]+)</value>.*?<multiplier>([0-9.]+)</multiplier>.*?</test>`,
|
|
luaExpr: `v1 = v1 * v2`,
|
|
expectedFunc: func(content string) string {
|
|
r := regexp.MustCompile(`<value>10.5</value>`)
|
|
return r.ReplaceAllString(content, "<value>26.25</value>")
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
regex := regexp.MustCompile(tc.regexPattern)
|
|
luaScript := buildLuaScript(tc.luaExpr)
|
|
|
|
// Debug information
|
|
t.Logf("Regex pattern: %s", tc.regexPattern)
|
|
t.Logf("Lua expression: %s", tc.luaExpr)
|
|
|
|
// Process the content
|
|
modifiedContent, _, _, err := process(tc.fileContent, regex, luaScript, "test.xml", tc.luaExpr)
|
|
if err != nil {
|
|
t.Fatalf("Error processing file: %v", err)
|
|
}
|
|
t.Logf("Modified content has length: %d", len(modifiedContent))
|
|
|
|
// Generate expected content
|
|
expectedContent := tc.expectedFunc(tc.fileContent)
|
|
t.Logf("Expected content has length: %d", len(expectedContent))
|
|
|
|
// Compare normalized content
|
|
normalizedModified := normalizeWhitespace(modifiedContent)
|
|
normalizedExpected := normalizeWhitespace(expectedContent)
|
|
|
|
// Check if the specific section was modified as expected
|
|
if !strings.Contains(normalizedModified, normalizeWhitespace(expectedContent)) {
|
|
t.Logf("Modified content: %s", normalizedModified)
|
|
t.Logf("Expected content: %s", normalizedExpected)
|
|
t.Fatalf("Expected modification not found in result")
|
|
} else {
|
|
t.Logf("Test passed - expected modification found in result")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFileOperations(t *testing.T) {
|
|
// Test file operations with sample XML files
|
|
|
|
// Test 1: Complex file with multiple items
|
|
t.Run("Complex file operations", func(t *testing.T) {
|
|
// Read test file
|
|
complexFile, err := os.ReadFile("test_complex.xml")
|
|
if err != nil {
|
|
t.Fatalf("Error reading test_complex.xml: %v", err)
|
|
}
|
|
fileContent := string(complexFile)
|
|
|
|
// Configure test
|
|
regexPattern := `(?s)<value>(\d+)</value>.*?<multiplier>(\d+)</multiplier>.*?<divider>(\d+)</divider>`
|
|
luaExpr := `v1 = v1 * v2 / v3` // Use direct assignment
|
|
|
|
// Execute test
|
|
regex := regexp.MustCompile(regexPattern)
|
|
luaScript := buildLuaScript(luaExpr)
|
|
|
|
t.Logf("Regex pattern: %s", regexPattern)
|
|
t.Logf("Lua expression: %s", luaExpr)
|
|
|
|
// Process the content
|
|
modifiedContent, _, _, err := process(fileContent, regex, luaScript, "test.xml", luaExpr )
|
|
if err != nil {
|
|
t.Fatalf("Error processing file: %v", err)
|
|
}
|
|
|
|
// Verify results - should have 75 and 450 as values
|
|
t.Logf("Modified content: %s", modifiedContent)
|
|
if !strings.Contains(modifiedContent, "<value>75</value>") {
|
|
t.Errorf("First value not modified correctly, expected <value>75</value>")
|
|
}
|
|
if !strings.Contains(modifiedContent, "<value>450</value>") {
|
|
t.Errorf("Second value not modified correctly, expected <value>450</value>")
|
|
}
|
|
t.Logf("Complex file test completed successfully")
|
|
})
|
|
|
|
// Test 2: Test data file with simple multiplication
|
|
t.Run("Simple multiplication in test data", func(t *testing.T) {
|
|
// Read test file
|
|
testDataFile, err := os.ReadFile("test_data.xml")
|
|
if err != nil {
|
|
t.Fatalf("Error reading test_data.xml: %v", err)
|
|
}
|
|
fileContent := string(testDataFile)
|
|
|
|
// Configure test for simple value
|
|
regexPattern := `(?s)<test id="simple">.*?<value>(\d+)</value>.*?</test>`
|
|
luaExpr := `v1 = v1 * 1.5` // Use direct assignment
|
|
|
|
// Execute test
|
|
regex := regexp.MustCompile(regexPattern)
|
|
luaScript := buildLuaScript(luaExpr)
|
|
|
|
t.Logf("Regex pattern: %s", regexPattern)
|
|
t.Logf("Lua expression: %s", luaExpr)
|
|
|
|
// Process the content
|
|
modifiedContent, _, _, err := process(fileContent, regex, luaScript, "test.xml", luaExpr)
|
|
if err != nil {
|
|
t.Fatalf("Error processing file: %v", err)
|
|
}
|
|
|
|
// Check for expected value (100 * 1.5 = 150)
|
|
if !strings.Contains(modifiedContent, "<value>150</value>") {
|
|
t.Errorf("Value not modified correctly, expected <value>150</value> in the simple test")
|
|
t.Logf("Modified content: %s", modifiedContent)
|
|
} else {
|
|
t.Logf("Simple test passed - found <value>150</value>")
|
|
}
|
|
})
|
|
|
|
// Test 3: Decimal values
|
|
t.Run("Decimal values in test data", func(t *testing.T) {
|
|
// Read test file
|
|
testDataFile, err := os.ReadFile("test_data.xml")
|
|
if err != nil {
|
|
t.Fatalf("Error reading test_data.xml: %v", err)
|
|
}
|
|
fileContent := string(testDataFile)
|
|
|
|
// Configure test for decimal values
|
|
regexPattern := `(?s)<test id="decimal">.*?<value>([0-9.]+)</value>.*?<multiplier>([0-9.]+)</multiplier>.*?</test>`
|
|
luaExpr := `v1 = v1 * v2` // Use direct assignment
|
|
|
|
// Execute test
|
|
regex := regexp.MustCompile(regexPattern)
|
|
luaScript := buildLuaScript(luaExpr)
|
|
|
|
t.Logf("Regex pattern: %s", regexPattern)
|
|
t.Logf("Lua expression: %s", luaExpr)
|
|
|
|
// Process the content
|
|
modifiedContent, _, _, err := process(fileContent, regex, luaScript, "test.xml", luaExpr)
|
|
if err != nil {
|
|
t.Fatalf("Error processing file: %v", err)
|
|
}
|
|
|
|
// Check for expected value (10.5 * 2.5 = 26.25)
|
|
if !strings.Contains(modifiedContent, "<value>26.25</value>") {
|
|
t.Errorf("Decimal value not modified correctly, expected <value>26.25</value>")
|
|
t.Logf("Modified content: %s", modifiedContent)
|
|
} else {
|
|
t.Logf("Decimal test passed - found <value>26.25</value>")
|
|
}
|
|
})
|
|
}
|
|
|
|
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 = v1 + v2 * v3 / v4 - v5`
|
|
luaScript := buildLuaScript(luaExpr)
|
|
|
|
// Expected: 10 + 20 * 30 / 40 - 50 = 10 + 15 - 50 = -25
|
|
|
|
modifiedContent, _, _, err := process(fileContents, regex, luaScript, "test.xml", luaExpr)
|
|
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 = v1 * v2`
|
|
luaScript := buildLuaScript(luaExpr)
|
|
|
|
// Expected: 10 * 110 = 1100
|
|
|
|
modifiedContent, _, _, err := process(fileContents, regex, luaScript, "test.xml", luaExpr)
|
|
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 := `v1 = tonumber(v1) * 2`
|
|
luaScript := buildLuaScript(luaExpr)
|
|
|
|
// This should double the value
|
|
modifiedContent, _, _, err := process(fileContents, regex, luaScript, "test.xml", luaExpr)
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
|
|
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, "test.xml", luaExpr)
|
|
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, "test.xml", luaExpr)
|
|
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)
|
|
}
|
|
}
|