-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvars_ctr_test.go
134 lines (119 loc) · 2.94 KB
/
vars_ctr_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
package gotten
import (
"fmt"
"github.com/stretchr/testify/assert"
"reflect"
"strconv"
"testing"
)
const (
ComplexPath = "/post/{year}/{m}/{d}/{hash_id}"
)
type (
Day struct {
day int
}
SimpleParams struct {
Year int `type:"path" default:"2018"`
Month string `type:"path" key:"m"`
Day fmt.Stringer `type:"path" key:"d"`
HashId string `type:"path"`
Page int `type:"query" default:"1"`
Num int `type:"query" require:"true"`
}
)
func (day Day) String() string {
return strconv.Itoa(day.day)
}
func TestPathKeyList(t *testing.T) {
// test addKey
keyList := make(PathKeyList)
assert.True(t, keyList.addKey("1"))
assert.True(t, keyList.addKey("2"))
assert.False(t, keyList.addKey("1"))
// test deleteKey
assert.False(t, keyList.deleteKey("3"))
assert.True(t, keyList.deleteKey("1"))
assert.False(t, keyList.deleteKey("1"))
// test empty
for _, testCase := range []struct {
listOne []string
listTwo []string
result bool
}{
{[]string{"1", "2"}, nil, false},
{[]string{"1", "2"}, []string{}, false},
{[]string{"1", "2"}, []string{"1"}, false},
{[]string{"1", "2"}, []string{"1", "2"}, true},
} {
keyList := make(PathKeyList)
for _, key := range testCase.listOne {
keyList.addKey(key)
}
for _, key := range testCase.listTwo {
keyList.deleteKey(key)
}
assert.Equal(t, testCase.result, keyList.empty())
}
}
func TestPathKeyRegexp(t *testing.T) {
assert.NotNil(t, pathKeyRegexp)
// find
for _, testCase := range []struct {
Src string
Result []string
}{
{`/user/{_}`, []string{`{_}`}},
{`/user/{Gid}`, []string{`{Gid}`}},
{`/user/{gid}`, []string{`{gid}`}},
{`/user/{group_id}`, []string{`{group_id}`}},
{`/user/{group1}`, []string{`{group1}`}},
{`/user/{gid}/{uid}`, []string{`{gid}`, `{uid}`}},
{`/user/{group-id}`, []string{}},
{`/user/{}`, []string{}},
{`/user/{0gid}`, []string{}},
} {
result := pathKeyRegexp.FindAllString(testCase.Src, -1)
for i := range result {
assert.Equal(t, testCase.Result[i], result[i])
}
}
}
func TestFieldExportable(t *testing.T) {
for _, testCase := range []struct {
name string
exportable bool
}{
{"Name", true},
{"N", true},
{"nAME", false},
{"name", false},
{"n", false},
} {
assert.Equal(t, testCase.exportable, fieldExportable(testCase.name))
}
}
func TestVarsParser(t *testing.T) {
parser, err := newVarsParser(ComplexPath)
assert.Nil(t, err)
assert.Nil(t, parser.parse(reflect.TypeOf(new(SimpleParams))))
for _, testCase := range []struct {
params *SimpleParams
path string
query string
}{
{&SimpleParams{
Month: "1",
Day: Day{1},
HashId: "1",
Num: 10,
}, `/post/2018/1/1/1`, `num=10&page=1`},
} {
ctr := parser.Build()
assert.Nil(t, ctr.setValues(reflect.ValueOf(testCase.params)))
result, err := ctr.getUrl()
assert.Nil(t, err)
assert.Equal(t, testCase.path, result.Path)
assert.Equal(t, testCase.query, result.RawQuery)
}
}