520 lines
15 KiB
Go
520 lines
15 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)
|
|
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`
|
|
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 := `
|
|
<config>
|
|
<item>
|
|
<value>100</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>150</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
|
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 := `
|
|
<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)
|
|
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 * 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 := `
|
|
<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 := `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 := `
|
|
<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 * 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 := `
|
|
<config>
|
|
<item>
|
|
<value>16</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>4</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
|
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)
|
|
}
|
|
}
|
|
|
|
// 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: `v[1] * v[2] / v[3]`,
|
|
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: `*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 * 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 * 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)
|
|
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 := `v[1] * v[2] / v[3]`
|
|
|
|
// 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)
|
|
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 := `*1.5`
|
|
|
|
// 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)
|
|
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 * v2`
|
|
|
|
// 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)
|
|
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>")
|
|
}
|
|
})
|
|
}
|