-
Notifications
You must be signed in to change notification settings - Fork 632
/
Copy pathregex_helpers_test.go
218 lines (211 loc) · 5.13 KB
/
regex_helpers_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package internal
import (
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMatchCaptureGroups(t *testing.T) {
tests := []struct {
name string
input string
pattern string
expected map[string]string
}{
{
name: "go-case",
input: "match this thing",
pattern: `(?P<name>match).*(?P<version>thing)`,
expected: map[string]string{
"name": "match",
"version": "thing",
},
},
{
name: "only matches the first instance",
input: "match this thing batch another think",
pattern: `(?P<name>[mb]atch).*?(?P<version>thin[gk])`,
expected: map[string]string{
"name": "match",
"version": "thing",
},
},
{
name: "nested capture groups",
input: "cool something to match against",
pattern: `((?P<name>match) (?P<version>against))`,
expected: map[string]string{
"name": "match",
"version": "against",
},
},
{
name: "nested optional capture groups",
input: "cool something to match against",
pattern: `((?P<name>match) (?P<version>against))?`,
expected: map[string]string{
"name": "match",
"version": "against",
},
},
{
name: "nested optional capture groups with larger match",
input: "cool something to match against match never",
pattern: `.*?((?P<name>match) (?P<version>(against|never)))?`,
expected: map[string]string{
"name": "match",
"version": "against",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := MatchNamedCaptureGroups(regexp.MustCompile(test.pattern), test.input)
assert.Equal(t, test.expected, actual)
})
}
}
func TestMatchNamedCaptureGroupsFromReader(t *testing.T) {
tests := []struct {
name string
pattern string
input string
want map[string]string
wantErr require.ErrorAssertionFunc
}{
{
name: "match single group",
pattern: `(?P<key>[^1-9]+)`,
input: "key",
want: map[string]string{"key": "key"},
wantErr: require.NoError,
},
{
name: "match multiple groups",
pattern: `(?P<key>[^1-9]+):(?P<value>\w+)`,
input: "key:value",
want: map[string]string{"key": "key", "value": "value"},
wantErr: require.NoError,
},
{
name: "no match",
pattern: `(?P<key>[^1-9]+)`,
input: "2345",
want: nil,
wantErr: require.NoError,
},
{
name: "error empty reader",
pattern: `(?P<key>\w+)`,
input: "",
want: nil,
wantErr: require.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
re := regexp.MustCompile(tt.pattern)
r := strings.NewReader(tt.input)
got, err := MatchNamedCaptureGroupsFromReader(re, r)
tt.wantErr(t, err)
assert.Equal(t, tt.want, got)
})
}
}
func TestMatchAnyFromReader(t *testing.T) {
tests := []struct {
name string
input string
patterns []*regexp.Regexp
want bool
wantErr require.ErrorAssertionFunc
}{
{
name: "match single pattern",
input: "hello world",
patterns: []*regexp.Regexp{regexp.MustCompile(`hello`)},
want: true,
wantErr: require.NoError,
},
{
name: "match multiple patterns",
input: "test case",
patterns: []*regexp.Regexp{regexp.MustCompile(`case`), regexp.MustCompile(`test`)},
want: true,
wantErr: require.NoError,
},
{
name: "no match",
input: "nothing here",
patterns: []*regexp.Regexp{regexp.MustCompile(`absent`)},
want: false,
wantErr: require.NoError,
},
{
name: "error empty reader",
input: "",
patterns: []*regexp.Regexp{regexp.MustCompile(`match`)},
want: false,
wantErr: require.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := strings.NewReader(tt.input)
got, err := MatchAnyFromReader(r, tt.patterns...)
tt.wantErr(t, err)
assert.Equal(t, tt.want, got)
})
}
}
func TestProcessReaderInChunks_ChunkBoundaries(t *testing.T) {
tests := []struct {
name string
input string
chunkSize int
expectedCalls []string
returnOnChunk int
wantErr require.ErrorAssertionFunc
}{
{
name: "go case",
input: "123456789012345",
chunkSize: 4,
returnOnChunk: 2,
expectedCalls: []string{"1234", "345678", "789012"},
wantErr: require.NoError,
},
{
name: "no match",
input: "123456789012345",
chunkSize: 4,
returnOnChunk: -1,
expectedCalls: []string{"1234", "345678", "789012", "12345"},
wantErr: require.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var actualCalls []string
var current int
handler := func(data []byte) (bool, error) {
actualCalls = append(actualCalls, string(data))
if current == tt.returnOnChunk {
return true, nil
}
current++
return false, nil
}
r := strings.NewReader(tt.input)
got, err := processReaderInChunks(r, tt.chunkSize, handler)
tt.wantErr(t, err)
if tt.returnOnChunk == -1 {
assert.False(t, got)
} else {
assert.True(t, got)
}
assert.Equal(t, tt.expectedCalls, actualCalls)
})
}
}