Skip to content

Commit b2caf47

Browse files
committed
suite: faster filtering of methods (-testify.m)
Refactor filtering of methods in suite.Run: the regexp given via -testify.m flag is compiled just once, out of the loop.
1 parent 67aa3d3 commit b2caf47

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

suite/suite.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,24 @@ func Run(t *testing.T, suite TestingSuite) {
140140
methodFinder := reflect.TypeOf(suite)
141141
suiteName := methodFinder.Elem().Name()
142142

143-
for i := 0; i < methodFinder.NumMethod(); i++ {
144-
method := methodFinder.Method(i)
145-
146-
ok, err := methodFilter(method.Name)
143+
var matchMethodRE *regexp.Regexp
144+
if *matchMethod != "" {
145+
var err error
146+
matchMethodRE, err = regexp.Compile(*matchMethod)
147147
if err != nil {
148148
fmt.Fprintf(os.Stderr, "testify: invalid regexp for -m: %s\n", err)
149149
os.Exit(1)
150150
}
151+
}
152+
153+
for i := 0; i < methodFinder.NumMethod(); i++ {
154+
method := methodFinder.Method(i)
151155

152-
if !ok {
156+
if !strings.HasPrefix(method.Name, "Test") {
157+
continue
158+
}
159+
// Apply -testify.m filter
160+
if matchMethodRE != nil && !matchMethodRE.MatchString(method.Name) {
153161
continue
154162
}
155163

@@ -225,15 +233,6 @@ func Run(t *testing.T, suite TestingSuite) {
225233
runTests(t, tests)
226234
}
227235

228-
// Filtering method according to set regular expression
229-
// specified command-line argument -m
230-
func methodFilter(name string) (bool, error) {
231-
if !strings.HasPrefix(name, "Test") {
232-
return false, nil
233-
}
234-
return regexp.MatchString(*matchMethod, name)
235-
}
236-
237236
func runTests(t *testing.T, tests []test) {
238237
if len(tests) == 0 {
239238
t.Log("warning: no tests to run")

0 commit comments

Comments
 (0)