Skip to content

Commit 4c81fd5

Browse files
authored
refactor: use slices.Contains to simplify code (#5087)
Signed-off-by: CoolCu <[email protected]>
1 parent a8a31a5 commit 4c81fd5

File tree

2 files changed

+5
-24
lines changed

2 files changed

+5
-24
lines changed

pkg/api/router_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package api_test
66

77
import (
88
"net/http"
9+
"slices"
910
"strings"
1011
"testing"
1112

@@ -427,7 +428,7 @@ func TestEndpointOptions(t *testing.T) {
427428
actualMethods := strings.Split(allowHeader, ", ")
428429

429430
for _, expectedMethod := range tt.expectedMethods {
430-
if !contains(actualMethods, expectedMethod) {
431+
if !slices.Contains(actualMethods, expectedMethod) {
431432
t.Errorf("expected method %s not found for route %s", expectedMethod, tt.route)
432433
}
433434
}
@@ -436,12 +437,3 @@ func TestEndpointOptions(t *testing.T) {
436437
})
437438
}
438439
}
439-
440-
func contains(slice []string, item string) bool {
441-
for _, s := range slice {
442-
if s == item {
443-
return true
444-
}
445-
}
446-
return false
447-
}

pkg/storer/netstore_test.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11+
"slices"
1112
"testing"
1213
"time"
1314

@@ -52,13 +53,7 @@ func testNetStore(t *testing.T, newStorer func(r retrieval.Interface) (*storer.D
5253
for {
5354
select {
5455
case op := <-lstore.PusherFeed():
55-
found := false
56-
for _, ch := range chunks {
57-
if op.Chunk.Equal(ch) {
58-
found = true
59-
break
60-
}
61-
}
56+
found := slices.ContainsFunc(chunks, op.Chunk.Equal)
6257
if !found {
6358
op.Err <- fmt.Errorf("incorrect chunk for push: have %s", op.Chunk.Address())
6459
continue
@@ -110,13 +105,7 @@ func testNetStore(t *testing.T, newStorer func(r retrieval.Interface) (*storer.D
110105
for {
111106
select {
112107
case op := <-lstore.PusherFeed():
113-
found := false
114-
for _, ch := range chunks {
115-
if op.Chunk.Equal(ch) {
116-
found = true
117-
break
118-
}
119-
}
108+
found := slices.ContainsFunc(chunks, op.Chunk.Equal)
120109
if !found {
121110
op.Err <- fmt.Errorf("incorrect chunk for push: have %s", op.Chunk.Address())
122111
continue

0 commit comments

Comments
 (0)