From ae4cc43f9c610fb498902dc0c8bde4e0813159f1 Mon Sep 17 00:00:00 2001 From: daheige Date: Sat, 5 Jun 2021 17:11:01 +0800 Subject: [PATCH] fix:Using the variable on range scope --- path_test.go | 5 ++--- recovery_test.go | 8 ++++---- tree_test.go | 19 +++++++++++-------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/path_test.go b/path_test.go index caefd63a..e136b1ec 100644 --- a/path_test.go +++ b/path_test.go @@ -80,9 +80,8 @@ func TestPathCleanMallocs(t *testing.T) { t.Skip("skipping malloc count in short mode") } - for _, test := range cleanTests { - allocs := testing.AllocsPerRun(100, func() { cleanPath(test.result) }) - assert.EqualValues(t, allocs, 0) + for k := range cleanTests { + assert.EqualValues(t, testing.AllocsPerRun(100, func() { cleanPath(cleanTests[k].result) }), 0) } } diff --git a/recovery_test.go b/recovery_test.go index 6cc2a47a..2566fe3c 100644 --- a/recovery_test.go +++ b/recovery_test.go @@ -120,8 +120,8 @@ func TestPanicWithBrokenPipe(t *testing.T) { syscall.ECONNRESET: "connection reset by peer", } - for errno, expectMsg := range expectMsgs { - t.Run(expectMsg, func(t *testing.T) { + for k := range expectMsgs { + t.Run(expectMsgs[k], func(t *testing.T) { var buf bytes.Buffer @@ -133,14 +133,14 @@ func TestPanicWithBrokenPipe(t *testing.T) { c.Status(expectCode) // Oops. Client connection closed - e := &net.OpError{Err: &os.SyscallError{Err: errno}} + e := &net.OpError{Err: &os.SyscallError{Err: k}} panic(e) }) // RUN w := performRequest(router, "GET", "/recovery") // TEST assert.Equal(t, expectCode, w.Code) - assert.Contains(t, strings.ToLower(buf.String()), expectMsg) + assert.Contains(t, strings.ToLower(buf.String()), expectMsgs[k]) }) } } diff --git a/tree_test.go b/tree_test.go index 298c5ed0..1cc23e35 100644 --- a/tree_test.go +++ b/tree_test.go @@ -328,20 +328,20 @@ func TestTreeDupliatePath(t *testing.T) { "/search/:query", "/user_:name", } - for _, route := range routes { + for k := range routes { recv := catchPanic(func() { - tree.addRoute(route, fakeHandler(route)) + tree.addRoute(routes[k], fakeHandler(routes[k])) }) if recv != nil { - t.Fatalf("panic inserting route '%s': %v", route, recv) + t.Fatalf("panic inserting route '%s': %v", routes[k], recv) } // Add again recv = catchPanic(func() { - tree.addRoute(route, nil) + tree.addRoute(routes[k], nil) }) if recv == nil { - t.Fatalf("no panic while inserting duplicate route '%s", route) + t.Fatalf("no panic while inserting duplicate route '%s", routes[k]) } } @@ -730,7 +730,7 @@ func TestTreeWildcardConflictEx(t *testing.T) { {"/con:nection", ":nection", `/con:tact`, `:tact`}, } - for _, conflict := range conflicts { + for k := range conflicts { // I have to re-create a 'tree', because the 'tree' will be // in an inconsistent state when the loop recovers from the // panic which threw by 'addRoute' function. @@ -746,10 +746,13 @@ func TestTreeWildcardConflictEx(t *testing.T) { } recv := catchPanic(func() { - tree.addRoute(conflict.route, fakeHandler(conflict.route)) + tree.addRoute(conflicts[k].route, fakeHandler(conflicts[k].route)) }) - if !regexp.MustCompile(fmt.Sprintf("'%s' in new path .* conflicts with existing wildcard '%s' in existing prefix '%s'", conflict.segPath, conflict.existSegPath, conflict.existPath)).MatchString(fmt.Sprint(recv)) { + if !regexp.MustCompile( + fmt.Sprintf("'%s' in new path .* conflicts with existing wildcard '%s' in existing prefix '%s'", + conflicts[k].segPath, conflicts[k].existSegPath, conflicts[k].existPath)). + MatchString(fmt.Sprint(recv)) { t.Fatalf("invalid wildcard conflict error (%v)", recv) } }