reference: use subtests for regex tests

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-10 15:37:12 +01:00
parent f481f877f1
commit 5703bcf17d
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -13,6 +13,7 @@ type regexpMatch struct {
} }
func checkRegexp(t *testing.T, r *regexp.Regexp, m regexpMatch) { func checkRegexp(t *testing.T, r *regexp.Regexp, m regexpMatch) {
t.Helper()
matches := r.FindStringSubmatch(m.input) matches := r.FindStringSubmatch(m.input)
if m.match && matches != nil { if m.match && matches != nil {
if len(matches) != (r.NumSubexp()+1) || matches[0] != m.input { if len(matches) != (r.NumSubexp()+1) || matches[0] != m.input {
@ -34,7 +35,10 @@ func checkRegexp(t *testing.T, r *regexp.Regexp, m regexpMatch) {
} }
func TestDomainRegexp(t *testing.T) { func TestDomainRegexp(t *testing.T) {
hostcases := []regexpMatch{ hostcases := []struct {
input string
match bool
}{
{ {
input: "test.com", input: "test.com",
match: true, match: true,
@ -157,8 +161,14 @@ func TestDomainRegexp(t *testing.T) {
}, },
} }
r := regexp.MustCompile(`^` + DomainRegexp.String() + `$`) r := regexp.MustCompile(`^` + DomainRegexp.String() + `$`)
for i := range hostcases { for _, tc := range hostcases {
checkRegexp(t, r, hostcases[i]) tc := tc
t.Run(tc.input, func(t *testing.T) {
match := r.MatchString(tc.input)
if match != tc.match {
t.Errorf("Expected match=%t, got %t", tc.match, match)
}
})
} }
} }
@ -452,8 +462,11 @@ func TestFullNameRegexp(t *testing.T) {
match: false, match: false,
}, },
} }
for i := range testcases { for _, tc := range testcases {
checkRegexp(t, anchoredNameRegexp, testcases[i]) tc := tc
t.Run(tc.input, func(t *testing.T) {
checkRegexp(t, anchoredNameRegexp, tc)
})
} }
} }
@ -522,13 +535,19 @@ func TestReferenceRegexp(t *testing.T) {
}, },
} }
for i := range testcases { for _, tc := range testcases {
checkRegexp(t, ReferenceRegexp, testcases[i]) tc := tc
t.Run(tc.input, func(t *testing.T) {
checkRegexp(t, ReferenceRegexp, tc)
})
} }
} }
func TestIdentifierRegexp(t *testing.T) { func TestIdentifierRegexp(t *testing.T) {
fullCases := []regexpMatch{ fullCases := []struct {
input string
match bool
}{
{ {
input: "da304e823d8ca2b9d863a3c897baeb852ba21ea9a9f1414736394ae7fcaf9821", input: "da304e823d8ca2b9d863a3c897baeb852ba21ea9a9f1414736394ae7fcaf9821",
match: true, match: true,
@ -550,7 +569,13 @@ func TestIdentifierRegexp(t *testing.T) {
match: false, match: false,
}, },
} }
for i := range fullCases { for _, tc := range fullCases {
checkRegexp(t, anchoredIdentifierRegexp, fullCases[i]) tc := tc
t.Run(tc.input, func(t *testing.T) {
match := anchoredIdentifierRegexp.MatchString(tc.input)
if match != tc.match {
t.Errorf("Expected match=%t, got %t", tc.match, match)
}
})
} }
} }