@@ -14,19 +14,16 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package testing
17
+ package field
18
18
19
19
import (
20
20
"fmt"
21
21
"reflect"
22
22
"regexp"
23
23
"strings"
24
- "testing"
25
-
26
- field "k8s.io/apimachinery/pkg/util/validation/field"
27
24
)
28
25
29
- // ErrorMatcher is a helper for comparing field. Error objects.
26
+ // ErrorMatcher is a helper for comparing Error objects.
30
27
type ErrorMatcher struct {
31
28
// TODO(thockin): consider whether type is ever NOT required, maybe just
32
29
// assume it.
@@ -42,9 +39,9 @@ type ErrorMatcher struct {
42
39
requireOriginWhenInvalid bool
43
40
}
44
41
45
- // Matches returns true if the two field. Error objects match according to the
42
+ // Matches returns true if the two Error objects match according to the
46
43
// configured criteria.
47
- func (m ErrorMatcher ) Matches (want , got * field. Error ) bool {
44
+ func (m ErrorMatcher ) Matches (want , got * Error ) bool {
48
45
if m .matchType && want .Type != got .Type {
49
46
return false
50
47
}
@@ -58,7 +55,7 @@ func (m ErrorMatcher) Matches(want, got *field.Error) bool {
58
55
if want .Origin != got .Origin {
59
56
return false
60
57
}
61
- if m .requireOriginWhenInvalid && want .Type == field . ErrorTypeInvalid {
58
+ if m .requireOriginWhenInvalid && want .Type == ErrorTypeInvalid {
62
59
if want .Origin == "" || got .Origin == "" {
63
60
return false
64
61
}
@@ -72,7 +69,7 @@ func (m ErrorMatcher) Matches(want, got *field.Error) bool {
72
69
73
70
// Render returns a string representation of the specified Error object,
74
71
// according to the criteria configured in the ErrorMatcher.
75
- func (m ErrorMatcher ) Render (e * field. Error ) string {
72
+ func (m ErrorMatcher ) Render (e * Error ) string {
76
73
buf := strings.Builder {}
77
74
78
75
comma := func () {
@@ -93,7 +90,7 @@ func (m ErrorMatcher) Render(e *field.Error) string {
93
90
comma ()
94
91
buf .WriteString (fmt .Sprintf ("Value=%v" , e .BadValue ))
95
92
}
96
- if m .matchOrigin || m .requireOriginWhenInvalid && e .Type == field . ErrorTypeInvalid {
93
+ if m .matchOrigin || m .requireOriginWhenInvalid && e .Type == ErrorTypeInvalid {
97
94
comma ()
98
95
buf .WriteString (fmt .Sprintf ("Origin=%q" , e .Origin ))
99
96
}
@@ -170,17 +167,25 @@ func (m ErrorMatcher) ByDetailRegexp() ErrorMatcher {
170
167
return m
171
168
}
172
169
170
+ // TestIntf lets users pass a testing.T while not coupling this package to Go's
171
+ // testing package.
172
+ type TestIntf interface {
173
+ Helper ()
174
+ Errorf (format string , args ... any )
175
+ Logf (format string , args ... any )
176
+ }
177
+
173
178
// Test compares two ErrorLists by the criteria configured in this matcher, and
174
179
// fails the test if they don't match. If a given "want" error matches multiple
175
180
// "got" errors, they will all be consumed. This might be OK (e.g. if there are
176
181
// multiple errors on the same field from the same origin) or it might be an
177
182
// insufficiently specific matcher, so these will be logged.
178
- func (m ErrorMatcher ) Test (tb testing. TB , want , got field. ErrorList ) {
183
+ func (m ErrorMatcher ) Test (tb TestIntf , want , got ErrorList ) {
179
184
tb .Helper ()
180
185
181
186
remaining := got
182
187
for _ , w := range want {
183
- tmp := make (field. ErrorList , 0 , len (remaining ))
188
+ tmp := make (ErrorList , 0 , len (remaining ))
184
189
n := 0
185
190
for _ , g := range remaining {
186
191
if m .Matches (w , g ) {
0 commit comments