fix:Using the variable on range scope

This commit is contained in:
daheige 2021-06-05 17:11:01 +08:00
parent be82e2f577
commit ae4cc43f9c
3 changed files with 17 additions and 15 deletions

View File

@ -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)
}
}

View File

@ -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])
})
}
}

View File

@ -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)
}
}