-
Notifications
You must be signed in to change notification settings - Fork 1
/
util_test.go
211 lines (180 loc) · 3.63 KB
/
util_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package ginplus
import (
"context"
"reflect"
"testing"
"github.com/gin-gonic/gin"
)
type A struct {
}
type B struct {
}
func (a *A) Middlewares() []gin.HandlerFunc {
return nil
}
func Test_isController(t *testing.T) {
type args struct {
c any
}
tests := []struct {
name string
args args
want bool
}{
{
name: "test",
args: args{
c: &A{},
},
want: true,
},
{
name: "test",
args: args{
c: &B{},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, got := isMiddleware(tt.args.c)
if got != tt.want {
t.Errorf("isController() = %v, want %v", got, tt.want)
}
})
}
}
type (
Call struct {
}
Req struct {
Name string `json:"name"`
}
Resp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data any `json:"data"`
}
)
func (c *Call) CallBack(_ context.Context, _ Req) (Resp, error) {
return Resp{}, nil
}
func Test_isCallBack(t *testing.T) {
ty := reflect.TypeOf(&Call{})
m, ok := ty.MethodByName("CallBack")
if !ok {
t.Error("not found method")
}
req, resp, ok := isCallBack(m.Type)
if ok {
t.Error("not found method CallBack: ", m.Type.String())
}
t.Log(req, resp)
}
func TestGinEngine_parseRoute(t *testing.T) {
instance := New(gin.Default())
t.Log(instance.parseRoute("Delete"))
}
func TestGinEngine_isPublic(t *testing.T) {
if !isPublic("Abc") {
t.Log("Abc is public")
}
if isPublic("abc") {
t.Log("abc is private")
}
}
type Pub struct {
v1 *V1
V1x *V1
*V1
}
type V1 struct {
}
type PubReq struct {
EID string `uri:"eid"`
Id int `uri:"id"`
Name string `json:"name"`
}
type PubResp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data any `json:"data"`
}
func (p *Pub) GetPingA(_ context.Context, _ *PubReq) (*PubResp, error) {
return nil, nil
}
func (p *Pub) getPingB(_ context.Context, _ *PubReq) (*PubResp, error) {
return nil, nil
}
func (p *V1) GetList(_ context.Context, _ *PubReq) (*PubResp, error) {
return nil, nil
}
func TestPrivateMethod(t *testing.T) {
New(gin.Default(), WithControllers(&Pub{
v1: &V1{},
}), AppendHttpMethodPrefixes(
HttpMethod{
Prefix: "get",
Method: httpMethod{get},
},
))
}
func TestGinEngine_genStructRoute(t *testing.T) {
New(gin.Default(), WithControllers(&Pub{
v1: &V1{},
}), AppendHttpMethodPrefixes(
HttpMethod{
Prefix: "Get",
Method: httpMethod{get},
},
))
}
type GenRouteV1 struct{}
type GenRouteV2 struct{}
type GenRouteApi struct {
ApiV1 *GenRouteV1
ApiV2 *GenRouteV2
}
func (g *GenRouteV1) GetPing(_ context.Context, _ *PubReq) (*PubResp, error) {
return nil, nil
}
func (g *GenRouteV2) PostPing(_ context.Context, _ *PubReq) (*PubResp, error) {
return nil, nil
}
func (g *GenRouteApi) PutPing(_ context.Context, _ *PubReq) (*PubResp, error) {
return nil, nil
}
func (g *GenRouteApi) DeletePing(_ context.Context, _ *PubReq) (*PubResp, error) {
return nil, nil
}
func TestGinEngine_genRoute(t *testing.T) {
New(gin.Default(), WithControllers(&GenRouteApi{
ApiV1: &GenRouteV1{},
ApiV2: &GenRouteV2{},
}))
}
type LogicApi struct{}
type LogicApiReq struct {
EID string `uri:"eid" skip:"true"`
PID uint `uri:"pid" skip:"true"`
Id int `uri:"id"`
}
type LogicApiResp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data any `json:"data"`
}
func (l *LogicApi) GetPing(_ context.Context, req *LogicApiReq) (*LogicApiResp, error) {
return &LogicApiResp{
Code: 0,
Msg: "ok",
Data: req,
}, nil
}
func TestGinEngine_GenRoute(t *testing.T) {
i := New(gin.Default())
group := i.Group("/enterprise/:eid/project/:pid")
i.GenRoute(group, &LogicApi{}).RegisterSwaggerUI()
NewCtrlC(i).Start()
}