-
Notifications
You must be signed in to change notification settings - Fork 0
/
slices_test.go
141 lines (130 loc) · 3.23 KB
/
slices_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"slices"
"testing"
qt "github.com/frankban/quicktest"
)
func Test_difference(t *testing.T) {
t.Parallel()
tests := []struct {
name string
testModules []string
nonTestModules []string
trueTestModules []string
}{
{
name: "empty slices",
testModules: []string{},
nonTestModules: []string{},
trueTestModules: []string{},
},
{
name: "some slices",
testModules: []string{"github.com/frankban/quicktest", "github.com/shirou/gopsutil", "rsc.io/quote"},
nonTestModules: []string{"github.com/hashicorp/nomad", "github.com/pkg/errors", "rsc.io/quote", "testdata/modfiles/mod1", "github.com/LK4D4/joincontext"},
trueTestModules: []string{"github.com/frankban/quicktest", "github.com/shirou/gopsutil"},
},
{
name: "duplicate items I",
testModules: []string{"github.com/frankban/quicktest", "rsc.io/quote", "github.com/shirou/gopsutil", "rsc.io/quote"},
nonTestModules: []string{"github.com/hashicorp/nomad", "github.com/pkg/errors", "rsc.io/quote", "testdata/modfiles/mod1", "github.com/LK4D4/joincontext"},
trueTestModules: []string{"github.com/frankban/quicktest", "github.com/shirou/gopsutil"},
},
{
name: "duplicate items II",
testModules: []string{"github.com/frankban/quicktest", "rsc.io/quote", "github.com/shirou/gopsutil", "rsc.io/quote"},
nonTestModules: []string{"rsc.io/quote", "github.com/hashicorp/nomad", "github.com/pkg/errors", "rsc.io/quote", "testdata/modfiles/mod1", "github.com/LK4D4/joincontext"},
trueTestModules: []string{"github.com/frankban/quicktest", "github.com/shirou/gopsutil"},
},
}
for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := qt.New(t)
got := difference(tt.testModules, tt.nonTestModules)
c.Assert(got, qt.DeepEquals, tt.trueTestModules)
})
}
}
func Test_dedupe(t *testing.T) {
t.Parallel()
tests := []struct {
name string
in []string
want []string
}{
{
name: "empty slice",
in: []string{},
want: []string{},
},
{
name: "nil slice",
in: nil,
want: nil,
},
{
name: "small slice",
in: []string{"a", "a"},
want: []string{"a"},
},
{
name: "large slice",
in: []string{"a", "a", "b", "a"},
want: []string{"a", "b"},
},
}
for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := qt.New(t)
got := dedupe(tt.in)
c.Assert(got, qt.DeepEquals, tt.want)
})
}
}
func Test_contains(t *testing.T) {
t.Parallel()
tests := []struct {
name string
a []string
x string
want bool
}{
{
name: "empty slice",
a: []string{},
x: "",
want: false,
},
{
name: "nil slice",
a: nil,
x: "",
want: false,
},
{
name: "found",
a: []string{"a", "bb", "c"},
x: "bb",
want: true,
},
{
name: "not found",
a: []string{"a", "bb", "c"},
x: "d",
want: false,
},
}
for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := qt.New(t)
got := slices.Contains(tt.a, tt.x)
c.Assert(got, qt.Equals, tt.want)
})
}
}