Add menu sort tests

This commit is contained in:
Bjørn Erik Pedersen 2016-02-07 12:34:43 +01:00
parent 5995eaaa08
commit 75044c199f
2 changed files with 28 additions and 1 deletions

View file

@ -152,8 +152,9 @@ func (ms *MenuSorter) Swap(i, j int) { ms.menu[i], ms.menu[j] = ms.menu[j], ms.m
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter. // Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (ms *MenuSorter) Less(i, j int) bool { return ms.by(ms.menu[i], ms.menu[j]) } func (ms *MenuSorter) Less(i, j int) bool { return ms.by(ms.menu[i], ms.menu[j]) }
func (m Menu) Sort() { func (m Menu) Sort() Menu {
MenuEntryBy(defaultMenuEntrySort).Sort(m) MenuEntryBy(defaultMenuEntrySort).Sort(m)
return m
} }
func (m Menu) Limit(n int) Menu { func (m Menu) Limit(n int) Menu {

View file

@ -497,6 +497,32 @@ func TestMenuLimit(t *testing.T) {
assert.Equal(t, m, m.Limit(5)) assert.Equal(t, m, m.Limit(5))
} }
func TestMenuSortByN(t *testing.T) {
for i, this := range []struct {
sortFunc func(p Menu) Menu
assertFunc func(p Menu) bool
}{
{(Menu).Sort, func(p Menu) bool { return p[0].Weight == 1 && p[1].Name == "nx" && p[2].Identifier == "ib" }},
{(Menu).ByWeight, func(p Menu) bool { return p[0].Weight == 1 && p[1].Name == "nx" && p[2].Identifier == "ib" }},
{(Menu).ByName, func(p Menu) bool { return p[0].Name == "na" }},
{(Menu).Reverse, func(p Menu) bool { return p[0].Identifier == "ib" && p[len(p)-1].Identifier == "ia" }},
} {
menu := Menu{&MenuEntry{Weight: 3, Name: "nb", Identifier: "ia"},
&MenuEntry{Weight: 1, Name: "na", Identifier: "ic"},
&MenuEntry{Weight: 1, Name: "nx", Identifier: "ic"},
&MenuEntry{Weight: 2, Name: "nb", Identifier: "ix"},
&MenuEntry{Weight: 2, Name: "nb", Identifier: "ib"}}
sorted := this.sortFunc(menu)
if !this.assertFunc(sorted) {
t.Errorf("[%d] sort error", i)
}
}
}
func TestHomeNodeMenu(t *testing.T) { func TestHomeNodeMenu(t *testing.T) {
viper.Reset() viper.Reset()
defer viper.Reset() defer viper.Reset()