fix testing

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2020-05-08 14:13:10 +08:00
parent fe436bf2c0
commit 0a09a5bb56
2 changed files with 54 additions and 42 deletions

28
tree.go
View File

@ -5,6 +5,7 @@
package gin
import (
"net/url"
"strings"
"unicode"
"unicode/utf8"
@ -478,15 +479,15 @@ walk: // Outer loop for walking the tree
// Expand slice within preallocated capacity
i := len(*value.params)
*value.params = (*value.params)[:i+1]
// val := path[:end]
// if unescape {
// if v, err := url.QueryUnescape(val); err == nil {
// val = v
// }
// }
val := path[:end]
if unescape {
if v, err := url.QueryUnescape(val); err == nil {
val = v
}
}
(*value.params)[i] = Param{
Key: n.path[1:],
Value: path[:end],
Value: val,
}
}
@ -540,14 +541,15 @@ walk: // Outer loop for walking the tree
// Expand slice within preallocated capacity
i := len(*value.params)
*value.params = (*value.params)[:i+1]
// if unescape {
// if v, err := url.QueryUnescape(path); err == nil {
// path = v
// }
// }
val := path
if unescape {
if v, err := url.QueryUnescape(path); err == nil {
val = v
}
}
(*value.params)[i] = Param{
Key: n.path[2:],
Value: path,
Value: val,
}
}

View File

@ -28,6 +28,11 @@ type testRequests []struct {
ps Params
}
func getParams() *Params {
ps := make(Params, 0, 20)
return &ps
}
func checkRequests(t *testing.T, tree *node, requests testRequests, unescapes ...bool) {
unescape := false
if len(unescapes) >= 1 {
@ -35,7 +40,7 @@ func checkRequests(t *testing.T, tree *node, requests testRequests, unescapes ..
}
for _, request := range requests {
value := tree.getValue(request.path, nil, unescape)
value := tree.getValue(request.path, getParams, unescape)
if value.handlers == nil {
if !request.nilHandler {
@ -50,10 +55,13 @@ func checkRequests(t *testing.T, tree *node, requests testRequests, unescapes ..
}
}
if !reflect.DeepEqual(value.params, request.ps) {
if value.params != nil {
if !reflect.DeepEqual(*value.params, request.ps) {
t.Errorf("Params mismatch for route '%s'", request.path)
}
}
}
}
func checkPriorities(t *testing.T, n *node) uint32 {
@ -76,27 +84,27 @@ func checkPriorities(t *testing.T, n *node) uint32 {
return prio
}
func checkMaxParams(t *testing.T, n *node) uint16 {
var maxParams uint16
for i := range n.children {
params := checkMaxParams(t, n.children[i])
if params > maxParams {
maxParams = params
}
}
if n.nType > root && !n.wildChild {
maxParams++
}
// func checkMaxParams(t *testing.T, n *node) uint16 {
// var maxParams uint16
// for i := range n.children {
// params := checkMaxParams(t, n.children[i])
// if params > maxParams {
// maxParams = params
// }
// }
// if n.nType > root && !n.wildChild {
// maxParams++
// }
if n.maxParams != maxParams {
t.Errorf(
"maxParams mismatch for node '%s': is %d, should be %d",
n.path, n.maxParams, maxParams,
)
}
// if n.maxParams != maxParams {
// t.Errorf(
// "maxParams mismatch for node '%s': is %d, should be %d",
// n.path, n.maxParams, maxParams,
// )
// }
return maxParams
}
// return maxParams
// }
func TestCountParams(t *testing.T) {
if countParams("/path/:param1/static/*catch-all") != uint16(2) {
@ -142,7 +150,7 @@ func TestTreeAddAndGet(t *testing.T) {
})
checkPriorities(t, tree)
checkMaxParams(t, tree)
// checkMaxParams(t, tree)
}
func TestTreeWildcard(t *testing.T) {
@ -186,7 +194,7 @@ func TestTreeWildcard(t *testing.T) {
})
checkPriorities(t, tree)
checkMaxParams(t, tree)
// checkMaxParams(t, tree)
}
func TestUnescapeParameters(t *testing.T) {
@ -224,7 +232,7 @@ func TestUnescapeParameters(t *testing.T) {
}, unescape)
checkPriorities(t, tree)
checkMaxParams(t, tree)
// checkMaxParams(t, tree)
}
func catchPanic(testFunc func()) (recv interface{}) {
@ -323,12 +331,14 @@ func TestTreeDupliatePath(t *testing.T) {
}
}
//printChildren(tree, "")
checkRequests(t, tree, testRequests{
{"/", false, "/", nil},
{"/doc/", false, "/doc/", nil},
{"/src/some/file.png", false, "/src/*filepath", Params{Param{Key: "filepath", Value: "/some/file.png"}}},
{"/search/someth!ng+in+ünìcodé", false, "/search/:query", Params{Param{Key: "query", Value: "someth!ng+in+ünìcodé"}}},
{"/user_gopher", false, "/user_:name", Params{Param{Key: "name", Value: "gopher"}}},
{"/src/some/file.png", false, "/src/*filepath", Params{Param{"filepath", "/some/file.png"}}},
{"/search/someth!ng+in+ünìcodé", false, "/search/:query", Params{Param{"query", "someth!ng+in+ünìcodé"}}},
{"/user_gopher", false, "/user_:name", Params{Param{"name", "gopher"}}},
})
}
@ -372,7 +382,7 @@ func TestTreeCatchMaxParams(t *testing.T) {
tree := &node{}
var route = "/cmd/*filepath"
tree.addRoute(route, fakeHandler(route))
checkMaxParams(t, tree)
// checkMaxParams(t, tree)
}
func TestTreeDoubleWildcard(t *testing.T) {