Skip to content

Commit

Permalink
refactor: use sort.Sort instead of sort.Slice
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed May 7, 2024
1 parent 2c73669 commit b2d35b1
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 85 deletions.
33 changes: 13 additions & 20 deletions pkg/dependency/parser/c/conan/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package conan_test
import (
"os"
"sort"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -36,18 +35,6 @@ func TestParse(t *testing.T) {
},
},
},
{
ID: "pkgb/system",
Name: "pkgb",
Version: "system",
Relationship: ftypes.RelationshipIndirect,
Locations: []ftypes.Location{
{
StartLine: 23,
EndLine: 29,
},
},
},
{
ID: "pkgc/0.1.1",
Name: "pkgc",
Expand All @@ -60,6 +47,18 @@ func TestParse(t *testing.T) {
},
},
},
{
ID: "pkgb/system",
Name: "pkgb",
Version: "system",
Relationship: ftypes.RelationshipIndirect,
Locations: []ftypes.Location{
{
StartLine: 23,
EndLine: 29,
},
},
},
},
wantDeps: []ftypes.Dependency{
{
Expand Down Expand Up @@ -156,13 +155,7 @@ func TestParse(t *testing.T) {
gotPkgs, gotDeps, err := conan.NewParser().Parse(f)
require.NoError(t, err)

sort.Slice(gotPkgs, func(i, j int) bool {
ret := strings.Compare(gotPkgs[i].Name, gotPkgs[j].Name)
if ret != 0 {
return ret < 0
}
return gotPkgs[i].Version < gotPkgs[j].Version
})
sort.Sort(ftypes.Packages(gotPkgs))

assert.Equal(t, tt.wantPkgs, gotPkgs)
assert.Equal(t, tt.wantDeps, gotDeps)
Expand Down
18 changes: 2 additions & 16 deletions pkg/dependency/parser/dotnet/core_deps/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"os"
"path"
"sort"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -47,21 +46,8 @@ func TestParse(t *testing.T) {
} else {
require.NoError(t, err)

sort.Slice(got, func(i, j int) bool {
ret := strings.Compare(got[i].Name, got[j].Name)
if ret == 0 {
return got[i].Version < got[j].Version
}
return ret < 0
})

sort.Slice(tt.want, func(i, j int) bool {
ret := strings.Compare(tt.want[i].Name, tt.want[j].Name)
if ret == 0 {
return tt.want[i].Version < tt.want[j].Version
}
return ret < 0
})
sort.Sort(ftypes.Packages(got))
sort.Sort(ftypes.Packages(tt.want))

assert.Equal(t, tt.want, got)
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/dependency/parser/golang/mod/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,8 @@ func TestParse(t *testing.T) {
got, _, err := NewParser(tt.replace).Parse(f)
require.NoError(t, err)

sort.Slice(got, func(i, j int) bool {
return got[i].Name < got[j].Name
})
sort.Slice(tt.want, func(i, j int) bool {
return tt.want[i].Name < tt.want[j].Name
})
sort.Sort(ftypes.Packages(got))
sort.Sort(ftypes.Packages(tt.want))

assert.Equal(t, tt.want, got)
})
Expand Down
8 changes: 2 additions & 6 deletions pkg/dependency/parser/golang/sum/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,8 @@ func TestParse(t *testing.T) {
got[i].ID = "" // Not compare IDs, tested in mod.TestModuleID()
}

sort.Slice(got, func(i, j int) bool {
return got[i].Name < got[j].Name
})
sort.Slice(v.want, func(i, j int) bool {
return v.want[i].Name < v.want[j].Name
})
sort.Sort(ftypes.Packages(got))
sort.Sort(ftypes.Packages(v.want))

assert.Equal(t, v.want, got)
})
Expand Down
8 changes: 2 additions & 6 deletions pkg/dependency/parser/java/jar/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,8 @@ func TestParse(t *testing.T) {
got, _, err := p.Parse(f)
require.NoError(t, err)

sort.Slice(got, func(i, j int) bool {
return got[i].Name < got[j].Name
})
sort.Slice(v.want, func(i, j int) bool {
return v.want[i].Name < v.want[j].Name
})
sort.Sort(ftypes.Packages(got))
sort.Sort(ftypes.Packages(v.want))

assert.Equal(t, v.want, got)
})
Expand Down
17 changes: 2 additions & 15 deletions pkg/dependency/parser/nuget/lock/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,8 @@ func TestParse(t *testing.T) {
got, deps, err := NewParser().Parse(f)
require.NoError(t, err)

sort.Slice(got, func(i, j int) bool {
ret := strings.Compare(got[i].Name, got[j].Name)
if ret == 0 {
return got[i].Version < got[j].Version
}
return ret < 0
})

sort.Slice(v.want, func(i, j int) bool {
ret := strings.Compare(v.want[i].Name, v.want[j].Name)
if ret == 0 {
return v.want[i].Version < v.want[j].Version
}
return ret < 0
})
sort.Sort(ftypes.Packages(got))
sort.Sort(ftypes.Packages(v.want))

assert.Equal(t, v.want, got)

Expand Down
18 changes: 2 additions & 16 deletions pkg/dependency/parser/python/pipenv/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"os"
"path"
"sort"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -40,21 +39,8 @@ func TestParse(t *testing.T) {
got, _, err := NewParser().Parse(f)
require.NoError(t, err)

sort.Slice(got, func(i, j int) bool {
ret := strings.Compare(got[i].Name, got[j].Name)
if ret == 0 {
return got[i].Version < got[j].Version
}
return ret < 0
})

sort.Slice(v.want, func(i, j int) bool {
ret := strings.Compare(v.want[i].Name, v.want[j].Name)
if ret == 0 {
return v.want[i].Version < v.want[j].Version
}
return ret < 0
})
sort.Sort(ftypes.Packages(got))
sort.Sort(ftypes.Packages(v.want))

assert.Equal(t, v.want, got)
})
Expand Down

0 comments on commit b2d35b1

Please sign in to comment.