Refactor everything to processors and implement json and xml processors such as they are
This commit is contained in:
605
processor/regex_test.go
Normal file
605
processor/regex_test.go
Normal file
@@ -0,0 +1,605 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestLogger implements the Logger interface for testing
|
||||
type TestLogger struct {
|
||||
T *testing.T // Reference to the test's *testing.T
|
||||
}
|
||||
|
||||
func (l *TestLogger) Printf(format string, v ...interface{}) {
|
||||
if l.T != nil {
|
||||
l.T.Logf(format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
content := `
|
||||
<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>`)
|
||||
processor := NewRegexProcessor(regex, &TestLogger{T: t})
|
||||
luaExpr := BuildLuaScript("*1.5")
|
||||
|
||||
// Enable verbose logging for this test
|
||||
t.Logf("Running test with regex pattern: %s", regex.String())
|
||||
t.Logf("Original content: %s", content)
|
||||
t.Logf("Lua expression: %s", luaExpr)
|
||||
|
||||
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test", "*1.5")
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing content: %v", err)
|
||||
}
|
||||
|
||||
// Verify match and modification counts
|
||||
if matchCount != 1 {
|
||||
t.Errorf("Expected 1 match, got %d", matchCount)
|
||||
}
|
||||
if modCount != 1 {
|
||||
t.Errorf("Expected 1 modification, got %d", modCount)
|
||||
}
|
||||
|
||||
t.Logf("Modified content: %s", modifiedContent)
|
||||
t.Logf("Expected content: %s", expected)
|
||||
|
||||
// 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) {
|
||||
content := `
|
||||
<config>
|
||||
<item>
|
||||
<value>100</value>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
expected := `
|
||||
<config>
|
||||
<item>
|
||||
<value>150</value>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||
processor := NewRegexProcessor(regex, &TestLogger{})
|
||||
luaExpr := BuildLuaScript("v1 * 1.5") // Use direct assignment syntax
|
||||
|
||||
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test", "v1 * 1.5")
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing content: %v", err)
|
||||
}
|
||||
|
||||
// Verify match and modification counts
|
||||
if matchCount != 1 {
|
||||
t.Errorf("Expected 1 match, got %d", matchCount)
|
||||
}
|
||||
if modCount != 1 {
|
||||
t.Errorf("Expected 1 modification, got %d", modCount)
|
||||
}
|
||||
|
||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
if normalizedModified != normalizedExpected {
|
||||
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShorthandNotationFloats(t *testing.T) {
|
||||
content := `
|
||||
<config>
|
||||
<item>
|
||||
<value>132.671327</value>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
expected := `
|
||||
<config>
|
||||
<item>
|
||||
<value>176.01681007940928</value>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`(?s)<value>(\d*\.?\d+)</value>`)
|
||||
processor := NewRegexProcessor(regex, &TestLogger{})
|
||||
luaExpr := BuildLuaScript("v1 * 1.32671327") // Use direct assignment syntax
|
||||
|
||||
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test", "v1 * 1.32671327")
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing content: %v", err)
|
||||
}
|
||||
|
||||
// Verify match and modification counts
|
||||
if matchCount != 1 {
|
||||
t.Errorf("Expected 1 match, got %d", matchCount)
|
||||
}
|
||||
if modCount != 1 {
|
||||
t.Errorf("Expected 1 modification, got %d", modCount)
|
||||
}
|
||||
|
||||
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) {
|
||||
content := `
|
||||
<config>
|
||||
<item>
|
||||
<value>100</value>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
expected := `
|
||||
<config>
|
||||
<item>
|
||||
<value>150</value>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||
processor := NewRegexProcessor(regex, &TestLogger{})
|
||||
luaExpr := BuildLuaScript("v1 = v1 * 1.5") // Use direct assignment syntax
|
||||
|
||||
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test", "v1 = v1 * 1.5")
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing content: %v", err)
|
||||
}
|
||||
|
||||
// Verify match and modification counts
|
||||
if matchCount != 1 {
|
||||
t.Errorf("Expected 1 match, got %d", matchCount)
|
||||
}
|
||||
if modCount != 1 {
|
||||
t.Errorf("Expected 1 modification, got %d", modCount)
|
||||
}
|
||||
|
||||
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) {
|
||||
content := `
|
||||
<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>`)
|
||||
processor := NewRegexProcessor(regex, &TestLogger{})
|
||||
luaExpr := BuildLuaScript("*1.5")
|
||||
|
||||
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test", "*1.5")
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing content: %v", err)
|
||||
}
|
||||
|
||||
// Verify match and modification counts
|
||||
if matchCount != 3 {
|
||||
t.Errorf("Expected 3 matches, got %d", matchCount)
|
||||
}
|
||||
if modCount != 3 {
|
||||
t.Errorf("Expected 3 modifications, got %d", modCount)
|
||||
}
|
||||
|
||||
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) {
|
||||
content := `
|
||||
<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>`)
|
||||
processor := NewRegexProcessor(regex, &TestLogger{})
|
||||
luaExpr := BuildLuaScript("v1 = v1 * v2") // Use direct assignment syntax
|
||||
|
||||
// Verify the regex matches before processing
|
||||
matches := regex.FindStringSubmatch(content)
|
||||
if len(matches) <= 1 {
|
||||
t.Fatalf("Regex didn't match any capture groups in test input: %v", content)
|
||||
}
|
||||
|
||||
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test", "v1 = v1 * v2")
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing content: %v", err)
|
||||
}
|
||||
|
||||
// Verify match and modification counts
|
||||
if matchCount != 1 {
|
||||
t.Errorf("Expected 1 match, got %d", matchCount)
|
||||
}
|
||||
if modCount != 1 {
|
||||
t.Errorf("Expected 1 modification, got %d", modCount)
|
||||
}
|
||||
|
||||
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) {
|
||||
content := `
|
||||
<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>`)
|
||||
processor := NewRegexProcessor(regex, &TestLogger{})
|
||||
luaExpr := BuildLuaScript("v1 = v1 * v2 / v3; v2 = min(v2 * 2, 5); v3 = max(1, v3 / 2)")
|
||||
|
||||
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test",
|
||||
"v1 = v1 * v2 / v3; v2 = min(v2 * 2, 5); v3 = max(1, v3 / 2)")
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing content: %v", err)
|
||||
}
|
||||
|
||||
// Verify match and modification counts
|
||||
if matchCount != 1 {
|
||||
t.Errorf("Expected 1 match, got %d", matchCount)
|
||||
}
|
||||
if modCount != 1 {
|
||||
t.Errorf("Expected 1 modification, got %d", modCount)
|
||||
}
|
||||
|
||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
if normalizedModified != normalizedExpected {
|
||||
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||
}
|
||||
}
|
||||
|
||||
// Added from main_test.go
|
||||
func TestDecimalValues(t *testing.T) {
|
||||
content := `
|
||||
<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>`)
|
||||
processor := NewRegexProcessor(regex, &TestLogger{})
|
||||
luaExpr := BuildLuaScript("v1 = v1 * v2")
|
||||
|
||||
modifiedContent, _, _, err := processor.ProcessContent(content, luaExpr, "test", "v1 = v1 * v2")
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing content: %v", err)
|
||||
}
|
||||
|
||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
if normalizedModified != normalizedExpected {
|
||||
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||
}
|
||||
}
|
||||
|
||||
// Added from main_test.go
|
||||
func TestLuaMathFunctions(t *testing.T) {
|
||||
content := `
|
||||
<config>
|
||||
<item>
|
||||
<value>16</value>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
expected := `
|
||||
<config>
|
||||
<item>
|
||||
<value>4</value>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||
processor := NewRegexProcessor(regex, &TestLogger{})
|
||||
luaExpr := BuildLuaScript("v1 = math.sqrt(v1)")
|
||||
|
||||
modifiedContent, _, _, err := processor.ProcessContent(content, luaExpr, "test", "v1 = math.sqrt(v1)")
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing content: %v", err)
|
||||
}
|
||||
|
||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
if normalizedModified != normalizedExpected {
|
||||
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||
}
|
||||
}
|
||||
|
||||
// Added from main_test.go
|
||||
func TestDirectAssignment(t *testing.T) {
|
||||
content := `
|
||||
<config>
|
||||
<item>
|
||||
<value>100</value>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
expected := `
|
||||
<config>
|
||||
<item>
|
||||
<value>0</value>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||
processor := NewRegexProcessor(regex, &TestLogger{})
|
||||
luaExpr := BuildLuaScript("=0")
|
||||
|
||||
modifiedContent, _, _, err := processor.ProcessContent(content, luaExpr, "test", "=0")
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing content: %v", err)
|
||||
}
|
||||
|
||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
if normalizedModified != normalizedExpected {
|
||||
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||
}
|
||||
}
|
||||
|
||||
// Added from main_test.go
|
||||
func TestStringAndNumericOperations(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
regexPattern string
|
||||
luaExpression string
|
||||
expectedOutput string
|
||||
expectedMods int
|
||||
}{
|
||||
{
|
||||
name: "Basic numeric multiplication",
|
||||
input: "<value>42</value>",
|
||||
regexPattern: "<value>(\\d+)</value>",
|
||||
luaExpression: "v1 = v1 * 2",
|
||||
expectedOutput: "<value>84</value>",
|
||||
expectedMods: 1,
|
||||
},
|
||||
{
|
||||
name: "Basic string manipulation",
|
||||
input: "<n>test</n>",
|
||||
regexPattern: "<n>(.*?)</n>",
|
||||
luaExpression: "s1 = string.upper(s1)",
|
||||
expectedOutput: "<n>TEST</n>",
|
||||
expectedMods: 1,
|
||||
},
|
||||
{
|
||||
name: "String concatenation",
|
||||
input: "<id>abc123</id>",
|
||||
regexPattern: "<id>(.*?)</id>",
|
||||
luaExpression: "s1 = s1 .. '_modified'",
|
||||
expectedOutput: "<id>abc123_modified</id>",
|
||||
expectedMods: 1,
|
||||
},
|
||||
{
|
||||
name: "Numeric value from string using num()",
|
||||
input: "<price>19.99</price>",
|
||||
regexPattern: "<price>(.*?)</price>",
|
||||
luaExpression: "v1 = num(s1) * 1.2",
|
||||
expectedOutput: "<price>23.987999999999996</price>",
|
||||
expectedMods: 1,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Compile the regex pattern with multiline support
|
||||
pattern := regexp.MustCompile("(?s)" + tt.regexPattern)
|
||||
processor := NewRegexProcessor(pattern, &TestLogger{})
|
||||
luaExpr := BuildLuaScript(tt.luaExpression)
|
||||
|
||||
// Process with our function
|
||||
result, modCount, _, err := processor.ProcessContent(tt.input, luaExpr, "test", tt.luaExpression)
|
||||
if err != nil {
|
||||
t.Fatalf("Process function failed: %v", err)
|
||||
}
|
||||
|
||||
// Check results
|
||||
if result != tt.expectedOutput {
|
||||
t.Errorf("Expected output: %s, got: %s", tt.expectedOutput, result)
|
||||
}
|
||||
|
||||
if modCount != tt.expectedMods {
|
||||
t.Errorf("Expected %d modifications, got %d", tt.expectedMods, modCount)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Added from main_test.go
|
||||
func TestEdgeCases(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
regexPattern string
|
||||
luaExpression string
|
||||
expectedOutput string
|
||||
expectedMods int
|
||||
}{
|
||||
{
|
||||
name: "Empty capture group",
|
||||
input: "<value></value>",
|
||||
regexPattern: "<value>(.*?)</value>",
|
||||
luaExpression: "s1 = 'filled'",
|
||||
expectedOutput: "<value>filled</value>",
|
||||
expectedMods: 1,
|
||||
},
|
||||
{
|
||||
name: "Non-numeric string with numeric operation",
|
||||
input: "<value>abc</value>",
|
||||
regexPattern: "<value>(.*?)</value>",
|
||||
luaExpression: "v1 = v1 * 2", // This would fail if we didn't handle strings properly
|
||||
expectedOutput: "<value>abc</value>", // Should remain unchanged
|
||||
expectedMods: 0, // No modifications
|
||||
},
|
||||
{
|
||||
name: "Invalid number conversion",
|
||||
input: "<value>abc</value>",
|
||||
regexPattern: "<value>(.*?)</value>",
|
||||
luaExpression: "v1 = num(s1) + 10", // num(s1) should return 0
|
||||
expectedOutput: "<value>10</value>",
|
||||
expectedMods: 1,
|
||||
},
|
||||
{
|
||||
name: "Multiline string",
|
||||
input: "<text>Line 1\nLine 2</text>",
|
||||
regexPattern: "<text>(.*?)</text>",
|
||||
luaExpression: "s1 = string.gsub(s1, '\\n', ' - ')",
|
||||
expectedOutput: "<text>Line 1 - Line 2</text>",
|
||||
expectedMods: 1,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Make sure the regex can match across multiple lines
|
||||
pattern := regexp.MustCompile("(?s)" + tt.regexPattern)
|
||||
processor := NewRegexProcessor(pattern, &TestLogger{})
|
||||
luaExpr := BuildLuaScript(tt.luaExpression)
|
||||
|
||||
// Process with our function
|
||||
result, modCount, _, err := processor.ProcessContent(tt.input, luaExpr, "test", tt.luaExpression)
|
||||
if err != nil {
|
||||
t.Fatalf("Process function failed: %v", err)
|
||||
}
|
||||
|
||||
// Check results
|
||||
if result != tt.expectedOutput {
|
||||
t.Errorf("Expected output: %s, got: %s", tt.expectedOutput, result)
|
||||
}
|
||||
|
||||
if modCount != tt.expectedMods {
|
||||
t.Errorf("Expected %d modifications, got %d", tt.expectedMods, modCount)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildLuaScript(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"*1.5", "v1 = v1*1.5"},
|
||||
{"/2", "v1 = v1/2"},
|
||||
{"+10", "v1 = v1+10"},
|
||||
{"-5", "v1 = v1-5"},
|
||||
{"^2", "v1 = v1^2"},
|
||||
{"%2", "v1 = v1%2"},
|
||||
{"=100", "v1 =100"},
|
||||
{"v1 * 2", "v1 = v1 * 2"},
|
||||
{"v1 + v2", "v1 = v1 + v2"},
|
||||
{"math.max(v1, 100)", "v1 = math.max(v1, 100)"},
|
||||
// Added from main_test.go
|
||||
{"s1 .. '_suffix'", "v1 = s1 .. '_suffix'"},
|
||||
{"v1 * v2", "v1 = v1 * v2"},
|
||||
{"s1 .. s2", "v1 = s1 .. s2"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
result := BuildLuaScript(tc.input)
|
||||
if result != tc.expected {
|
||||
t.Errorf("BuildLuaScript(%q): expected %q, got %q", tc.input, tc.expected, result)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user