-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_part_test.go
162 lines (148 loc) · 3.47 KB
/
path_part_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
package urit
import (
"github.com/stretchr/testify/require"
"regexp"
"strings"
"testing"
)
func TestPathPart_Match_SingleFixed(t *testing.T) {
vars := &pathVars{}
pt := &pathPart{
fixed: true,
fixedValue: `foo`,
}
require.True(t, pt.match(`foo`, 0, vars, nil, nil))
require.False(t, pt.match(`bar`, 0, vars, nil, nil))
}
func TestPathPart_Match_SingleVar(t *testing.T) {
vars := newPathVars(Names)
pt := &pathPart{
fixed: false,
name: "foo",
}
require.True(t, pt.match(`bar`, 0, vars, nil, nil))
require.Equal(t, 1, vars.Len())
v, ok := vars.Get(0)
require.True(t, ok)
require.Equal(t, `bar`, v)
v, ok = vars.Get("foo")
require.True(t, ok)
require.Equal(t, "bar", v)
}
func TestPathPart_Match_SingleVarRegex(t *testing.T) {
vars := newPathVars(Names)
pt := &pathPart{
fixed: false,
name: "foo",
regexp: regexp.MustCompile(`^[a-z]{3}$`),
}
require.True(t, pt.match(`bar`, 0, vars, nil, nil))
require.Equal(t, 1, vars.Len())
v, ok := vars.Get(0)
require.True(t, ok)
require.Equal(t, `bar`, v)
v, ok = vars.Get("foo")
require.True(t, ok)
require.Equal(t, "bar", v)
require.False(t, pt.match(`123`, 0, vars, nil, nil))
}
func TestPathPart_OverallRegexp(t *testing.T) {
pt := &pathPart{
subParts: []pathPart{
{
fixed: true,
fixedValue: `--`,
},
{
fixed: false,
orgRegexp: `^(?:[a-z\+\-]{3})$`,
name: `foo`,
},
{
fixed: true,
fixedValue: `++`,
},
{
fixed: false,
orgRegexp: `([0-9]*)?`,
name: `bar`,
},
},
}
rx := pt.overallRegexp()
require.NotNil(t, rx)
ss := rx.FindStringSubmatch(`--a+z++12345`)
require.NotEmpty(t, ss)
ss = rx.FindStringSubmatch(`--a+z++`)
require.NotEmpty(t, ss)
vars := newPathVars(Names)
ok := pt.multiMatch(`--a+z++12345`, 0, vars, nil)
require.True(t, ok)
require.Equal(t, 2, vars.Len())
v, ok := vars.Get(0)
require.True(t, ok)
require.Equal(t, `a+z`, v)
v, ok = vars.Get("foo")
require.True(t, ok)
require.Equal(t, `a+z`, v)
v, ok = vars.Get(1)
require.True(t, ok)
require.Equal(t, `12345`, v)
v, ok = vars.Get("bar")
require.True(t, ok)
require.Equal(t, `12345`, v)
ok = pt.multiMatch(`--a+z++`, 0, vars, nil)
require.True(t, ok)
}
func TestPathPart_OverallRegexpMatch(t *testing.T) {
vars := newPathVars(Names)
pt := pathPart{
subParts: []pathPart{
{
fixed: true,
fixedValue: `--`,
},
{
fixed: false,
orgRegexp: ``,
name: `foo`,
},
{
fixed: true,
fixedValue: `++`,
},
{
fixed: false,
orgRegexp: ``,
name: `bar`,
},
},
}
ok := pt.multiMatch(`--abc++123`, 0, vars, nil)
require.True(t, ok)
require.Equal(t, 2, vars.Len())
v, ok := vars.Get(0)
require.True(t, ok)
require.Equal(t, "abc", v)
v, ok = vars.Get(1)
require.True(t, ok)
require.Equal(t, "123", v)
vars.Clear()
ok = pt.multiMatch(`--abc++123`, 0, vars, []VarMatchOption{&fooUpperChecker{}})
require.True(t, ok)
require.Equal(t, 2, vars.Len())
v, ok = vars.Get(0)
require.True(t, ok)
require.Equal(t, "ABC", v)
v, ok = vars.Get(1)
require.True(t, ok)
require.Equal(t, "123", v)
}
type fooUpperChecker struct {
}
func (o *fooUpperChecker) Match(value string, position int, name string, rx *regexp.Regexp, rxs string, pathPos int, vars PathVars) (string, bool) {
return strings.ToUpper(value), true
}
func (o *fooUpperChecker) Applicable(value string, position int, name string, rx *regexp.Regexp, rxs string, pathPos int, vars PathVars) bool {
return name == "foo"
}