Remove ExpectRegexpsByPath()

All use cases are converted.

Co-Authored-by: Yongrui Lin <yongrlin@outlook.com>
This commit is contained in:
Tim Hockin
2025-08-02 16:54:24 -07:00
committed by yongruilin
parent b922fd9db4
commit 8b08c8e59c
3 changed files with 13 additions and 71 deletions

View File

@@ -19,6 +19,7 @@ package nonzerodefaults
import (
"testing"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
)
@@ -27,13 +28,13 @@ func Test(t *testing.T) {
st.Value(&Struct{
// All zero-values.
}).ExpectRegexpsByPath(map[string][]string{
"stringField": {"Required value"},
"stringPtrField": {"Required value"},
"intField": {"Required value"},
"intPtrField": {"Required value"},
"boolField": {"Required value"},
"boolPtrField": {"Required value"},
}).ExpectMatches(field.ErrorMatcher{}.ByType().ByField(), field.ErrorList{
field.Required(field.NewPath("stringField"), ""),
field.Required(field.NewPath("stringPtrField"), ""),
field.Required(field.NewPath("intField"), ""),
field.Required(field.NewPath("intPtrField"), ""),
field.Required(field.NewPath("boolField"), ""),
field.Required(field.NewPath("boolPtrField"), ""),
})
st.Value(&Struct{

View File

@@ -19,6 +19,7 @@ package zerodefaults
import (
"testing"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
)
@@ -27,13 +28,13 @@ func Test(t *testing.T) {
st.Value(&Struct{
// All zero-values.
}).ExpectRegexpsByPath(map[string][]string{
}).ExpectMatches(field.ErrorMatcher{}.ByType().ByField(), field.ErrorList{
// "stringField": optional value fields with zero defaults are just docs
// "intField": optional value fields with zero defaults are just docs
// "boolField": optional value fields with zero defaults are just docs
"stringPtrField": {"Required value"},
"intPtrField": {"Required value"},
"boolPtrField": {"Required value"},
field.Required(field.NewPath("stringPtrField"), ""),
field.Required(field.NewPath("intPtrField"), ""),
field.Required(field.NewPath("boolPtrField"), ""),
})
st.Value(&Struct{

View File

@@ -28,7 +28,6 @@ import (
"os"
"path"
"reflect"
"regexp"
"sort"
"strings"
"testing"
@@ -358,65 +357,6 @@ func (v *ValidationTester) validateFalseArgsByPath() map[string][]string {
return byPath
}
func (v *ValidationTester) ExpectRegexpsByPath(regexpStringsByPath map[string][]string) *ValidationTester {
v.T.Helper()
v.T.Run(fmt.Sprintf("%T", v.value), func(t *testing.T) {
t.Helper()
errorsByPath := v.getErrorsByPath()
// sanity check
if want, got := len(regexpStringsByPath), len(errorsByPath); got != want {
t.Fatalf("wrong number of error-fields: expected %d, got %d:\nwanted:\n%sgot:\n%s",
want, got, renderByPath(regexpStringsByPath), renderByPath(errorsByPath))
}
// compile regexps
regexpsByPath := map[string][]*regexp.Regexp{}
for field, strs := range regexpStringsByPath {
regexps := make([]*regexp.Regexp, 0, len(strs))
for _, str := range strs {
regexps = append(regexps, regexp.MustCompile(str))
}
regexpsByPath[field] = regexps
}
for field := range errorsByPath {
errors := errorsByPath[field]
regexps := regexpsByPath[field]
// sanity check
if want, got := len(regexps), len(errors); got != want {
t.Fatalf("field %q: wrong number of errors: expected %d, got %d:\nwanted:\n%sgot:\n%s",
field, want, got, renderList(regexpStringsByPath[field]), renderList(errors))
}
// build a set of errors and expectations, so we can track them,
expSet := sets.New(regexps...)
for _, err := range errors {
var found *regexp.Regexp
for _, re := range regexps {
if re.MatchString(err) {
found = re
break // done with regexps
}
}
if found != nil {
expSet.Delete(found)
continue // next error
}
t.Errorf("field %q, error %q did not match any expectation", field, err)
}
if len(expSet) != 0 {
t.Errorf("field %q had unsatisfied expectations: %q", field, expSet.UnsortedList())
}
}
})
return v
}
func (v *ValidationTester) ExpectMatches(matcher field.ErrorMatcher, expected field.ErrorList) *ValidationTester {
v.Helper()