Skip to content

Commit 946e488

Browse files
authored
pkg,service: simplify tests with slices.Contains (#4040)
1 parent 20b436d commit 946e488

File tree

5 files changed

+17
-93
lines changed

5 files changed

+17
-93
lines changed

‎pkg/dwarf/line/line_parser_test.go‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os/exec"
1212
"path/filepath"
1313
"runtime"
14+
"slices"
1415
"strings"
1516
"testing"
1617
"time"
@@ -331,14 +332,7 @@ func TestDebugLineC(t *testing.T) {
331332
t.Fatal("Parser could not parse Filenames")
332333
}
333334
for _, fn := range ln.FileNames {
334-
found := false
335-
for _, cmp := range file {
336-
if filepath.ToSlash(fn.Path) == cmp {
337-
found = true
338-
break
339-
}
340-
}
341-
if !found {
335+
if !slices.Contains(file, filepath.ToSlash(fn.Path)) {
342336
t.Fatalf("Found %s does not appear in the filelist\n", fn.Path)
343337
}
344338
}

‎pkg/proc/proc_test.go‎

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"path/filepath"
2020
"reflect"
2121
"runtime"
22+
"slices"
2223
"sort"
2324
"strings"
2425
"sync"
@@ -63,14 +64,7 @@ func TestMain(m *testing.M) {
6364

6465
func matchSkipConditions(conditions ...string) bool {
6566
for _, cond := range conditions {
66-
condfound := false
67-
for _, s := range []string{runtime.GOOS, runtime.GOARCH, testBackend, buildMode} {
68-
if s == cond {
69-
condfound = true
70-
break
71-
}
72-
}
73-
if !condfound {
67+
if !slices.Contains([]string{runtime.GOOS, runtime.GOARCH, testBackend, buildMode}, cond) {
7468
return false
7569
}
7670
}
@@ -183,14 +177,7 @@ func assertLineNumber(p *proc.Target, t *testing.T, lineno int, descr string) (s
183177

184178
func assertLineNumberIn(p *proc.Target, t *testing.T, linenos []int, descr string) (string, int) {
185179
f, l := currentLineNumber(p, t)
186-
found := false
187-
for _, lineno := range linenos {
188-
if l == lineno {
189-
found = true
190-
break
191-
}
192-
}
193-
if !found {
180+
if !slices.Contains(linenos, l) {
194181
_, callerFile, callerLine, _ := runtime.Caller(1)
195182
t.Fatalf("%s expected lines :%#v got %s:%d\n\tat %s:%d", descr, linenos, f, l, callerFile, callerLine)
196183
}
@@ -4012,21 +3999,12 @@ func TestDeadlockBreakpoint(t *testing.T) {
40123999
})
40134000
}
40144001

4015-
func findSource(source string, sources []string) bool {
4016-
for _, s := range sources {
4017-
if s == source {
4018-
return true
4019-
}
4020-
}
4021-
return false
4022-
}
4023-
40244002
func TestListImages(t *testing.T) {
40254003
protest.MustHaveCgo(t)
40264004
pluginFixtures := protest.WithPlugins(t, protest.AllNonOptimized, "plugin1/", "plugin2/")
40274005

40284006
withTestProcessArgs("plugintest", t, ".", []string{pluginFixtures[0].Path, pluginFixtures[1].Path}, protest.AllNonOptimized, func(p *proc.Target, grp *proc.TargetGroup, fixture protest.Fixture) {
4029-
if !findSource(fixture.Source, p.BinInfo().Sources) {
4007+
if !slices.Contains(p.BinInfo().Sources, fixture.Source) {
40304008
t.Fatalf("could not find %s in sources: %q\n", fixture.Source, p.BinInfo().Sources)
40314009
}
40324010

@@ -4043,7 +4021,7 @@ func TestListImages(t *testing.T) {
40434021
if !plugin1Found {
40444022
t.Fatalf("Could not find plugin1")
40454023
}
4046-
if !findSource(fixture.Source, p.BinInfo().Sources) {
4024+
if !slices.Contains(p.BinInfo().Sources, fixture.Source) {
40474025
// Source files for the base program must be available even after a plugin is loaded. Issue #2074.
40484026
t.Fatalf("could not find %s in sources (after loading plugin): %q\n", fixture.Source, p.BinInfo().Sources)
40494027
}

‎pkg/proc/variables_test.go‎

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,14 +1003,7 @@ func TestEvalExpression(t *testing.T) {
10031003
}
10041004
switch e := tc.err.(type) {
10051005
case *altError:
1006-
ok := false
1007-
for _, tgtErr := range e.errs {
1008-
if tgtErr == err.Error() {
1009-
ok = true
1010-
break
1011-
}
1012-
}
1013-
if !ok {
1006+
if !slices.Contains(e.errs, err.Error()) {
10141007
t.Fatalf("Unexpected error. Expected %s got %s", tc.err.Error(), err.Error())
10151008
}
10161009
default:
@@ -1511,14 +1504,7 @@ func testCallFunctionIntl(t *testing.T, grp *proc.TargetGroup, p *proc.Target, t
15111504
}
15121505
switch e := tc.err.(type) {
15131506
case *altError:
1514-
ok := false
1515-
for _, tgtErr := range e.errs {
1516-
if tgtErr == err.Error() {
1517-
ok = true
1518-
break
1519-
}
1520-
}
1521-
if !ok {
1507+
if !slices.Contains(e.errs, err.Error()) {
15221508
t.Fatalf("Unexpected error. Expected %s got %s", tc.err.Error(), err.Error())
15231509
}
15241510
default:
@@ -1598,14 +1584,7 @@ func TestIssue1531(t *testing.T) {
15981584
for i := 0; i < len(mv.Children); i += 2 {
15991585
cv := &mv.Children[i]
16001586
s := constant.StringVal(cv.Value)
1601-
found := false
1602-
for j := range keys {
1603-
if keys[j] == s {
1604-
found = true
1605-
break
1606-
}
1607-
}
1608-
if !found {
1587+
if !slices.Contains(keys, s) {
16091588
t.Errorf("key %q not allowed", s)
16101589
return
16111590
}
@@ -1962,14 +1941,7 @@ func TestClassicMap(t *testing.T) {
19621941
}
19631942
switch e := tc.err.(type) {
19641943
case *altError:
1965-
ok := false
1966-
for _, tgtErr := range e.errs {
1967-
if tgtErr == err.Error() {
1968-
ok = true
1969-
break
1970-
}
1971-
}
1972-
if !ok {
1944+
if !slices.Contains(e.errs, err.Error()) {
19731945
t.Fatalf("Unexpected error. Expected %s got %s", tc.err.Error(), err.Error())
19741946
}
19751947
default:

‎service/dap/server_test.go‎

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"reflect"
1717
"regexp"
1818
"runtime"
19+
"slices"
1920
"strconv"
2021
"strings"
2122
"testing"
@@ -982,13 +983,7 @@ func checkStackFramesNamed(testName string, t *testing.T, got *dap.StackTraceRes
982983
startLineOk = got.Body.StackFrames[0].Line == wantStartLine
983984
}
984985
case []int:
985-
startLineOk = false
986-
for _, ln := range wantStartLine {
987-
if got.Body.StackFrames[0].Line == ln {
988-
startLineOk = true
989-
break
990-
}
991-
}
986+
startLineOk = slices.Contains(wantStartLine, got.Body.StackFrames[0].Line)
992987
}
993988

994989
if wantFrames > 0 && !startLineOk {
@@ -2720,24 +2715,15 @@ func TestRegistersScopeAndVariables(t *testing.T) {
27202715
}
27212716

27222717
func findPcReg(regs []dap.Variable) int {
2718+
pcRegNames := []string{"rip", "pc", "eip", "era"}
27232719
for i, reg := range regs {
2724-
if isPcReg(reg) {
2720+
if slices.Contains(pcRegNames, strings.TrimSpace(reg.Name)) {
27252721
return i
27262722
}
27272723
}
27282724
return -1
27292725
}
27302726

2731-
func isPcReg(reg dap.Variable) bool {
2732-
pcRegNames := []string{"rip", "pc", "eip", "era"}
2733-
for _, name := range pcRegNames {
2734-
if name == strings.TrimSpace(reg.Name) {
2735-
return true
2736-
}
2737-
}
2738-
return false
2739-
}
2740-
27412727
// TestShadowedVariables executes to a breakpoint and checks the shadowed
27422728
// variable is named correctly.
27432729
func TestShadowedVariables(t *testing.T) {

‎service/test/integration2_test.go‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313
"reflect"
1414
"runtime"
15+
"slices"
1516
"strconv"
1617
"strings"
1718
"testing"
@@ -1635,14 +1636,7 @@ func TestTypesCommand(t *testing.T) {
16351636
types, err := c.ListTypes("")
16361637
assertNoError(err, t, "ListTypes()")
16371638

1638-
found := false
1639-
for i := range types {
1640-
if types[i] == "main.astruct" {
1641-
found = true
1642-
break
1643-
}
1644-
}
1645-
if !found {
1639+
if !slices.Contains(types, "main.astruct") {
16461640
t.Fatal("Type astruct not found in ListTypes output")
16471641
}
16481642

0 commit comments

Comments
 (0)