-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath_test.go
111 lines (106 loc) · 3.16 KB
/
path_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
package httprouter
import (
"fmt"
"reflect"
"testing"
)
func Test_resolveKeyPairFromPattern(t *testing.T) {
type args struct {
pattern string
}
tests := []struct {
name string
args args
wantKp []keyPair
}{
// TODO: Add test cases.
{"test-0", args{"/a/b/c/d"}, nil},
{"test-1", args{"/a/:name/c/:id"}, []keyPair{{2, "name"}, {4, "id"}}},
{"test-2", args{"/a/b/:user/*id"}, []keyPair{{3, "user"}, {4, "id"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotKp := resolveKeyPairFromPattern(tt.args.pattern); !reflect.DeepEqual(gotKp, tt.wantKp) {
t.Errorf("resolveKeyPairFromPattern() = %v, want %v", gotKp, tt.wantKp)
}
})
}
}
func Test_resolveParamsFromPath(t *testing.T) {
type args struct {
path string
kp []keyPair
iswildChild bool
}
paramsPools.update(16)
tests := []struct {
name string
args args
want Params
}{
// TODO: Add test cases.
{"test-0", args{"/a/b/c", nil, false}, nil},
{"test-1", args{"/a/b/c", nil, true}, nil},
{"test-2", args{"/a/tanzy2018/c/123", resolveKeyPairFromPattern("/a/:name/c/:id"), false},
Params{{Key: "name", Value: "tanzy2018"}, {Key: "id", Value: "123"}}},
{"test-3", args{"/a/tanzy2018/c/123", resolveKeyPairFromPattern("/a/:user/c/*id"), true},
Params{{Key: "user", Value: "tanzy2018"}, {Key: "id", Value: "123"}}},
{"test-4", args{"/a/tanzy2018/c", resolveKeyPairFromPattern("/a/:user/c/*id"), true},
Params{{Key: "user", Value: "tanzy2018"}, {Key: "id", Value: ""}}},
{"test-5", args{"/a/tanzy2018/c/d/e", resolveKeyPairFromPattern("/a/:user/c/*id"), true},
Params{{Key: "user", Value: "tanzy2018"}, {Key: "id", Value: "d/e"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := resolveParamsFromPath(tt.args.path, tt.args.kp, tt.args.iswildChild); !reflect.DeepEqual(got, tt.want) {
t.Errorf("resolveParamsFromPath() = %v, want %v", got, tt.want)
}
})
}
}
func Test_unifyPattern(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
{"test-0", args{"/a/b/c"}, "/a/b/c"},
{"test-1", args{"/a/:name/c"}, fmt.Sprintf("/a/%s/c", placeHolder)},
{"test-2", args{"/a/:user/c"}, fmt.Sprintf("/a/%s/c", placeHolder)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := unifyPattern(tt.args.path); got != tt.want {
t.Errorf("unifyPattern() = %v, want %v", got, tt.want)
}
})
}
}
func Test_makeSegments(t *testing.T) {
type args struct {
path string
max int
}
tests := []struct {
name string
args args
wantSegaments []string
}{
// TODO: Add test cases.
{"test-0", args{"/a/b/c", 16}, []string{"a", "b", "c"}},
{"test-1", args{"a/b/c", 16}, []string{"a", "b", "c"}},
{"test-2", args{"a/b/c/", 16}, []string{"a", "b", "c"}},
{"test-3", args{"a/b/c/", 2}, []string{"a", "b/c"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotSegaments := makeSegments(tt.args.path, tt.args.max); !reflect.DeepEqual(gotSegaments, tt.wantSegaments) {
t.Errorf("makeSegments() = %v, want %v", gotSegaments, tt.wantSegaments)
}
})
}
}