Fix up the tests and some minor bugs
This commit is contained in:
@@ -281,7 +281,7 @@ func resolveRegexPlaceholders(pattern string) string {
|
||||
replacement := `-?\d*\.?\d+`
|
||||
return parts[1] + replacement
|
||||
})
|
||||
pattern = strings.ReplaceAll(pattern, "!num", `"?(-?\d*\.?\d+)"?`)
|
||||
pattern = strings.ReplaceAll(pattern, "!num", `(-?\d*\.?\d+)`)
|
||||
pattern = strings.ReplaceAll(pattern, "!any", `.*?`)
|
||||
repPattern := regexp.MustCompile(`!rep\(([^,]+),\s*(\d+)\)`)
|
||||
// !rep(pattern, count) repeats the pattern n times
|
||||
|
@@ -1060,19 +1060,6 @@ func TestAnyPlaceholderReplacement(t *testing.T) {
|
||||
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
||||
}
|
||||
|
||||
// "!num" placeholder gets replaced with quoted or unquoted number pattern
|
||||
func TestNumPlaceholderReplacement(t *testing.T) {
|
||||
// Setup
|
||||
pattern := "value: !num"
|
||||
|
||||
// Execute function
|
||||
result := resolveRegexPlaceholders(pattern)
|
||||
|
||||
// Verify results
|
||||
expectedPattern := `value: "?(-?\d*\.?\d+)"?`
|
||||
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
||||
}
|
||||
|
||||
// "!rep(pattern, count)" correctly repeats the pattern with separators
|
||||
func TestRepPatternRepetition(t *testing.T) {
|
||||
// Setup
|
||||
@@ -1082,7 +1069,7 @@ func TestRepPatternRepetition(t *testing.T) {
|
||||
result := resolveRegexPlaceholders(pattern)
|
||||
|
||||
// Verify results
|
||||
expectedPattern := "a.*?a.*?a"
|
||||
expectedPattern := "(?s)a.*?a.*?a"
|
||||
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
||||
}
|
||||
|
||||
@@ -1091,29 +1078,12 @@ func TestMultiplePlaceholdersReplacedCorrectly(t *testing.T) {
|
||||
// Setup
|
||||
pattern := "(?s)some(?<num1>!num)text!anymore!rep(!num, 3)"
|
||||
|
||||
// Redirect stdout to capture fmt.Printf output
|
||||
oldStdout := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
|
||||
// Execute function
|
||||
result := resolveRegexPlaceholders(pattern)
|
||||
|
||||
// Restore stdout
|
||||
w.Close()
|
||||
os.Stdout = oldStdout
|
||||
|
||||
// Read captured output
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, r)
|
||||
output := buf.String()
|
||||
|
||||
// Verify results
|
||||
expectedPattern := "(?s)some(?<num1>-?\\d*\\.?\\d+)text.*?more-?\\d*\\.?\\d+.*?-?\\d*\\.?\\d+.*?-?\\d*\\.?\\d+"
|
||||
expectedPattern := "(?s)some(?<num1>-?\\d*\\.?\\d+)text.*?more(-?\\d*\\.?\\d+).*?(-?\\d*\\.?\\d+).*?(-?\\d*\\.?\\d+)"
|
||||
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
||||
|
||||
expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", pattern)
|
||||
assert.Equal(t, expectedOutput, output, "Expected output message %q, got %q", expectedOutput, output)
|
||||
}
|
||||
|
||||
// Pattern already containing "(?s)" prefix remains unchanged
|
||||
@@ -1155,7 +1125,7 @@ func TestMalformedRepPatternReturnsUnchanged(t *testing.T) {
|
||||
result := resolveRegexPlaceholders(pattern)
|
||||
|
||||
// Verify results
|
||||
expectedPattern := "!rep(somepattern)"
|
||||
expectedPattern := "(?s)!rep(somepattern)"
|
||||
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
||||
}
|
||||
|
||||
@@ -1164,29 +1134,12 @@ func TestNestedPlaceholderPatterns(t *testing.T) {
|
||||
// Setup
|
||||
pattern := "!rep(!num, 3)"
|
||||
|
||||
// Redirect stdout to capture fmt.Printf output
|
||||
oldStdout := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
|
||||
// Execute function
|
||||
result := resolveRegexPlaceholders(pattern)
|
||||
|
||||
// Restore stdout
|
||||
w.Close()
|
||||
os.Stdout = oldStdout
|
||||
|
||||
// Read captured output
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, r)
|
||||
output := buf.String()
|
||||
|
||||
// Verify results
|
||||
expectedPattern := `"?(-?\d*\.?\d+)".*?"?(-?\d*\.?\d+)".*?"?(-?\d*\.?\d+)"`
|
||||
expectedPattern := `(?s)(-?\d*\.?\d+).*?(-?\d*\.?\d+).*?(-?\d*\.?\d+)`
|
||||
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
||||
|
||||
expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", expectedPattern)
|
||||
assert.Equal(t, expectedOutput, output, "Expected output message %q, got %q", expectedOutput, output)
|
||||
}
|
||||
|
||||
// Returns empty slice when input is empty
|
||||
@@ -1317,15 +1270,3 @@ func TestDeduplicateGroupsWithAdjacentNonOverlappingRanges(t *testing.T) {
|
||||
assert.Equal(t, "Group2", result[1].Name, "Expected Group2 to be the second in the result")
|
||||
assert.Equal(t, "Group3", result[2].Name, "Expected Group3 to be the third in the result")
|
||||
}
|
||||
|
||||
// Handles nil groups in the input array
|
||||
func TestDeduplicateGroupsWithNilInput(t *testing.T) {
|
||||
// Arrange
|
||||
captureGroups := []*CaptureGroup{nil, nil}
|
||||
|
||||
// Act
|
||||
result := deduplicateGroups(captureGroups)
|
||||
|
||||
// Assert
|
||||
assert.Empty(t, result, "Expected empty slice when input contains only nil groups")
|
||||
}
|
||||
|
Reference in New Issue
Block a user