Files
BigChef/main_test.go
2025-03-22 02:18:28 +01:00

304 lines
7.2 KiB
Go

package main
import (
"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)
}
}