-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
159 lines (148 loc) · 3.18 KB
/
parser_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
package routing
import (
"fmt"
"strconv"
"testing"
)
func TestParser_Parse_ValidatesValidPaths(t *testing.T) {
paths := []string{
"/",
"/path1",
"/path1/path2",
"/path1/path2/",
"/path1/{id}",
"/path1/{id}/",
"/path1/{id:[0-9]{4}-[0-9]{2}-[0-9]{2}}/",
"/path1/{id:/ab+c/}",
"/path1/{id}/path2",
"/path1/{id:/ab+c/}/path2",
"/{id}",
"/{id:^$}",
"/{id}/",
"/{id}/path1",
"/asdf-{id}",
"/{id}-adfasf",
"/{id}/{name}",
"/{id}/{name}/",
"/{id}-{name}/",
"/{id:[0-9]+}-{name:/ab+c/}/",
}
for _, path := range paths {
parser := newParser(path)
valid, err := parser.parse()
if !valid {
t.Errorf("parser error %v in path %v", err, path)
}
}
}
func TestParser_Parse_NoValidateInvalidPaths(t *testing.T) {
paths := []string{
"",
"//",
"/path1//path2",
"/path1//",
"path1/path2/",
"/path1/{id}}",
"/path1/{{id}",
"/path1/id}",
"/path1/{id",
"/path1/{id{",
"/path1/{id/",
"/path1/{}",
"/path1/{id}{name}",
"/{}",
"/{/}",
"/{",
"/}",
"/{name{id}}",
"/{.id}",
"/{id/name}",
":[0-9]+",
"/:[0-9]+",
"/path1:[0-9]+/{id}",
"/path1/:[0-9]+{id}",
"/path1/{:[0-9]+id}",
"/path1/{id}:[0-9]+",
}
for _, path := range paths {
parser := newParser(path)
valid, _ := parser.parse()
if valid {
t.Errorf("invalid path %v is validated", path)
}
}
}
func TestParser_Parse_ReturnsErrorWhenExpressionIsInvalid(t *testing.T) {
paths := []string{
"/path1/{id:[0-9+}/",
}
for _, path := range paths {
parser := newParser(path)
_, err := parser.parse()
if err == nil {
t.Errorf("the code did not return an error")
}
}
}
func TestParser_Parse_CheckChunks(t *testing.T) {
paths := []string{
"/",
"/path1",
"/path1/path2",
"/path1/path2/",
"/path1/{id}",
"/path1/{id}/",
"/path1/{id}/path2",
"/{id}",
"/{id}/",
"/{id}/path1",
"/asdf-{id}",
"/{id}-adfasf",
"/{id}/{name}",
"/{id}/{name}/",
"/{id}-{name}/",
"/{id:[0-9]+}/",
"/path1/{id:/ab+c/}",
"/path1/{id:/ab+c/}/path2",
"/{id:[0-9]+}-{name:/ab+c/}/",
}
expectedChunks := []string{
"[{0 / }]",
"[{0 /path1 }]",
"[{0 /path1/path2 }]",
"[{0 /path1/path2/ }]",
"[{0 /path1/ } {1 id }]",
"[{0 /path1/ } {1 id } {0 / }]",
"[{0 /path1/ } {1 id } {0 /path2 }]",
"[{0 / } {1 id }]",
"[{0 / } {1 id } {0 / }]",
"[{0 / } {1 id } {0 /path1 }]",
"[{0 /asdf- } {1 id }]",
"[{0 / } {1 id } {0 -adfasf }]",
"[{0 / } {1 id } {0 / } {1 name }]",
"[{0 / } {1 id } {0 / } {1 name } {0 / }]",
"[{0 / } {1 id } {0 - } {1 name } {0 / }]",
"[{0 / } {1 id ^[0-9]+$} {0 / }]",
"[{0 /path1/ } {1 id ^/ab+c/$}]",
"[{0 /path1/ } {1 id ^/ab+c/$} {0 /path2 }]",
"[{0 / } {1 id ^[0-9]+$} {0 - } {1 name ^/ab+c/$} {0 / }]",
}
for index, path := range paths {
parser := newParser(path)
valid, err := parser.parse()
if !valid {
t.Errorf("%v in path %v", err, path)
}
var chunks []string
for _, chunk := range parser.chunks {
expString := ""
if chunk.exp != nil {
expString = chunk.exp.String()
}
chunks = append(chunks, "{"+strconv.Itoa(chunk.t)+" "+chunk.v+" "+expString+"}")
}
if expectedChunks[index] != fmt.Sprintf("%v", chunks) {
t.Errorf("parser error invalid chunk %v for path %v", chunks, path)
}
}
}