mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Improve pagination testing
* Add missing pagination test cases * Remove some unreachable code paths * Fix some corner cases
This commit is contained in:
parent
0c2297075a
commit
de76d4a84e
2 changed files with 116 additions and 22 deletions
|
@ -88,19 +88,11 @@ func (p *pager) Next() *pager {
|
|||
|
||||
// First returns the pager for the first page.
|
||||
func (p *pager) First() *pager {
|
||||
if p.TotalPages() == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return p.pagers[0]
|
||||
}
|
||||
|
||||
// Last returns the pager for the last page.
|
||||
func (p *pager) Last() *pager {
|
||||
if p.TotalPages() == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return p.pagers[len(p.pagers)-1]
|
||||
}
|
||||
|
||||
|
@ -138,10 +130,6 @@ func splitPages(pages Pages, size int) []Pages {
|
|||
// If it's not, one will be created with all pages in Data["Pages"].
|
||||
func (n *Node) Paginator() (*pager, error) {
|
||||
|
||||
if n.IsPage() {
|
||||
return nil, errors.New("Paginators isn't supported for content pages.")
|
||||
}
|
||||
|
||||
var initError error
|
||||
|
||||
n.paginatorInit.Do(func() {
|
||||
|
@ -170,15 +158,18 @@ func (n *Node) Paginator() (*pager, error) {
|
|||
return n.paginator, nil
|
||||
}
|
||||
|
||||
func (p *Page) Paginator() (*pager, error) {
|
||||
return nil, errors.New("Paginators not supported for content pages.")
|
||||
}
|
||||
func (p *Page) Paginate(seq interface{}) (*pager, error) {
|
||||
return nil, errors.New("Paginators not supported for content pages.")
|
||||
}
|
||||
|
||||
// Paginate gets this Node's paginator if it's already created.
|
||||
// If it's not, one will be created with the qiven sequence.
|
||||
// Note that repeated calls will return the same result, even if the sequence is different.
|
||||
func (n *Node) Paginate(seq interface{}) (*pager, error) {
|
||||
|
||||
if n.IsPage() {
|
||||
return nil, errors.New("Paginators isn't supported for content pages.")
|
||||
}
|
||||
|
||||
var initError error
|
||||
|
||||
n.paginatorInit.Do(func() {
|
||||
|
@ -227,16 +218,16 @@ func paginatePages(seq interface{}, section string) (pagers, error) {
|
|||
}
|
||||
|
||||
urlFactory := newPaginationUrlFactory(section)
|
||||
paginator := newPaginator(pages, paginateSize, urlFactory)
|
||||
paginator, _ := newPaginator(pages, paginateSize, urlFactory)
|
||||
pagers := paginator.Pagers()
|
||||
|
||||
return pagers, nil
|
||||
}
|
||||
|
||||
func newPaginator(pages Pages, size int, urlFactory paginationUrlFactory) *paginator {
|
||||
func newPaginator(pages Pages, size int, urlFactory paginationUrlFactory) (*paginator, error) {
|
||||
|
||||
if size <= 0 {
|
||||
panic("Paginator size must be positive")
|
||||
return nil, errors.New("Paginator size must be positive")
|
||||
}
|
||||
|
||||
split := splitPages(pages, size)
|
||||
|
@ -250,7 +241,7 @@ func newPaginator(pages Pages, size int, urlFactory paginationUrlFactory) *pagin
|
|||
|
||||
p.pagers = pagers
|
||||
|
||||
return p
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func newPaginationUrlFactory(pathElements ...string) paginationUrlFactory {
|
||||
|
|
|
@ -24,14 +24,17 @@ func TestSplitPages(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func TestPaginator(t *testing.T) {
|
||||
func TestPager(t *testing.T) {
|
||||
|
||||
pages := createTestPages(21)
|
||||
urlFactory := func(page int) string {
|
||||
return fmt.Sprintf("page/%d/", page)
|
||||
}
|
||||
|
||||
paginator := newPaginator(pages, 5, urlFactory)
|
||||
_, err := newPaginator(pages, -1, urlFactory)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
paginator, _ := newPaginator(pages, 5, urlFactory)
|
||||
paginatorPages := paginator.Pagers()
|
||||
|
||||
assert.Equal(t, 5, len(paginatorPages))
|
||||
|
@ -43,22 +46,40 @@ func TestPaginator(t *testing.T) {
|
|||
assert.Equal(t, "page/1/", first.Url())
|
||||
assert.Equal(t, first, first.First())
|
||||
assert.Equal(t, true, first.HasNext())
|
||||
assert.Equal(t, paginatorPages[1], first.Next())
|
||||
assert.Equal(t, false, first.HasPrev())
|
||||
assert.Nil(t, first.Prev())
|
||||
assert.Equal(t, 5, first.NumberOfElements())
|
||||
assert.Equal(t, 1, first.PageNumber())
|
||||
|
||||
third := paginatorPages[2]
|
||||
assert.Equal(t, true, third.HasNext())
|
||||
assert.Equal(t, true, third.HasPrev())
|
||||
assert.Equal(t, paginatorPages[1], third.Prev())
|
||||
|
||||
last := paginatorPages[4]
|
||||
assert.Equal(t, "page/5/", last.Url())
|
||||
assert.Equal(t, last, last.Last())
|
||||
assert.Equal(t, false, last.HasNext())
|
||||
assert.Nil(t, last.Next())
|
||||
assert.Equal(t, true, last.HasPrev())
|
||||
assert.Equal(t, 1, last.NumberOfElements())
|
||||
assert.Equal(t, 5, last.PageNumber())
|
||||
}
|
||||
|
||||
func TestPagerNoPages(t *testing.T) {
|
||||
pages := createTestPages(0)
|
||||
urlFactory := func(page int) string {
|
||||
return fmt.Sprintf("page/%d/", page)
|
||||
}
|
||||
|
||||
paginator, _ := newPaginator(pages, 5, urlFactory)
|
||||
paginatorPages := paginator.Pagers()
|
||||
|
||||
assert.Equal(t, 0, len(paginatorPages))
|
||||
assert.Equal(t, 0, paginator.TotalNumberOfElements())
|
||||
assert.Equal(t, 5, paginator.PageSize())
|
||||
assert.Equal(t, 0, paginator.TotalPages())
|
||||
}
|
||||
|
||||
func TestPaginationUrlFactory(t *testing.T) {
|
||||
|
@ -73,6 +94,88 @@ func TestPaginationUrlFactory(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func TestPaginator(t *testing.T) {
|
||||
viper.Set("paginate", 5)
|
||||
pages := createTestPages(12)
|
||||
s := &Site{}
|
||||
n1 := s.newHomeNode()
|
||||
n2 := s.newHomeNode()
|
||||
n1.Data["Pages"] = pages
|
||||
|
||||
paginator1, err := n1.Paginator()
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, paginator1)
|
||||
assert.Equal(t, 3, paginator1.TotalPages())
|
||||
assert.Equal(t, 12, paginator1.TotalNumberOfElements())
|
||||
|
||||
n2.paginator = paginator1.Next()
|
||||
paginator2, err := n2.Paginator()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, paginator2, paginator1.Next())
|
||||
|
||||
n1.Data["Pages"] = createTestPages(1)
|
||||
samePaginator, _ := n1.Paginator()
|
||||
assert.Equal(t, paginator1, samePaginator)
|
||||
|
||||
p, _ := NewPage("test")
|
||||
_, err = p.Paginator()
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestPaginatorWithNegativePaginate(t *testing.T) {
|
||||
viper.Set("paginate", -1)
|
||||
s := &Site{}
|
||||
_, err := s.newHomeNode().Paginator()
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestPaginate(t *testing.T) {
|
||||
viper.Set("paginate", 5)
|
||||
pages := createTestPages(6)
|
||||
s := &Site{}
|
||||
n1 := s.newHomeNode()
|
||||
n2 := s.newHomeNode()
|
||||
|
||||
paginator1, err := n1.Paginate(pages)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, paginator1)
|
||||
assert.Equal(t, 2, paginator1.TotalPages())
|
||||
assert.Equal(t, 6, paginator1.TotalNumberOfElements())
|
||||
|
||||
n2.paginator = paginator1.Next()
|
||||
paginator2, err := n2.Paginate(pages)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, paginator2, paginator1.Next())
|
||||
|
||||
samePaginator, err := n1.Paginate(createTestPages(2))
|
||||
assert.Equal(t, paginator1, samePaginator)
|
||||
|
||||
p, _ := NewPage("test")
|
||||
_, err = p.Paginate(pages)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestPaginateWithNegativePaginate(t *testing.T) {
|
||||
viper.Set("paginate", -1)
|
||||
s := &Site{}
|
||||
_, err := s.newHomeNode().Paginate(createTestPages(2))
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestPaginatePages(t *testing.T) {
|
||||
viper.Set("paginate", 11)
|
||||
for i, seq := range []interface{}{createTestPages(11), WeightedPages{}, PageGroup{}, &Pages{}} {
|
||||
v, err := paginatePages(seq, "t")
|
||||
assert.NotNil(t, v, "Val %d", i)
|
||||
assert.Nil(t, err, "Err %d", i)
|
||||
}
|
||||
_, err := paginatePages(Site{}, "t")
|
||||
assert.NotNil(t, err)
|
||||
|
||||
}
|
||||
|
||||
func createTestPages(num int) Pages {
|
||||
pages := make(Pages, num)
|
||||
|
||||
|
|
Loading…
Reference in a new issue