-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a81b233
commit fa50405
Showing
7 changed files
with
147 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package regex | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFindStringMatch(t *testing.T) { | ||
cases := []struct { | ||
Case string | ||
Pattern string | ||
Text string | ||
Index int | ||
Expected string | ||
}{ | ||
{ | ||
Case: "Full match at index 0", | ||
Pattern: `\w+`, | ||
Text: "hello", | ||
Index: 0, | ||
Expected: "hello", | ||
}, | ||
{ | ||
Case: "Capture group at index 1", | ||
Pattern: `hello (\w+)`, | ||
Text: "hello world", | ||
Index: 1, | ||
Expected: "world", | ||
}, | ||
{ | ||
Case: "No matches returns original text", | ||
Pattern: `\d+`, | ||
Text: "hello", | ||
Index: 0, | ||
Expected: "hello", | ||
}, | ||
{ | ||
Case: "Invalid pattern returns original text", | ||
Pattern: `[invalid`, | ||
Text: "hello", | ||
Index: 0, | ||
Expected: "hello", | ||
}, | ||
{ | ||
Case: "Empty text returns empty string", | ||
Pattern: `\w+`, | ||
Text: "", | ||
Index: 0, | ||
Expected: "", | ||
}, | ||
{ | ||
Case: "Index out of bounds returns original text", | ||
Pattern: `(\w+)`, | ||
Text: "hello", | ||
Index: 2, | ||
Expected: "hello", | ||
}, | ||
{ | ||
Case: "Multiple capture groups", | ||
Pattern: `(\w+)\s(\w+)`, | ||
Text: "hello world", | ||
Index: 2, | ||
Expected: "world", | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
got := FindStringMatch(tc.Pattern, tc.Text, tc.Index) | ||
assert.Equal(t, tc.Expected, got, tc.Case) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters