Run all tests twice - case-sensitive, then case-insensitive

master
Aadhavan Srinivasan 5 days ago
parent 932a20f641
commit 027dfb4d6b

@ -309,6 +309,10 @@ var reTests = []struct {
{`\0009`, nil, "\x009", []Group{{0, 2}}},
{`\141`, nil, "a", []Group{{0, 1}}},
// At this point, the python test suite has a bunch
// of backreference tests. Since my engine doesn't
// implement backreferences, I've skipped those tests.
{`*a`, nil, ``, nil},
{`(*)b`, nil, ``, nil},
{`a**`, nil, ``, nil},
@ -318,10 +322,6 @@ var reTests = []struct {
{`a[b-]`, nil, `a-`, []Group{{0, 2}}},
{`a[b-a]`, nil, `a-`, nil},
// At this point, the python test suite has a bunch
// of backreference tests. Since my engine doesn't
// implement backreferences, I've skipped those tests.
// Todo - add numeric range tests
}
@ -427,6 +427,27 @@ func TestFindAllMatches(t *testing.T) {
}
})
}
fmt.Printf("----------------CASE INSENSITIVE MATCHING----------------")
// Case-insensitive run, with test string case-swapped
for _, test := range reTests {
t.Run(test.re+" "+swapCase(test.str), func(t *testing.T) {
regComp, err := Compile(test.re, append(test.flags, RE_CASE_INSENSITIVE)...)
if err != nil {
if test.result != nil {
panic(fmt.Errorf("Test Error: %v", err))
}
} else {
matchIndices := FindAllMatches(regComp, swapCase(test.str))
zeroGroups := make([]Group, len(matchIndices))
for i, m := range matchIndices {
zeroGroups[i] = m[0]
}
if !slices.Equal(test.result, zeroGroups) {
t.Errorf("Wanted %v Got %v\n", test.result, zeroGroups)
}
}
})
}
}
func TestFindString(t *testing.T) {
@ -452,6 +473,30 @@ func TestFindString(t *testing.T) {
}
})
}
fmt.Printf("----------------CASE INSENSITIVE MATCHING----------------")
// Case-insensitive run, with test string case-swapped
for _, test := range reTests {
t.Run(test.re+" "+swapCase(test.str), func(t *testing.T) {
regComp, err := Compile(test.re, append(test.flags, RE_CASE_INSENSITIVE)...)
if err != nil {
if test.result != nil {
panic(err)
}
} else {
foundString := FindString(regComp, swapCase(test.str))
if len(test.result) == 0 {
if foundString != "" {
t.Errorf("Expected no match got %v\n", foundString)
}
} else {
expectedString := test.str[test.result[0].startIdx:test.result[0].endIdx]
if foundString != expectedString {
t.Errorf("Wanted %v Got %v\n", expectedString, foundString)
}
}
}
})
}
}
func TestFindAllGroups(t *testing.T) {
@ -475,4 +520,26 @@ func TestFindAllGroups(t *testing.T) {
}
})
}
fmt.Printf("----------------CASE INSENSITIVE MATCHING----------------")
// Case-insensitive run, with test string case-swapped
for _, test := range groupTests {
t.Run(test.re+" "+swapCase(test.str), func(t *testing.T) {
regComp, err := Compile(test.re, append(test.flags, RE_CASE_INSENSITIVE)...)
if err != nil {
if test.result != nil {
panic(err)
}
}
matchIndices := FindAllMatches(regComp, swapCase(test.str))
for i := range matchIndices {
for j := range matchIndices[i] {
if matchIndices[i][j].isValid() {
if test.result[i][j] != matchIndices[i][j] {
t.Errorf("Wanted %v Got %v\n", test.result, matchIndices)
}
}
}
}
})
}
}

Loading…
Cancel
Save