shithub: hugo

Download patch

ref: 88a320fb2d1474f29cc73dfb992967eda004ec6b
parent: b21d280c675242c42245c1a7a5e00fb1610904e5
author: Bjørn Erik Pedersen <[email protected]>
date: Sat Feb 6 17:10:36 EST 2016

Fix buggy Pages.Limit

Also add missing page sort related tests.

--- a/hugolib/pageSort.go
+++ b/hugolib/pageSort.go
@@ -64,7 +64,7 @@
 }
 
 func (p Pages) Limit(n int) Pages {
-	if len(p) < n {
+	if len(p) > n {
 		return p[0:n]
 	}
 	return p
--- a/hugolib/pageSort_test.go
+++ b/hugolib/pageSort_test.go
@@ -15,12 +15,12 @@
 
 import (
 	"fmt"
+	"github.com/spf13/hugo/source"
 	"github.com/stretchr/testify/assert"
+	"html/template"
 	"path/filepath"
 	"testing"
 	"time"
-
-	"github.com/spf13/hugo/source"
 )
 
 func TestDefaultSort(t *testing.T) {
@@ -34,6 +34,7 @@
 	// first by weight
 	setSortVals([3]time.Time{d1, d2, d3}, [3]string{"b", "a", "c"}, [3]int{3, 2, 1}, p)
 	p.Sort()
+
 	assert.Equal(t, 1, p[0].Weight)
 
 	// next by date
@@ -41,14 +42,55 @@
 	p.Sort()
 	assert.Equal(t, d1, p[0].Date)
 
-	// finally by title
+	// finally by link title
 	setSortVals([3]time.Time{d3, d3, d3}, [3]string{"b", "c", "a"}, [3]int{1, 1, 1}, p)
 	p.Sort()
-	assert.Equal(t, "a", p[0].Title)
-	assert.Equal(t, "b", p[1].Title)
-	assert.Equal(t, "c", p[2].Title)
+	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 }},
+		{(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)
+
+		fmt.Printf("Pages %v", 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))
+}
+
 func TestPageSortReverse(t *testing.T) {
 	p1 := createSortTestPages(10)
 	assert.Equal(t, 0, p1[0].FuzzyWordCount)
@@ -75,6 +117,10 @@
 		pages[i].Date = dates[i]
 		pages[i].Weight = weights[i]
 		pages[i].Title = titles[i]
+		// 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]
+		pages[len(dates)-1-i].Content = template.HTML(titles[i] + "_content")
 	}
 
 }