fix:Using the variable on range scope

This commit is contained in:
daheige 2021-06-05 17:46:11 +08:00
parent ae4cc43f9c
commit c44a2bdfe7
2 changed files with 38 additions and 41 deletions

View File

@ -80,8 +80,8 @@ func TestPathCleanMallocs(t *testing.T) {
t.Skip("skipping malloc count in short mode") t.Skip("skipping malloc count in short mode")
} }
for k := range cleanTests { for key := range cleanTests {
assert.EqualValues(t, testing.AllocsPerRun(100, func() { cleanPath(cleanTests[k].result) }), 0) assert.EqualValues(t, testing.AllocsPerRun(100, func() { cleanPath(cleanTests[key].result) }), 0)
} }
} }
@ -89,8 +89,8 @@ func BenchmarkPathClean(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for _, test := range cleanTests { for key := range cleanTests {
cleanPath(test.path) cleanPath(cleanTests[key].path)
} }
} }
} }

View File

@ -245,17 +245,17 @@ type testRoute struct {
func testRoutes(t *testing.T, routes []testRoute) { func testRoutes(t *testing.T, routes []testRoute) {
tree := &node{} tree := &node{}
for _, route := range routes { for k := range routes {
recv := catchPanic(func() { recv := catchPanic(func() {
tree.addRoute(route.path, nil) tree.addRoute(routes[k].path, nil)
}) })
if route.conflict { if routes[k].conflict {
if recv == nil { if recv == nil {
t.Errorf("no panic for conflicting route '%s'", route.path) t.Errorf("no panic for conflicting route '%s'", routes[k].path)
} }
} else if recv != nil { } else if recv != nil {
t.Errorf("unexpected panic for route '%s': %v", route.path, recv) t.Errorf("unexpected panic for route '%s': %v", routes[k].path, recv)
} }
} }
} }
@ -365,12 +365,9 @@ func TestEmptyWildcardName(t *testing.T) {
"/cmd/:/", "/cmd/:/",
"/src/*", "/src/*",
} }
for _, route := range routes { for k := range routes {
recv := catchPanic(func() { if catchPanic(func() { tree.addRoute(routes[k], nil) }) == nil {
tree.addRoute(route, nil) t.Fatalf("no panic while inserting route with empty wildcard name '%s", routes[k])
})
if recv == nil {
t.Fatalf("no panic while inserting route with empty wildcard name '%s", route)
} }
} }
} }
@ -403,20 +400,20 @@ func TestTreeCatchMaxParams(t *testing.T) {
func TestTreeDoubleWildcard(t *testing.T) { func TestTreeDoubleWildcard(t *testing.T) {
const panicMsg = "only one wildcard per path segment is allowed" const panicMsg = "only one wildcard per path segment is allowed"
routes := [...]string{ routes := []string{
"/:foo:bar", "/:foo:bar",
"/:foo:bar/", "/:foo:bar/",
"/:foo*bar", "/:foo*bar",
} }
for _, route := range routes { for key := range routes {
tree := &node{} tree := &node{}
recv := catchPanic(func() { recv := catchPanic(func() {
tree.addRoute(route, nil) tree.addRoute(routes[key], nil)
}) })
if rs, ok := recv.(string); !ok || !strings.HasPrefix(rs, panicMsg) { if rs, ok := recv.(string); !ok || !strings.HasPrefix(rs, panicMsg) {
t.Fatalf(`"Expected panic "%s" for route '%s', got "%v"`, panicMsg, route, recv) t.Fatalf(`"Expected panic "%s" for route '%s', got "%v"`, panicMsg, routes[key], recv)
} }
} }
} }
@ -426,7 +423,7 @@ func TestTreeDoubleWildcard(t *testing.T) {
routes := [...]string{ routes := [...]string{
"/:id/:name/:id", "/:id/:name/:id",
} }
for _, route := range routes { for k := range routes {
... ...
} }
}*/ }*/
@ -460,12 +457,12 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
"/no/b", "/no/b",
"/api/hello/:name", "/api/hello/:name",
} }
for _, route := range routes { for key := range routes {
recv := catchPanic(func() { recv := catchPanic(func() {
tree.addRoute(route, fakeHandler(route)) tree.addRoute(routes[key], fakeHandler(routes[key]))
}) })
if recv != nil { if recv != nil {
t.Fatalf("panic inserting route '%s': %v", route, recv) t.Fatalf("panic inserting route '%s': %v", routes[key], recv)
} }
} }
@ -572,32 +569,32 @@ func TestTreeFindCaseInsensitivePath(t *testing.T) {
longPath, longPath,
} }
for _, route := range routes { for key := range routes {
recv := catchPanic(func() { recv := catchPanic(func() {
tree.addRoute(route, fakeHandler(route)) tree.addRoute(routes[key], fakeHandler(routes[key]))
}) })
if recv != nil { if recv != nil {
t.Fatalf("panic inserting route '%s': %v", route, recv) t.Fatalf("panic inserting route '%s': %v", routes[key], recv)
} }
} }
// Check out == in for all registered routes // Check out == in for all registered routes
// With fixTrailingSlash = true // With fixTrailingSlash = true
for _, route := range routes { for key := range routes {
out, found := tree.findCaseInsensitivePath(route, true) out, found := tree.findCaseInsensitivePath(routes[key], true)
if !found { if !found {
t.Errorf("Route '%s' not found!", route) t.Errorf("Route '%s' not found!", routes[key])
} else if string(out) != route { } else if string(out) != routes[key] {
t.Errorf("Wrong result for route '%s': %s", route, string(out)) t.Errorf("Wrong result for route '%s': %s", routes[key], string(out))
} }
} }
// With fixTrailingSlash = false // With fixTrailingSlash = false
for _, route := range routes { for key := range routes {
out, found := tree.findCaseInsensitivePath(route, false) out, found := tree.findCaseInsensitivePath(routes[key], false)
if !found { if !found {
t.Errorf("Route '%s' not found!", route) t.Errorf("Route '%s' not found!", routes[key])
} else if string(out) != route { } else if string(out) != routes[key] {
t.Errorf("Wrong result for route '%s': %s", route, string(out)) t.Errorf("Wrong result for route '%s': %s", routes[key], string(out))
} }
} }
@ -730,7 +727,7 @@ func TestTreeWildcardConflictEx(t *testing.T) {
{"/con:nection", ":nection", `/con:tact`, `:tact`}, {"/con:nection", ":nection", `/con:tact`, `:tact`},
} }
for k := range conflicts { for outKey := range conflicts {
// I have to re-create a 'tree', because the 'tree' will be // I have to re-create a 'tree', because the 'tree' will be
// in an inconsistent state when the loop recovers from the // in an inconsistent state when the loop recovers from the
// panic which threw by 'addRoute' function. // panic which threw by 'addRoute' function.
@ -741,17 +738,17 @@ func TestTreeWildcardConflictEx(t *testing.T) {
"/who/foo/hello", "/who/foo/hello",
} }
for _, route := range routes { for k := range routes {
tree.addRoute(route, fakeHandler(route)) tree.addRoute(routes[k], fakeHandler(routes[k]))
} }
recv := catchPanic(func() { recv := catchPanic(func() {
tree.addRoute(conflicts[k].route, fakeHandler(conflicts[k].route)) tree.addRoute(conflicts[outKey].route, fakeHandler(conflicts[outKey].route))
}) })
if !regexp.MustCompile( if !regexp.MustCompile(
fmt.Sprintf("'%s' in new path .* conflicts with existing wildcard '%s' in existing prefix '%s'", fmt.Sprintf("'%s' in new path .* conflicts with existing wildcard '%s' in existing prefix '%s'",
conflicts[k].segPath, conflicts[k].existSegPath, conflicts[k].existPath)). conflicts[outKey].segPath, conflicts[outKey].existSegPath, conflicts[outKey].existPath)).
MatchString(fmt.Sprint(recv)) { MatchString(fmt.Sprint(recv)) {
t.Fatalf("invalid wildcard conflict error (%v)", recv) t.Fatalf("invalid wildcard conflict error (%v)", recv)
} }