-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrestructure_test.go
326 lines (270 loc) · 7.69 KB
/
restructure_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package restructure
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func assertRegion(t *testing.T, s string, begin int, end int, r *Submatch) {
assert.NotNil(t, r)
assert.Equal(t, s, string(r.Bytes))
assert.EqualValues(t, begin, r.Begin)
assert.EqualValues(t, end, r.End)
}
type DotName struct {
Dot string `regexp:"\\."`
Name string `regexp:"\\w+"`
}
type DotExpr struct {
_ struct{} `regexp:"^"`
Head string `regexp:"\\w+"`
Tail *DotName `regexp:"?"`
_ struct{} `regexp:"$"`
}
func TestMatchNameDotName(t *testing.T) {
pattern, err := Compile(DotExpr{}, Options{})
require.NoError(t, err)
var v DotExpr
assert.True(t, pattern.Find(&v, "foo.bar"))
assert.Equal(t, "foo", v.Head)
require.NotNil(t, v.Tail)
assert.Equal(t, ".", v.Tail.Dot)
assert.Equal(t, "bar", v.Tail.Name)
}
func TestMatchNameDotNameHeadOnly(t *testing.T) {
pattern, err := Compile(DotExpr{}, Options{})
require.NoError(t, err)
var v DotExpr
assert.True(t, pattern.Find(&v, "head"))
assert.Equal(t, "head", v.Head)
assert.Nil(t, v.Tail)
}
func TestMatchNameDotNameFails(t *testing.T) {
pattern, err := Compile(DotExpr{}, Options{})
require.NoError(t, err)
var v DotExpr
assert.False(t, pattern.Find(&v, ".oops"))
}
type URL struct {
_ string `regexp:"^"`
Scheme string `regexp:"[[:alpha:]]+" json:"scheme"`
_ string `regexp:"://"`
Host string `regexp:".*" json:"host"`
_ string `regexp:"$"`
}
func TestMatchURL(t *testing.T) {
pattern, err := Compile(URL{}, Options{})
require.NoError(t, err)
var v URL
require.True(t, pattern.Find(&v, "http://example.com"))
assert.Equal(t, "http", v.Scheme)
assert.Equal(t, "example.com", v.Host)
}
func TestCombinationWithJSONTags(t *testing.T) {
pattern, err := Compile(URL{}, Options{})
require.NoError(t, err)
var v URL
require.True(t, pattern.Find(&v, "http://example.com"))
js, err := json.Marshal(&v)
require.NoError(t, err)
assert.Equal(t, "{\"scheme\":\"http\",\"host\":\"example.com\"}", string(js))
}
type PtrURL struct {
_ struct{} `regexp:"^"`
Scheme *string `regexp:"[[:alpha:]]+"`
_ struct{} `regexp:"://"`
Host *string `regexp:".*"`
_ struct{} `regexp:"$"`
}
func TestMatchPtrURL(t *testing.T) {
pattern, err := Compile(PtrURL{}, Options{})
require.NoError(t, err)
var v PtrURL
require.True(t, pattern.Find(&v, "http://example.com"))
require.NotNil(t, v.Scheme)
require.NotNil(t, v.Host)
assert.Equal(t, "http", *v.Scheme)
assert.Equal(t, "example.com", *v.Host)
}
func TestMatchPtrURLFailed(t *testing.T) {
pattern, err := Compile(PtrURL{}, Options{})
require.NoError(t, err)
var v PtrURL
require.False(t, pattern.Find(&v, "oops"))
assert.Nil(t, v.Scheme)
assert.Nil(t, v.Host)
}
type NakedURL struct {
_ string `^`
Scheme string `[[:alpha:]]+`
_ string `://`
Host string `.*`
_ string `$`
}
func TestMatchNakedURL(t *testing.T) {
pattern, err := Compile(NakedURL{}, Options{})
require.NoError(t, err)
var v NakedURL
require.True(t, pattern.Find(&v, "http://example.com"))
assert.Equal(t, "http", v.Scheme)
assert.Equal(t, "example.com", v.Host)
}
type Nothing struct {
X string
}
func TestEmptyPattern(t *testing.T) {
pattern, err := Compile(Nothing{}, Options{})
require.NoError(t, err)
var v Nothing
require.True(t, pattern.Find(&v, "abc"))
}
type Malformed struct {
X string `regexp:"\w"` // this is malformed because \w is not a valid escape sequence
}
func TestErrorOnMalformedTag(t *testing.T) {
_, err := Compile(Malformed{}, Options{})
assert.Error(t, err)
}
type HasSubcaptures struct {
Name string `a(bc)?d`
}
func TestRemoveSubcaptures(t *testing.T) {
pattern, err := Compile(HasSubcaptures{}, Options{})
require.NoError(t, err)
var v HasSubcaptures
require.True(t, pattern.Find(&v, "abcd"))
assert.Equal(t, "abcd", v.Name)
}
type DotNameRegion struct {
Dot *Submatch `regexp:"\\."`
Name *Submatch `regexp:"\\w+"`
}
type DotExprRegion struct {
_ struct{} `regexp:"^"`
Head Submatch `regexp:"\\w+"`
Tail *DotNameRegion `regexp:"?"`
_ struct{} `regexp:"$"`
}
func TestMatchNameDotNameRegion(t *testing.T) {
pattern, err := Compile(DotExprRegion{}, Options{})
require.NoError(t, err)
var v DotExprRegion
assert.True(t, pattern.Find(&v, "foo.bar"))
assertRegion(t, "foo", 0, 3, &v.Head)
assert.NotNil(t, v.Tail)
assertRegion(t, ".", 3, 4, v.Tail.Dot)
assertRegion(t, "bar", 4, 7, v.Tail.Name)
}
type DotNamePos struct {
Begin Pos
Dot string `regexp:"\\."`
Middle Pos
Name string `regexp:"\\w+"`
End Pos
}
type DotExprPos struct {
Begin Pos
_ struct{} `regexp:"^"`
Head string `regexp:"\\w+"`
Middle Pos
Tail *DotNamePos `regexp:"?"`
_ struct{} `regexp:"$"`
End Pos
}
func TestMatchNameDotNamePos(t *testing.T) {
pattern, err := Compile(DotExprPos{}, Options{})
require.NoError(t, err)
var v DotExprPos
assert.True(t, pattern.Find(&v, "foo.bar"))
assert.EqualValues(t, 0, v.Begin)
assert.EqualValues(t, 3, v.Middle)
assert.EqualValues(t, 3, v.Tail.Begin)
assert.EqualValues(t, 4, v.Tail.Middle)
assert.EqualValues(t, 7, v.Tail.End)
assert.EqualValues(t, 7, v.End)
}
type DegeneratePos struct {
X Pos
Y Pos
}
func TestDegeneratePos(t *testing.T) {
// This tests what happens if there are degenerate position captures
pattern, err := Compile(DegeneratePos{}, Options{})
require.NoError(t, err)
var v DegeneratePos
assert.True(t, pattern.Find(&v, "abc"))
assert.EqualValues(t, 0, v.X)
assert.EqualValues(t, 0, v.Y)
}
type UnexportedPos struct {
Exported Pos
unexported Pos
_ struct{} `regexp:"$"`
}
func TestUnexportedPos(t *testing.T) {
// This tests what happens if there are non-exported Pos fields
pattern, err := Compile(UnexportedPos{}, Options{})
require.NoError(t, err)
var v UnexportedPos
assert.True(t, pattern.Find(&v, "abc"))
assert.EqualValues(t, 3, v.Exported)
assert.EqualValues(t, 0, v.unexported) // should be ignored
}
type Word struct {
S string `\w+`
}
func TestFindAllWords_Simple(t *testing.T) {
pattern := MustCompile(Word{}, Options{})
var words []Word
pattern.FindAll(&words, "ham is spam", -1)
require.Len(t, words, 3)
assert.EqualValues(t, "ham", words[0].S)
assert.EqualValues(t, "is", words[1].S)
assert.EqualValues(t, "spam", words[2].S)
}
func TestFindAllWords_Ptr(t *testing.T) {
pattern := MustCompile(Word{}, Options{})
var words []*Word
pattern.FindAll(&words, "ham is spam", -1)
require.Len(t, words, 3)
assert.EqualValues(t, "ham", words[0].S)
assert.EqualValues(t, "is", words[1].S)
assert.EqualValues(t, "spam", words[2].S)
}
func TestFindAllWords_NoMatches(t *testing.T) {
pattern := MustCompile(Word{}, Options{})
var words []*Word
pattern.FindAll(&words, "*&!", -1)
require.Empty(t, words)
}
func TestFindAllWords_ByValueSlicePanics(t *testing.T) {
pattern := MustCompile(Word{}, Options{})
var words []*Word
// This should panic because words is passed by value not by pointer:
assert.Panics(t, func() { pattern.FindAll(words, "*&!", -1) })
}
type WordSubmatch struct {
S *Submatch `\w+`
}
func TestFindAllWords_Regions(t *testing.T) {
pattern := MustCompile(WordSubmatch{}, Options{})
var words []*WordSubmatch
pattern.FindAll(&words, "ham is spam", -1)
require.Len(t, words, 3)
assertRegion(t, "ham", 0, 3, words[0].S)
assertRegion(t, "is", 4, 6, words[1].S)
assertRegion(t, "spam", 7, 11, words[2].S)
}
type ExprWithInt struct {
Number int `regexp:"^\\d+"`
_ string `regexp:"\\s+"`
Animal string `regexp:"\\w+$"`
}
func TestMatchWithInt(t *testing.T) {
pattern, err := Compile(ExprWithInt{}, Options{})
require.NoError(t, err)
var v ExprWithInt
assert.True(t, pattern.Find(&v, "4 wombats"))
assert.Equal(t, 4, v.Number)
assert.Equal(t, "wombats", v.Animal)
}