Add SortLockedProjects helper func

This commit is contained in:
sam boyer 2016-09-02 12:09:00 -04:00
Родитель d2cc579fe7
Коммит 53c4056f3e
2 изменённых файлов: 48 добавлений и 0 удалений

22
lock.go
Просмотреть файл

@ -1,5 +1,7 @@
package gps
import "sort"
// Lock represents data from a lock file (or however the implementing tool
// chooses to store it) at a particular version that is relevant to the
// satisfiability solving process.
@ -159,3 +161,23 @@ func prepLock(l Lock) Lock {
return rl
}
// SortLockedProjects sorts a slice of LockedProject in alphabetical order by
// ProjectRoot.
func SortLockedProjects(lps []LockedProject) {
sort.Stable(lpsorter(lps))
}
type lpsorter []LockedProject
func (lps lpsorter) Swap(i, j int) {
lps[i], lps[j] = lps[j], lps[i]
}
func (lps lpsorter) Len() int {
return len(lps)
}
func (lps lpsorter) Less(i, j int) bool {
return lps[i].pi.ProjectRoot < lps[j].pi.ProjectRoot
}

26
lock_test.go Normal file
Просмотреть файл

@ -0,0 +1,26 @@
package gps
import (
"reflect"
"testing"
)
func TestLockedProjectSorting(t *testing.T) {
// version doesn't matter here
lps := []LockedProject{
NewLockedProject("github.com/sdboyer/gps", NewVersion("v0.10.0"), "", nil),
NewLockedProject("foo", NewVersion("nada"), "", nil),
NewLockedProject("bar", NewVersion("zip"), "", nil),
NewLockedProject("qux", NewVersion("zilch"), "", nil),
}
lps2 := make([]LockedProject, len(lps))
copy(lps2, lps)
SortLockedProjects(lps2)
// only the two should have switched positions
lps[0], lps[2] = lps[2], lps[0]
if !reflect.DeepEqual(lps, lps2) {
t.Errorf("SortLockedProject did not sort as expected:\n\t(GOT) %s\n\t(WNT) %s", lps2, lps)
}
}