-
Notifications
You must be signed in to change notification settings - Fork 5
/
matcher_test.go
72 lines (62 loc) · 1.4 KB
/
matcher_test.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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRegexpMatcher(t *testing.T) {
t.Parallel()
tests := []struct {
desc string
regex string
s string
wantSel []string
wantFull []string
}{
{
desc: "empty",
s: "foo",
wantSel: []string{},
wantFull: []string{},
},
{
desc: "full match",
regex: "a(?:b|c)",
s: "foo ab bar ac",
wantSel: []string{"ab", "ac"},
wantFull: []string{"ab", "ac"},
},
{
desc: "subexp match",
regex: "a(b|c)",
s: "foo ab bar ac",
wantSel: []string{"b", "c"},
wantFull: []string{"ab", "ac"},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
m, err := compileRegexpMatcher(tt.desc, tt.regex)
require.NoError(t, err, "compile regex")
assert.NotPanics(t, func() {
_ = m.String()
}, "String")
t.Run("Name", func(t *testing.T) {
assert.Equal(t, tt.desc, m.Name())
})
t.Run("Matches", func(t *testing.T) {
ms := m.AppendMatches(tt.s, nil)
gotSel := make([]string, len(ms))
gotFull := make([]string, len(ms))
for i, m := range ms {
gotSel[i] = tt.s[m.Sel.Start:m.Sel.End]
gotFull[i] = tt.s[m.Full.Start:m.Full.End]
}
assert.Equal(t, tt.wantSel, gotSel)
assert.Equal(t, tt.wantFull, gotFull)
})
})
}
}