2015-12-10 17:19:38 -05:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-07-21 15:01:56 -04:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2015-07-25 10:34:35 -04:00
|
|
|
"time"
|
2016-04-22 14:43:00 -04:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-11-28 06:28:24 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-07-21 15:01:56 -04:00
|
|
|
)
|
|
|
|
|
2015-07-25 11:22:19 -04:00
|
|
|
func TestDefaultSort(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2015-07-25 10:34:35 -04:00
|
|
|
d1 := time.Now()
|
2015-07-25 11:22:19 -04:00
|
|
|
d2 := d1.Add(-1 * time.Hour)
|
|
|
|
d3 := d1.Add(-2 * time.Hour)
|
2016-11-06 09:38:52 -05:00
|
|
|
d4 := d1.Add(-3 * time.Hour)
|
2015-07-25 10:34:35 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
s := newTestSite(t)
|
|
|
|
|
|
|
|
p := createSortTestPages(s, 4)
|
2015-07-25 10:34:35 -04:00
|
|
|
|
|
|
|
// first by weight
|
2016-11-06 09:38:52 -05:00
|
|
|
setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "a", "c", "d"}, [4]int{4, 3, 2, 1}, p)
|
2018-09-21 08:23:00 -04:00
|
|
|
p.sort()
|
2016-11-06 09:38:52 -05:00
|
|
|
|
|
|
|
assert.Equal(t, 1, p[0].Weight)
|
|
|
|
|
|
|
|
// Consider zero weight, issue #2673
|
|
|
|
setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "a", "d", "c"}, [4]int{0, 0, 0, 1}, p)
|
2018-09-21 08:23:00 -04:00
|
|
|
p.sort()
|
2016-02-06 16:10:36 -05:00
|
|
|
|
2015-07-25 10:34:35 -04:00
|
|
|
assert.Equal(t, 1, p[0].Weight)
|
|
|
|
|
|
|
|
// next by date
|
2016-11-06 09:38:52 -05:00
|
|
|
setSortVals([4]time.Time{d3, d4, d1, d2}, [4]string{"a", "b", "c", "d"}, [4]int{1, 1, 1, 1}, p)
|
2018-09-21 08:23:00 -04:00
|
|
|
p.sort()
|
2015-07-25 10:34:35 -04:00
|
|
|
assert.Equal(t, d1, p[0].Date)
|
|
|
|
|
2016-02-06 16:10:36 -05:00
|
|
|
// finally by link title
|
2016-11-06 09:38:52 -05:00
|
|
|
setSortVals([4]time.Time{d3, d3, d3, d3}, [4]string{"b", "c", "a", "d"}, [4]int{1, 1, 1, 1}, p)
|
2018-09-21 08:23:00 -04:00
|
|
|
p.sort()
|
2016-02-06 16:10:36 -05:00
|
|
|
assert.Equal(t, "al", p[0].LinkTitle())
|
|
|
|
assert.Equal(t, "bl", p[1].LinkTitle())
|
|
|
|
assert.Equal(t, "cl", p[2].LinkTitle())
|
|
|
|
}
|
|
|
|
|
2018-11-28 06:28:24 -05:00
|
|
|
// https://github.com/gohugoio/hugo/issues/4953
|
|
|
|
func TestSortByLinkTitle(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := require.New(t)
|
|
|
|
s := newTestSite(t)
|
|
|
|
pages := createSortTestPages(s, 6)
|
|
|
|
|
|
|
|
for i, p := range pages {
|
|
|
|
if i < 5 {
|
|
|
|
p.title = fmt.Sprintf("title%d", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > 2 {
|
|
|
|
p.linkTitle = fmt.Sprintf("linkTitle%d", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pages.shuffle()
|
|
|
|
|
|
|
|
bylt := pages.ByLinkTitle()
|
|
|
|
|
|
|
|
for i, p := range bylt {
|
|
|
|
msg := fmt.Sprintf("test: %d", i)
|
|
|
|
if i < 3 {
|
|
|
|
assert.Equal(fmt.Sprintf("linkTitle%d", i+3), p.LinkTitle(), msg)
|
|
|
|
} else {
|
|
|
|
assert.Equal(fmt.Sprintf("title%d", i-3), p.LinkTitle(), msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 16:10:36 -05:00
|
|
|
func TestSortByN(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t)
|
2016-02-06 16:10:36 -05:00
|
|
|
d1 := time.Now()
|
|
|
|
d2 := d1.Add(-2 * time.Hour)
|
|
|
|
d3 := d1.Add(-10 * time.Hour)
|
2016-11-06 09:38:52 -05:00
|
|
|
d4 := d1.Add(-20 * time.Hour)
|
2016-02-06 16:10:36 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
p := createSortTestPages(s, 4)
|
2016-02-06 16:10:36 -05:00
|
|
|
|
|
|
|
for i, this := range []struct {
|
|
|
|
sortFunc func(p Pages) Pages
|
|
|
|
assertFunc func(p Pages) bool
|
|
|
|
}{
|
|
|
|
{(Pages).ByWeight, func(p Pages) bool { return p[0].Weight == 1 }},
|
2018-01-15 14:40:39 -05:00
|
|
|
{(Pages).ByTitle, func(p Pages) bool { return p[0].title == "ab" }},
|
2016-02-06 16:10:36 -05:00
|
|
|
{(Pages).ByLinkTitle, func(p Pages) bool { return p[0].LinkTitle() == "abl" }},
|
2016-11-06 09:38:52 -05:00
|
|
|
{(Pages).ByDate, func(p Pages) bool { return p[0].Date == d4 }},
|
|
|
|
{(Pages).ByPublishDate, func(p Pages) bool { return p[0].PublishDate == d4 }},
|
|
|
|
{(Pages).ByExpiryDate, func(p Pages) bool { return p[0].ExpiryDate == d4 }},
|
|
|
|
{(Pages).ByLastmod, func(p Pages) bool { return p[1].Lastmod == d3 }},
|
2018-04-19 12:06:40 -04:00
|
|
|
{(Pages).ByLength, func(p Pages) bool { return p[0].content() == "b_content" }},
|
2016-02-06 16:10:36 -05:00
|
|
|
} {
|
2016-11-06 09:38:52 -05:00
|
|
|
setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "ab", "cde", "fg"}, [4]int{0, 3, 2, 1}, p)
|
2016-02-06 16:10:36 -05:00
|
|
|
|
|
|
|
sorted := this.sortFunc(p)
|
|
|
|
if !this.assertFunc(sorted) {
|
|
|
|
t.Errorf("[%d] sort error", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLimit(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t)
|
|
|
|
p := createSortTestPages(s, 10)
|
2016-02-06 16:10:36 -05:00
|
|
|
firstFive := p.Limit(5)
|
|
|
|
assert.Equal(t, 5, len(firstFive))
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
assert.Equal(t, p[i], firstFive[i])
|
|
|
|
}
|
|
|
|
assert.Equal(t, p, p.Limit(10))
|
|
|
|
assert.Equal(t, p, p.Limit(11))
|
2015-07-25 10:34:35 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 15:01:56 -04:00
|
|
|
func TestPageSortReverse(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t)
|
|
|
|
p1 := createSortTestPages(s, 10)
|
2016-08-17 07:41:48 -04:00
|
|
|
assert.Equal(t, 0, p1[0].fuzzyWordCount)
|
|
|
|
assert.Equal(t, 9, p1[9].fuzzyWordCount)
|
2015-07-20 19:29:22 -04:00
|
|
|
p2 := p1.Reverse()
|
2016-08-17 07:41:48 -04:00
|
|
|
assert.Equal(t, 9, p2[0].fuzzyWordCount)
|
|
|
|
assert.Equal(t, 0, p2[9].fuzzyWordCount)
|
2015-07-20 19:29:22 -04:00
|
|
|
// cached
|
2018-09-21 04:10:11 -04:00
|
|
|
assert.True(t, pagesEqual(p2, p1.Reverse()))
|
2015-07-21 15:01:56 -04:00
|
|
|
}
|
|
|
|
|
2017-02-10 08:01:25 -05:00
|
|
|
func TestPageSortByParam(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2017-02-19 02:50:08 -05:00
|
|
|
var k interface{} = "arbitrarily.nested"
|
2017-02-04 22:20:06 -05:00
|
|
|
s := newTestSite(t)
|
2017-02-10 08:01:25 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
unsorted := createSortTestPages(s, 10)
|
2018-01-15 14:40:39 -05:00
|
|
|
delete(unsorted[9].params, "arbitrarily")
|
2017-02-10 08:01:25 -05:00
|
|
|
|
|
|
|
firstSetValue, _ := unsorted[0].Param(k)
|
|
|
|
secondSetValue, _ := unsorted[1].Param(k)
|
|
|
|
lastSetValue, _ := unsorted[8].Param(k)
|
|
|
|
unsetValue, _ := unsorted[9].Param(k)
|
|
|
|
|
|
|
|
assert.Equal(t, "xyz100", firstSetValue)
|
|
|
|
assert.Equal(t, "xyz99", secondSetValue)
|
|
|
|
assert.Equal(t, "xyz92", lastSetValue)
|
|
|
|
assert.Equal(t, nil, unsetValue)
|
|
|
|
|
2017-02-19 02:50:08 -05:00
|
|
|
sorted := unsorted.ByParam("arbitrarily.nested")
|
2017-02-10 08:01:25 -05:00
|
|
|
firstSetSortedValue, _ := sorted[0].Param(k)
|
|
|
|
secondSetSortedValue, _ := sorted[1].Param(k)
|
|
|
|
lastSetSortedValue, _ := sorted[8].Param(k)
|
|
|
|
unsetSortedValue, _ := sorted[9].Param(k)
|
|
|
|
|
|
|
|
assert.Equal(t, firstSetValue, firstSetSortedValue)
|
|
|
|
assert.Equal(t, secondSetValue, lastSetSortedValue)
|
|
|
|
assert.Equal(t, lastSetValue, secondSetSortedValue)
|
|
|
|
assert.Equal(t, unsetValue, unsetSortedValue)
|
|
|
|
}
|
|
|
|
|
Support numeric sort in ByParam
With this commit ByParam takes into account a type of a value under a
key. If both values are numeric then they're coerced into float64 and
then get compared.
If any value isn't numeric, for example it's nil or string, then both
values coerced into string and get compared as strings
(lexicographicaly)
Nil values are always sent to the end.
Numeric values confirm to any type listed below:
uint8, uint16, uint32, uint64, int, int8, int16, int32, int64, float32, float64
Closes #5305
2019-01-15 07:41:54 -05:00
|
|
|
func TestPageSortByParamNumeric(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
var k interface{} = "arbitrarily.nested"
|
|
|
|
s := newTestSite(t)
|
|
|
|
|
|
|
|
n := 10
|
|
|
|
unsorted := createSortTestPages(s, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
v := 100 - i
|
|
|
|
if i%2 == 0 {
|
|
|
|
v = 100.0 - i
|
|
|
|
}
|
|
|
|
|
|
|
|
unsorted[i].params = map[string]interface{}{
|
|
|
|
"arbitrarily": map[string]interface{}{
|
|
|
|
"nested": v,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(unsorted[9].params, "arbitrarily")
|
|
|
|
|
|
|
|
firstSetValue, _ := unsorted[0].Param(k)
|
|
|
|
secondSetValue, _ := unsorted[1].Param(k)
|
|
|
|
lastSetValue, _ := unsorted[8].Param(k)
|
|
|
|
unsetValue, _ := unsorted[9].Param(k)
|
|
|
|
|
|
|
|
assert.Equal(t, 100, firstSetValue)
|
|
|
|
assert.Equal(t, 99, secondSetValue)
|
|
|
|
assert.Equal(t, 92, lastSetValue)
|
|
|
|
assert.Equal(t, nil, unsetValue)
|
|
|
|
|
|
|
|
sorted := unsorted.ByParam("arbitrarily.nested")
|
|
|
|
firstSetSortedValue, _ := sorted[0].Param(k)
|
|
|
|
secondSetSortedValue, _ := sorted[1].Param(k)
|
|
|
|
lastSetSortedValue, _ := sorted[8].Param(k)
|
|
|
|
unsetSortedValue, _ := sorted[9].Param(k)
|
|
|
|
|
|
|
|
assert.Equal(t, 92, firstSetSortedValue)
|
|
|
|
assert.Equal(t, 93, secondSetSortedValue)
|
|
|
|
assert.Equal(t, 100, lastSetSortedValue)
|
|
|
|
assert.Equal(t, unsetValue, unsetSortedValue)
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:01:56 -04:00
|
|
|
func BenchmarkSortByWeightAndReverse(b *testing.B) {
|
2017-02-04 22:20:06 -05:00
|
|
|
s := newTestSite(b)
|
|
|
|
p := createSortTestPages(s, 300)
|
2015-07-21 15:01:56 -04:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
p = p.ByWeight().Reverse()
|
|
|
|
}
|
2015-07-25 10:34:35 -04:00
|
|
|
}
|
|
|
|
|
2016-11-06 09:38:52 -05:00
|
|
|
func setSortVals(dates [4]time.Time, titles [4]string, weights [4]int, pages Pages) {
|
2015-07-25 10:34:35 -04:00
|
|
|
for i := range dates {
|
|
|
|
pages[i].Date = dates[i]
|
2016-04-22 14:43:00 -04:00
|
|
|
pages[i].Lastmod = dates[i]
|
2015-07-25 10:34:35 -04:00
|
|
|
pages[i].Weight = weights[i]
|
2018-01-15 14:40:39 -05:00
|
|
|
pages[i].title = titles[i]
|
2016-02-06 16:10:36 -05:00
|
|
|
// make sure we compare apples and ... apples ...
|
2018-01-15 14:40:39 -05:00
|
|
|
pages[len(dates)-1-i].linkTitle = pages[i].title + "l"
|
2016-02-06 16:10:36 -05:00
|
|
|
pages[len(dates)-1-i].PublishDate = dates[i]
|
2016-05-11 10:08:48 -04:00
|
|
|
pages[len(dates)-1-i].ExpiryDate = dates[i]
|
2018-04-23 23:57:33 -04:00
|
|
|
pages[len(dates)-1-i].workContent = []byte(titles[i] + "_content")
|
2015-07-25 10:34:35 -04:00
|
|
|
}
|
2016-04-22 14:43:00 -04:00
|
|
|
lastLastMod := pages[2].Lastmod
|
|
|
|
pages[2].Lastmod = pages[1].Lastmod
|
|
|
|
pages[1].Lastmod = lastLastMod
|
2018-04-23 23:57:33 -04:00
|
|
|
|
|
|
|
for _, p := range pages {
|
|
|
|
p.resetContent()
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:01:56 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func createSortTestPages(s *Site, num int) Pages {
|
2015-07-21 15:01:56 -04:00
|
|
|
pages := make(Pages, num)
|
|
|
|
|
|
|
|
for i := 0; i < num; i++ {
|
2017-02-04 22:20:06 -05:00
|
|
|
p := s.newPage(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))
|
2018-01-15 14:40:39 -05:00
|
|
|
p.params = map[string]interface{}{
|
2017-02-19 02:50:08 -05:00
|
|
|
"arbitrarily": map[string]interface{}{
|
|
|
|
"nested": ("xyz" + fmt.Sprintf("%v", 100-i)),
|
|
|
|
},
|
2015-07-21 15:01:56 -04:00
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2015-07-21 15:01:56 -04:00
|
|
|
w := 5
|
2016-11-06 09:38:52 -05:00
|
|
|
|
2015-07-21 15:01:56 -04:00
|
|
|
if i%2 == 0 {
|
|
|
|
w = 10
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
p.fuzzyWordCount = i
|
|
|
|
p.Weight = w
|
|
|
|
p.Description = "initial"
|
|
|
|
|
|
|
|
pages[i] = p
|
2015-07-21 15:01:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pages
|
|
|
|
}
|