-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathreverse_test.go
182 lines (171 loc) · 4.09 KB
/
reverse_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
// Copyright 2012 The Gorilla Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package reverse
import (
"net/url"
"testing"
)
func copyValues(values url.Values) url.Values {
rv := url.Values{}
for k, v := range values {
rv[k] = make([]string, len(v))
copy(rv[k], v)
}
return rv
}
type reverseTest struct {
pattern string
values url.Values
result string
valid bool
}
var reverseTests = []reverseTest{
{
pattern: `^1(\d+)3$`,
values: url.Values{"": []string{"2"}},
result: "123",
valid: true,
},
{
pattern: `^1(\d+)3$`,
values: url.Values{"": []string{"a"}},
result: "1a3",
valid: false,
},
{
pattern: `^4(?P<foo>\d+)6$`,
values: url.Values{"foo": []string{"5"}},
result: "456",
valid: true,
},
{
pattern: `^4(?P<foo>\d+)6$`,
values: url.Values{"foo": []string{"b"}},
result: "4b6",
valid: false,
},
{
pattern: `^7(?P<foo>\d)(\d)0$`,
values: url.Values{"": []string{"9"}, "foo": []string{"8"}},
result: "7890",
valid: true,
},
{
pattern: `^7(?P<foo>\d)(\d)0$`,
values: url.Values{"": []string{"d"}, "foo": []string{"c"}},
result: "7cd0",
valid: false,
},
{
pattern: `(?P<foo>\d)(\d)(?P<foo>\d)`,
values: url.Values{"": []string{"2"}, "foo": []string{"1", "3"}},
result: "123",
valid: true,
},
{
pattern: `(?P<foo>\d)(\d)(?P<foo>\d)`,
values: url.Values{"": []string{"b"}, "foo": []string{"a", "c"}},
result: "abc",
valid: false,
},
}
func TestReverseRegexp(t *testing.T) {
for _, test := range reverseTests {
r, err := CompileRegexp(test.pattern)
if err != nil {
t.Fatal(err)
}
// MatchString()
if r.MatchString(test.result) != test.valid {
t.Errorf("%q: expected match %v, got %v", test.pattern, test.valid, !test.valid)
}
// Values()
if test.valid {
values := r.Values(test.result)
reverted, err := r.Revert(copyValues(values))
if err != nil {
t.Fatalf("%s: pattern: %q, values: %#v, indices: %#v, groups: %#v", err, test.pattern, values, r.indices, r.groups)
}
if reverted != test.result {
t.Errorf("%q: expected reverted %q, got %q for values %v", test.pattern, test.result, reverted, values)
}
}
// Revert()
reverted, err := r.Revert(copyValues(test.values))
if err != nil {
t.Fatalf("%s: pattern: %q, values: %#v, indices: %#v, groups: %#v", err, test.pattern, test.values, r.indices, r.groups)
}
if reverted != test.result {
t.Errorf("%q: expected reverted %q, got %q for values %v", test.pattern, test.result, reverted, test.values)
}
// RevertValid()
reverted, err = r.RevertValid(copyValues(test.values))
if test.valid {
if err != nil {
t.Errorf("%q: expected success on RevertValid, got %v", test.pattern, err)
} else if reverted != test.result {
t.Errorf("%q: expected reverted %q, got %q for values %v", test.pattern, test.result, reverted, test.values)
}
} else {
if err == nil {
t.Errorf("%q: expected error on RevertValid", test.pattern)
}
}
}
}
type groupTest struct {
pattern string
groups []string
indices []int
}
var groupTests = []groupTest{
groupTest{
pattern: `^1(\d+)3$`,
groups: []string{""},
indices: []int{1},
},
groupTest{
pattern: `^1(\d+([a-z]+)(\d+([a-z]+)))(?P<foo>\d+)3([a-z]+(\d+))(?P<bar>\d+)$`,
groups: []string{"", "foo", "", "bar"},
indices: []int{1, 5, 6, 8},
},
}
func TestGroups(t *testing.T) {
for _, test := range groupTests {
r, err := CompileRegexp(test.pattern)
if err != nil {
t.Fatal(err)
}
groups := r.Groups()
indices := r.Indices()
if !stringSliceEqual(test.groups, groups) {
t.Errorf("Expected %v, got %v", test.groups, groups)
}
if !intSliceEqual(test.indices, indices) {
t.Errorf("Expected %v, got %v", test.indices, indices)
}
}
}
func intSliceEqual(a, b []int) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if v != b[k] {
return false
}
}
return true
}
func stringSliceEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if v != b[k] {
return false
}
}
return true
}