Add more test cases

This commit is contained in:
2025-03-22 02:22:00 +01:00
parent e799150c3d
commit 6b5bfa700a
4 changed files with 261 additions and 1 deletions

1
.gitignore vendored
View File

@@ -1,2 +1 @@
*.exe *.exe
*.xml

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"os"
"regexp" "regexp"
"strings" "strings"
"testing" "testing"
@@ -301,3 +302,218 @@ func TestLuaMathFunctions(t *testing.T) {
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified) 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>")
}
})
}

12
test_complex.xml Normal file
View File

@@ -0,0 +1,12 @@
<config>
<item>
<value>150</value>
<multiplier>2</multiplier>
<divider>4</divider>
</item>
<item>
<value>300</value>
<multiplier>3</multiplier>
<divider>2</divider>
</item>
</config>

33
test_data.xml Normal file
View File

@@ -0,0 +1,33 @@
<tests>
<!-- Test simple value replacement -->
<test id="simple">
<value>100</value>
</test>
<!-- Test with multiple capture groups -->
<test id="multi">
<value>50</value>
<multiplier>3</multiplier>
<divider>2</divider>
</test>
<!-- Test with inline format -->
<test id="inline"><value>200</value>,<multiplier>2</multiplier>,<divider>4</divider></test>
<!-- Test with nested elements -->
<test id="nested">
<data>
<value>75</value>
<settings>
<multiplier>4</multiplier>
<divider>3</divider>
</settings>
</data>
</test>
<!-- Test with decimal values -->
<test id="decimal">
<value>10.5</value>
<multiplier>2.5</multiplier>
</test>
</tests>