From 6b5bfa700aa5747b81eb23ab99695ee286020188 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 22 Mar 2025 02:22:00 +0100 Subject: [PATCH] Add more test cases --- .gitignore | 1 - main_test.go | 216 +++++++++++++++++++++++++++++++++++++++++++++++ test_complex.xml | 12 +++ test_data.xml | 33 ++++++++ 4 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 test_complex.xml create mode 100644 test_data.xml diff --git a/.gitignore b/.gitignore index 13fc1fc..b883f1f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ *.exe -*.xml diff --git a/main_test.go b/main_test.go index 4117b46..d334b9e 100644 --- a/main_test.go +++ b/main_test.go @@ -1,6 +1,7 @@ package main import ( + "os" "regexp" "strings" "testing" @@ -301,3 +302,218 @@ func TestLuaMathFunctions(t *testing.T) { 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)(\d+).*?(\d+).*?(\d+)`, + luaExpr: `v[1] * v[2] / v[3]`, + expectedFunc: func(content string) string { + // Replace values manually for verification + r := regexp.MustCompile(`(?s)\s*150\s*2\s*4\s*`) + content = r.ReplaceAllString(content, "\n 75\n 2\n 4\n ") + + r = regexp.MustCompile(`(?s)\s*300\s*3\s*2\s*`) + content = r.ReplaceAllString(content, "\n 450\n 3\n 2\n ") + + return content + }, + }, + { + name: "Test data - simple multiplication", + fileContent: string(testDataFile), + regexPattern: `(?s).*?(\d+).*?`, + luaExpr: `*1.5`, + expectedFunc: func(content string) string { + r := regexp.MustCompile(`100`) + return r.ReplaceAllString(content, "150") + }, + }, + { + name: "Test data - multiple capture groups", + fileContent: string(testDataFile), + regexPattern: `(?s).*?(\d+).*?(\d+).*?(\d+).*?`, + luaExpr: `v1 * v2 / v3`, + expectedFunc: func(content string) string { + r := regexp.MustCompile(`50`) + return r.ReplaceAllString(content, "75") + }, + }, + { + name: "Test data - decimal values", + fileContent: string(testDataFile), + regexPattern: `(?s).*?([0-9.]+).*?([0-9.]+).*?`, + luaExpr: `v1 * v2`, + expectedFunc: func(content string) string { + r := regexp.MustCompile(`10.5`) + return r.ReplaceAllString(content, "26.25") + }, + }, + } + + 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)(\d+).*?(\d+).*?(\d+)` + 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, "75") { + t.Errorf("First value not modified correctly, expected 75") + } + if !strings.Contains(modifiedContent, "450") { + t.Errorf("Second value not modified correctly, expected 450") + } + 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).*?(\d+).*?` + 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, "150") { + t.Errorf("Value not modified correctly, expected 150 in the simple test") + t.Logf("Modified content: %s", modifiedContent) + } else { + t.Logf("Simple test passed - found 150") + } + }) + + // 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).*?([0-9.]+).*?([0-9.]+).*?` + 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, "26.25") { + t.Errorf("Decimal value not modified correctly, expected 26.25") + t.Logf("Modified content: %s", modifiedContent) + } else { + t.Logf("Decimal test passed - found 26.25") + } + }) +} diff --git a/test_complex.xml b/test_complex.xml new file mode 100644 index 0000000..01f637a --- /dev/null +++ b/test_complex.xml @@ -0,0 +1,12 @@ + + + 150 + 2 + 4 + + + 300 + 3 + 2 + + diff --git a/test_data.xml b/test_data.xml new file mode 100644 index 0000000..d9f4a51 --- /dev/null +++ b/test_data.xml @@ -0,0 +1,33 @@ + + + + 100 + + + + + 50 + 3 + 2 + + + + 200,2,4 + + + + + 75 + + 4 + 3 + + + + + + + 10.5 + 2.5 + + \ No newline at end of file