-
Notifications
You must be signed in to change notification settings - Fork 0
/
agext_test.go
223 lines (208 loc) · 7.12 KB
/
agext_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
219
220
221
222
223
// Copyright 2015 ALRUX Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Tests for AGExt additional methods
package regexp
import (
"testing"
)
type FindNamedTest struct {
pattern string
input string
output map[string]string
}
var findNamedTests = []FindNamedTest{
{"[x-z]", "defabcdef", nil},
{"[a-c]", "defabcdef", map[string]string{"": "a"}},
{"([a-c])", "defabcdef", map[string]string{"": "a"}},
{"(?P<test>[a-c])", "defabcdef", map[string]string{"": "a", "test": "a"}},
{"(?P<test>[a-c]+).*", "defabcdef", map[string]string{"": "abcdef", "test": "abc"}},
}
func TestFindNamed(t *testing.T) {
for _, tc := range findNamedTests {
re, err := Compile(tc.pattern)
if err != nil {
t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
continue
}
actual := re.FindStringNamed(tc.input)
if len(actual) != len(tc.output) {
t.Errorf("%q.FindStringNamed(%q) = %q; want %q",
tc.pattern, tc.input, actual, tc.output)
} else {
for key, value := range actual {
if value != tc.output[key] {
t.Errorf("%q.FindStringNamed(%q)[%q] = %q; want %q",
tc.pattern, tc.input, key, value, tc.output[key])
}
}
}
// now try bytes
bactual := re.FindNamed([]byte(tc.input))
if len(bactual) != len(tc.output) {
t.Errorf("%q.FindNamed(%q) = %q; want %q",
tc.pattern, tc.input, bactual, tc.output)
} else {
for key, value := range bactual {
if string(value) != tc.output[key] {
t.Errorf("%q.FindNamed(%q)[%q] = %q; want %q",
tc.pattern, tc.input, key, value, tc.output[key])
}
}
}
}
}
type FindAllNamedTest struct {
pattern string
input string
output []map[string]string
}
var findAllNamedTests = []FindAllNamedTest{
{"[x-z]", "defabcdef", nil},
{"[a-c]", "defabcdef", []map[string]string{{"": "a"}, {"": "b"}, {"": "c"}}},
{"([a-c])", "defabcdef", []map[string]string{{"": "a"}, {"": "b"}, {"": "c"}}},
{"(?P<test>[a-c])", "defabcdef", []map[string]string{{"": "a", "test": "a"}, {"": "b", "test": "b"}, {"": "c", "test": "c"}}},
{"(?P<test>[a-c]+).*", "defabcdef", []map[string]string{{"": "abcdef", "test": "abc"}}},
}
func TestFindAllNamed(t *testing.T) {
for _, tc := range findAllNamedTests {
re, err := Compile(tc.pattern)
if err != nil {
t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
continue
}
actual := re.FindAllStringNamed(tc.input, -1)
if len(actual) != len(tc.output) {
t.Errorf("%q.FindAllStringNamed(%q, -1) = %q; want %q",
tc.pattern, tc.input, actual, tc.output)
} else {
for i, matches := range actual {
if len(matches) != len(tc.output[i]) {
for key, value := range matches {
if value != tc.output[i][key] {
t.Errorf("%q.FindAllStringNamed(%q, -1)[%d][%q] = %q; want %q",
tc.pattern, tc.input, i, key, value, tc.output[i][key])
}
}
}
}
}
if len(tc.output) > 0 {
actual = re.FindAllStringNamed(tc.input, 1)
if len(actual) != 1 {
t.Errorf("%q.FindAllStringNamed(%q, 1) = %q; want %q",
tc.pattern, tc.input, actual, tc.output[:1])
}
}
// now try bytes
bactual := re.FindAllNamed([]byte(tc.input), -1)
if len(bactual) != len(tc.output) {
t.Errorf("%q.FindAllNamed(%q, -1) = %q; want %q",
tc.pattern, tc.input, bactual, tc.output)
} else {
for i, matches := range bactual {
if len(matches) != len(tc.output[i]) {
for key, value := range matches {
if string(value) != tc.output[i][key] {
t.Errorf("%q.FindAllNamed(%q, -1)[%d][%q] = %q; want %q",
tc.pattern, tc.input, i, key, value, tc.output[i][key])
}
}
}
}
}
if len(tc.output) > 0 {
bactual = re.FindAllNamed([]byte(tc.input), 1)
if len(bactual) != 1 {
t.Errorf("%q.FindAllStringNamed(%q, 1) = %q; want %q",
tc.pattern, tc.input, bactual, tc.output[:1])
}
}
}
}
type ReplaceAllSubmatchFuncTest struct {
pattern string
replacement func([]string) string
input, output string
}
var replaceAllSubmatchFuncTests = []ReplaceAllSubmatchFuncTest{
{"[a-c]", func(s []string) string { return "x" + s[0] + "y" }, "defabcdef", "defxayxbyxcydef"},
{"[a-c]+", func(s []string) string { return "x" + s[0] + "y" }, "defabcdef", "defxabcydef"},
{"[a-c]*", func(s []string) string { return "x" + s[0] + "y" }, "defabcdef", "xydxyexyfxabcydxyexyfxy"},
{"x([a-c]+)y", func(s []string) string { return "-" + s[1] + "+" }, "xydxyexyfxabcydxyexyfxy", "xydxyexyf-abc+dxyexyfxy"},
}
func TestReplaceAllSubmatchFunc(t *testing.T) {
for _, tc := range replaceAllSubmatchFuncTests {
re, err := Compile(tc.pattern)
if err != nil {
t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
continue
}
actual := re.ReplaceAllStringSubmatchFunc(tc.input, tc.replacement)
if actual != tc.output {
t.Errorf("%q.ReplaceAllSubmatchFunc(%q,fn) = %q; want %q",
tc.pattern, tc.input, actual, tc.output)
}
// now try bytes
actual = string(re.ReplaceAllSubmatchFunc([]byte(tc.input),
func(b [][]byte) []byte {
s := make([]string, len(b))
for i, v := range b {
s[i] = string(v)
}
return []byte(tc.replacement(s))
}))
if actual != tc.output {
t.Errorf("%q.ReplaceAllSubmatchFunc(%q,fn) = %q; want %q",
tc.pattern, tc.input, actual, tc.output)
}
}
}
type ReplaceAllNamedFuncTest struct {
pattern string
replacement func(map[string]string) string
input, output string
}
var replaceAllNamedFuncTests = []ReplaceAllNamedFuncTest{
{"[a-c]", func(s map[string]string) string { return "x" + s[""] + "y" }, "defabcdef", "defxayxbyxcydef"},
{"[a-c]+", func(s map[string]string) string { return "x" + s[""] + "y" }, "defabcdef", "defxabcydef"},
{"[a-c]*", func(s map[string]string) string { return "x" + s[""] + "y" }, "defabcdef", "xydxyexyfxabcydxyexyfxy"},
{"x(?P<test>[a-c]+)y", func(s map[string]string) string { return "-" + s["test"] + "+" }, "xydxyexyfxabcydxyexyfxy", "xydxyexyf-abc+dxyexyfxy"},
}
func TestReplaceAllNamedFunc(t *testing.T) {
for _, tc := range replaceAllNamedFuncTests {
re, err := Compile(tc.pattern)
if err != nil {
t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
continue
}
actual := re.ReplaceAllStringNamedFunc(tc.input, tc.replacement)
if actual != tc.output {
t.Errorf("%q.ReplaceAllNamedFunc(%q,fn) = %q; want %q",
tc.pattern, tc.input, actual, tc.output)
}
// now try bytes
actual = string(re.ReplaceAllNamedFunc([]byte(tc.input),
func(b map[string][]byte) []byte {
s := make(map[string]string, len(b))
for i, v := range b {
s[i] = string(v)
}
return []byte(tc.replacement(s))
}))
if actual != tc.output {
t.Errorf("%q.ReplaceAllNamedFunc(%q,fn) = %q; want %q",
tc.pattern, tc.input, actual, tc.output)
}
}
}