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"
|
2016-02-06 16:10:36 -05:00
|
|
|
"html/template"
|
2015-07-21 15:01:56 -04:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2015-07-25 10:34:35 -04:00
|
|
|
"time"
|
2016-04-22 14:43:00 -04:00
|
|
|
|
2016-10-24 07:45:30 -04:00
|
|
|
"github.com/spf13/hugo/helpers"
|
2016-04-22 14:43:00 -04:00
|
|
|
"github.com/spf13/hugo/source"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2015-07-21 15:01:56 -04:00
|
|
|
)
|
|
|
|
|
2015-07-25 11:22:19 -04:00
|
|
|
func TestDefaultSort(t *testing.T) {
|
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)
|
2015-07-25 10:34:35 -04:00
|
|
|
|
|
|
|
p := createSortTestPages(3)
|
|
|
|
|
|
|
|
// first by weight
|
2015-07-25 11:22:19 -04:00
|
|
|
setSortVals([3]time.Time{d1, d2, d3}, [3]string{"b", "a", "c"}, [3]int{3, 2, 1}, p)
|
2015-07-25 10:34:35 -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
|
|
|
|
setSortVals([3]time.Time{d3, d1, d2}, [3]string{"a", "b", "c"}, [3]int{1, 1, 1}, p)
|
|
|
|
p.Sort()
|
|
|
|
assert.Equal(t, d1, p[0].Date)
|
|
|
|
|
2016-02-06 16:10:36 -05:00
|
|
|
// finally by link title
|
2015-07-25 11:22:19 -04:00
|
|
|
setSortVals([3]time.Time{d3, d3, d3}, [3]string{"b", "c", "a"}, [3]int{1, 1, 1}, p)
|
2015-07-25 10:34:35 -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())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSortByN(t *testing.T) {
|
|
|
|
|
|
|
|
d1 := time.Now()
|
|
|
|
d2 := d1.Add(-2 * time.Hour)
|
|
|
|
d3 := d1.Add(-10 * time.Hour)
|
|
|
|
|
|
|
|
p := createSortTestPages(3)
|
|
|
|
|
|
|
|
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 }},
|
|
|
|
{(Pages).ByTitle, func(p Pages) bool { return p[0].Title == "ab" }},
|
|
|
|
{(Pages).ByLinkTitle, func(p Pages) bool { return p[0].LinkTitle() == "abl" }},
|
|
|
|
{(Pages).ByDate, func(p Pages) bool { return p[0].Date == d3 }},
|
|
|
|
{(Pages).ByPublishDate, func(p Pages) bool { return p[0].PublishDate == d3 }},
|
2016-05-11 10:08:48 -04:00
|
|
|
{(Pages).ByExpiryDate, func(p Pages) bool { return p[0].ExpiryDate == d3 }},
|
2016-04-22 14:43:00 -04:00
|
|
|
{(Pages).ByLastmod, func(p Pages) bool { return p[1].Lastmod == d2 }},
|
2016-02-06 16:10:36 -05:00
|
|
|
{(Pages).ByLength, func(p Pages) bool { return p[0].Content == "b_content" }},
|
|
|
|
} {
|
|
|
|
setSortVals([3]time.Time{d1, d2, d3}, [3]string{"b", "ab", "cde"}, [3]int{3, 2, 1}, p)
|
|
|
|
|
|
|
|
sorted := this.sortFunc(p)
|
|
|
|
if !this.assertFunc(sorted) {
|
|
|
|
t.Errorf("[%d] sort error", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLimit(t *testing.T) {
|
|
|
|
p := createSortTestPages(10)
|
|
|
|
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) {
|
2015-07-20 19:29:22 -04:00
|
|
|
p1 := createSortTestPages(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
|
|
|
|
assert.True(t, probablyEqualPages(p2, p1.Reverse()))
|
2015-07-21 15:01:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByWeightAndReverse(b *testing.B) {
|
|
|
|
|
|
|
|
p := createSortTestPages(300)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
p = p.ByWeight().Reverse()
|
|
|
|
}
|
2015-07-25 10:34:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func setSortVals(dates [3]time.Time, titles [3]string, weights [3]int, pages Pages) {
|
|
|
|
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]
|
|
|
|
pages[i].Title = titles[i]
|
2016-02-06 16:10:36 -05:00
|
|
|
// make sure we compare apples and ... apples ...
|
|
|
|
pages[len(dates)-1-i].linkTitle = pages[i].Title + "l"
|
|
|
|
pages[len(dates)-1-i].PublishDate = dates[i]
|
2016-05-11 10:08:48 -04:00
|
|
|
pages[len(dates)-1-i].ExpiryDate = dates[i]
|
2016-02-06 16:10:36 -05:00
|
|
|
pages[len(dates)-1-i].Content = template.HTML(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
|
2015-07-21 15:01:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func createSortTestPages(num int) Pages {
|
|
|
|
pages := make(Pages, num)
|
|
|
|
|
2016-10-24 07:45:30 -04:00
|
|
|
info := newSiteInfo(siteBuilderCfg{baseURL: "http://base", language: helpers.NewDefaultLanguage()})
|
|
|
|
|
2015-07-21 15:01:56 -04:00
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
pages[i] = &Page{
|
|
|
|
Node: Node{
|
|
|
|
URLPath: URLPath{
|
|
|
|
Section: "z",
|
|
|
|
URL: fmt.Sprintf("http://base/x/y/p%d.html", i),
|
|
|
|
},
|
2016-10-24 07:45:30 -04:00
|
|
|
Site: &info,
|
2015-07-21 15:01:56 -04:00
|
|
|
},
|
|
|
|
Source: Source{File: *source.NewFile(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))},
|
|
|
|
}
|
|
|
|
w := 5
|
|
|
|
if i%2 == 0 {
|
|
|
|
w = 10
|
|
|
|
}
|
2016-08-17 07:41:48 -04:00
|
|
|
pages[i].fuzzyWordCount = i
|
2015-07-21 15:01:56 -04:00
|
|
|
pages[i].Weight = w
|
2015-07-20 19:29:22 -04:00
|
|
|
pages[i].Description = "initial"
|
2015-07-21 15:01:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pages
|
|
|
|
}
|