})
}
+
+func TestToPages(t *testing.T) {
+ assert := require.New(t)
+
+ p1, p2 := &testPage{title: "p1"}, &testPage{title: "p2"}
+ pages12 := Pages{p1, p2}
+
+ mustToPages := func(in interface{}) Pages {
+ p, err := ToPages(in)
+ assert.NoError(err)
+ return p
+ }
+
+ assert.Equal(Pages{}, mustToPages(nil))
+ assert.Equal(pages12, mustToPages(pages12))
+ assert.Equal(pages12, mustToPages([]Page{p1, p2}))
+ assert.Equal(pages12, mustToPages([]interface{}{p1, p2}))
+
+ _, err := ToPages("not a page")
+ assert.Error(err)
+}
return nil, errors.New("sequence must be provided")
}
- seqv := reflect.ValueOf(seq)
- seqv, isNil := indirect(seqv)
+ seqv, isNil := indirect(reflect.ValueOf(seq))
if isNil {
return nil, errors.New("can't iterate over a nil value")
}
+ var sliceType reflect.Type
switch seqv.Kind() {
- case reflect.Array, reflect.Slice, reflect.Map:
- // ok
+ case reflect.Array, reflect.Slice:
+ sliceType = seqv.Type()
+ case reflect.Map:
+ sliceType = reflect.SliceOf(seqv.Type().Elem())
default:
return nil, errors.New("can't sort " + reflect.ValueOf(seq).Type().String())
}
// Create a list of pairs that will be used to do the sort
- p := pairList{SortAsc: true, SliceType: reflect.SliceOf(seqv.Type().Elem())}
+ p := pairList{SortAsc: true, SliceType: sliceType}
p.Pairs = make([]pair, seqv.Len())
var sortByField string
package collections
import (
+ "fmt"
"reflect"
"testing"
"github.com/gohugoio/hugo/deps"
)
+type stringsSlice []string
+
func TestSort(t *testing.T) {
t.Parallel()
}{
{[]string{"class1", "class2", "class3"}, nil, "asc", []string{"class1", "class2", "class3"}},
{[]string{"class3", "class1", "class2"}, nil, "asc", []string{"class1", "class2", "class3"}},
+ // Issue 6023
+ {stringsSlice{"class3", "class1", "class2"}, nil, "asc", stringsSlice{"class1", "class2", "class3"}},
+
{[]int{1, 2, 3, 4, 5}, nil, "asc", []int{1, 2, 3, 4, 5}},
{[]int{5, 4, 3, 1, 2}, nil, "asc", []int{1, 2, 3, 4, 5}},
// test sort key parameter is focibly set empty
},
{nil, nil, "asc", false},
} {
- var result interface{}
- var err error
- if test.sortByField == nil {
- result, err = ns.Sort(test.seq)
- } else {
- result, err = ns.Sort(test.seq, test.sortByField, test.sortAsc)
- }
- if b, ok := test.expect.(bool); ok && !b {
- if err == nil {
- t.Errorf("[%d] Sort didn't return an expected error", i)
- }
- } else {
- if err != nil {
- t.Errorf("[%d] failed: %s", i, err)
- continue
+ t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) {
+ var result interface{}
+ var err error
+ if test.sortByField == nil {
+ result, err = ns.Sort(test.seq)
+ } else {
+ result, err = ns.Sort(test.seq, test.sortByField, test.sortAsc)
}
- if !reflect.DeepEqual(result, test.expect) {
- t.Errorf("[%d] Sort called on sequence: %v | sortByField: `%v` | got %v but expected %v", i, test.seq, test.sortByField, result, test.expect)
+
+ if b, ok := test.expect.(bool); ok && !b {
+ if err == nil {
+ t.Fatal("Sort didn't return an expected error")
+ }
+ } else {
+ if err != nil {
+ t.Fatalf("failed: %s", err)
+ }
+ if !reflect.DeepEqual(result, test.expect) {
+ t.Fatalf("Sort called on sequence: %#v | sortByField: `%v` | got\n%#v but expected\n%#v", test.seq, test.sortByField, result, test.expect)
+ }
}
- }
+ })
+
}
}