Skip to content

Commit 6d8dd85

Browse files
committed
test: simplify TestExtractZones
Signed-off-by: Oleksandr Redko <[email protected]>
1 parent c10cb9c commit 6d8dd85

File tree

1 file changed

+7
-52
lines changed

1 file changed

+7
-52
lines changed

pkg/networks/usernet/dnshosts/dnshosts_test.go

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ package dnshosts
2828
import (
2929
"fmt"
3030
"net"
31-
"sort"
31+
"slices"
32+
"strings"
3233
"testing"
3334

3435
"github.com/containers/gvisor-tap-vsock/pkg/types"
@@ -96,14 +97,6 @@ func Test_zoneHost(t *testing.T) {
9697

9798
func TestExtractZones(t *testing.T) {
9899
equalZones := func(za, zb []types.Zone) bool {
99-
find := func(list []types.Zone, name string) (types.Zone, bool) {
100-
for _, z := range list {
101-
if z.Name == name {
102-
return z, true
103-
}
104-
}
105-
return types.Zone{}, false
106-
}
107100
equal := func(a, b types.Zone) bool {
108101
if a.Name != b.Name {
109102
return false
@@ -125,10 +118,11 @@ func TestExtractZones(t *testing.T) {
125118
}
126119

127120
for _, a := range za {
128-
b, ok := find(zb, a.Name)
129-
if !ok {
121+
ib := slices.IndexFunc(zb, func(z types.Zone) bool { return z.Name == a.Name })
122+
if ib < 0 {
130123
return false
131124
}
125+
b := zb[ib]
132126
if !equal(a, b) {
133127
return false
134128
}
@@ -182,52 +176,13 @@ func TestExtractZones(t *testing.T) {
182176
t.Run(fmt.Sprint(i), func(t *testing.T) {
183177
gotZones := ExtractZones(hosts)
184178
for _, zone := range gotZones {
185-
sort.Sort(recordSorter(zone.Records))
179+
slices.SortFunc(zone.Records, func(a, b types.Record) int { return strings.Compare(a.Name, b.Name) })
186180
}
187-
sort.Sort(zoneSorter(gotZones))
181+
slices.SortFunc(gotZones, func(a, b types.Zone) int { return strings.Compare(a.Name, b.Name) })
188182

189183
if !equalZones(gotZones, tt.wantZones) {
190184
t.Errorf("extractZones() = %+v, want %+v", gotZones, tt.wantZones)
191185
}
192186
})
193187
}
194188
}
195-
196-
var (
197-
_ sort.Interface = recordSorter(nil)
198-
_ sort.Interface = zoneSorter(nil)
199-
)
200-
201-
type recordSorter []types.Record
202-
203-
// Len implements sort.Interface
204-
func (r recordSorter) Len() int {
205-
return len(r)
206-
}
207-
208-
// Less implements sort.Interface
209-
func (r recordSorter) Less(i, j int) bool {
210-
return r[i].Name < r[j].Name
211-
}
212-
213-
// Swap implements sort.Interface
214-
func (r recordSorter) Swap(i, j int) {
215-
r[i], r[j] = r[j], r[i]
216-
}
217-
218-
type zoneSorter []types.Zone
219-
220-
// Len implements sort.Interface
221-
func (z zoneSorter) Len() int {
222-
return len(z)
223-
}
224-
225-
// Less implements sort.Interface
226-
func (z zoneSorter) Less(i, j int) bool {
227-
return z[i].Name < z[j].Name
228-
}
229-
230-
// Swap implements sort.Interface
231-
func (z zoneSorter) Swap(i, j int) {
232-
z[i], z[j] = z[j], z[i]
233-
}

0 commit comments

Comments
 (0)