forked from ethereum/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestmatch.go
77 lines (72 loc) · 1.32 KB
/
testmatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package hivesim
import (
"regexp"
"strings"
)
type testMatcher struct {
suite *regexp.Regexp
test *regexp.Regexp
pattern string
}
func parseTestPattern(p string) (m testMatcher, err error) {
parts := splitRegexp(p)
m.suite, err = regexp.Compile(parts[0])
if err != nil {
return m, err
}
if len(parts) > 1 {
m.test, err = regexp.Compile(strings.Join(parts[1:], "/"))
if err != nil {
return m, err
}
}
m.pattern = p
return m, nil
}
// match checks whether the pattern matches suite and test name.
func (m *testMatcher) match(suite, test string) bool {
if m.suite != nil && !m.suite.MatchString(suite) {
return false
}
if test != "" && m.test != nil && !m.test.MatchString(test) {
return false
}
return true
}
// splitRegexp splits the expression s into /-separated parts.
//
// This is borrowed from package testing.
func splitRegexp(s string) []string {
a := make([]string, 0, strings.Count(s, "/"))
cs := 0
cp := 0
for i := 0; i < len(s); {
switch s[i] {
case '[':
cs++
case ']':
if cs--; cs < 0 { // An unmatched ']' is legal.
cs = 0
}
case '(':
if cs == 0 {
cp++
}
case ')':
if cs == 0 {
cp--
}
case '\\':
i++
case '/':
if cs == 0 && cp == 0 {
a = append(a, s[:i])
s = s[i+1:]
i = 0
continue
}
}
i++
}
return append(a, s)
}