Skip to content

Commit bc7459e

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 7d37b5c commit bc7459e

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
@@ -138,16 +138,24 @@ func Run(t *testing.T, suite TestingSuite) {
138138
methodFinder := reflect.TypeOf(suite)
139139
suiteName := methodFinder.Elem().Name()
140140

141-
for i := 0; i < methodFinder.NumMethod(); i++ {
142-
method := methodFinder.Method(i)
143-
144-
ok, err := methodFilter(method.Name)
141+
var matchMethodRE *regexp.Regexp
142+
if *matchMethod != "" {
143+
var err error
144+
matchMethodRE, err = regexp.Compile(*matchMethod)
145145
if err != nil {
146146
fmt.Fprintf(os.Stderr, "testify: invalid regexp for -m: %s\n", err)
147147
os.Exit(1)
148148
}
149+
}
150+
151+
for i := 0; i < methodFinder.NumMethod(); i++ {
152+
method := methodFinder.Method(i)
149153

150-
if !ok {
154+
if !strings.HasPrefix(method.Name, "Test") {
155+
continue
156+
}
157+
// Apply -testify.m filter
158+
if matchMethodRE != nil && !matchMethodRE.MatchString(method.Name) {
151159
continue
152160
}
153161

@@ -217,15 +225,6 @@ func Run(t *testing.T, suite TestingSuite) {
217225
runTests(t, tests)
218226
}
219227

220-
// Filtering method according to set regular expression
221-
// specified command-line argument -m
222-
func methodFilter(name string) (bool, error) {
223-
if !strings.HasPrefix(name, "Test") {
224-
return false, nil
225-
}
226-
return regexp.MatchString(*matchMethod, name)
227-
}
228-
229228
func runTests(t *testing.T, tests []test) {
230229
if len(tests) == 0 {
231230
t.Log("warning: no tests to run")

0 commit comments

Comments
 (0)