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.
|
|
|
|
|
2014-12-27 08:11:19 -05:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-22 12:59:46 -04:00
|
|
|
"html/template"
|
2014-12-27 08:11:19 -05:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2016-10-24 07:45:30 -04:00
|
|
|
"github.com/spf13/hugo/helpers"
|
2015-05-20 02:21:21 -04:00
|
|
|
"github.com/spf13/hugo/source"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2014-12-27 08:11:19 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSplitPages(t *testing.T) {
|
|
|
|
|
|
|
|
pages := createTestPages(21)
|
|
|
|
chunks := splitPages(pages, 5)
|
|
|
|
assert.Equal(t, 5, len(chunks))
|
|
|
|
|
|
|
|
for i := 0; i < 4; i++ {
|
2015-07-16 07:17:54 -04:00
|
|
|
assert.Equal(t, 5, chunks[i].Len())
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
lastChunk := chunks[4]
|
2015-07-16 07:17:54 -04:00
|
|
|
assert.Equal(t, 1, lastChunk.Len())
|
2014-12-27 08:11:19 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
func TestSplitPageGroups(t *testing.T) {
|
|
|
|
|
|
|
|
pages := createTestPages(21)
|
|
|
|
groups, _ := pages.GroupBy("Weight", "desc")
|
|
|
|
chunks := splitPageGroups(groups, 5)
|
|
|
|
assert.Equal(t, 5, len(chunks))
|
|
|
|
|
|
|
|
firstChunk := chunks[0]
|
|
|
|
|
|
|
|
// alternate weight 5 and 10
|
|
|
|
if groups, ok := firstChunk.(PagesGroup); ok {
|
|
|
|
assert.Equal(t, 5, groups.Len())
|
|
|
|
for _, pg := range groups {
|
|
|
|
// first group 10 in weight
|
|
|
|
assert.Equal(t, 10, pg.Key)
|
|
|
|
for _, p := range pg.Pages {
|
2016-08-17 07:41:48 -04:00
|
|
|
assert.True(t, p.fuzzyWordCount%2 == 0) // magic test
|
2015-07-16 07:17:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatal("Excepted PageGroup")
|
|
|
|
}
|
|
|
|
|
|
|
|
lastChunk := chunks[4]
|
|
|
|
|
|
|
|
if groups, ok := lastChunk.(PagesGroup); ok {
|
|
|
|
assert.Equal(t, 1, groups.Len())
|
|
|
|
for _, pg := range groups {
|
|
|
|
// last should have 5 in weight
|
|
|
|
assert.Equal(t, 5, pg.Key)
|
|
|
|
for _, p := range pg.Pages {
|
2016-08-17 07:41:48 -04:00
|
|
|
assert.True(t, p.fuzzyWordCount%2 != 0) // magic test
|
2015-07-16 07:17:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatal("Excepted PageGroup")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-12-27 08:11:19 -05:00
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
func TestPager(t *testing.T) {
|
2014-12-27 08:11:19 -05:00
|
|
|
pages := createTestPages(21)
|
2015-07-16 07:17:54 -04:00
|
|
|
groups, _ := pages.GroupBy("Weight", "desc")
|
|
|
|
|
2014-12-27 08:11:19 -05:00
|
|
|
urlFactory := func(page int) string {
|
|
|
|
return fmt.Sprintf("page/%d/", page)
|
|
|
|
}
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
_, err := newPaginatorFromPages(pages, -1, urlFactory)
|
2015-01-26 19:27:07 -05:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
_, err = newPaginatorFromPageGroups(groups, -1, urlFactory)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
pag, err := newPaginatorFromPages(pages, 5, urlFactory)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
doTestPages(t, pag)
|
|
|
|
first := pag.Pagers()[0].First()
|
2016-11-23 04:44:35 -05:00
|
|
|
assert.Equal(t, "Pager 1", first.String())
|
2015-07-16 07:17:54 -04:00
|
|
|
assert.NotEmpty(t, first.Pages())
|
|
|
|
assert.Empty(t, first.PageGroups())
|
|
|
|
|
|
|
|
pag, err = newPaginatorFromPageGroups(groups, 5, urlFactory)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
doTestPages(t, pag)
|
|
|
|
first = pag.Pagers()[0].First()
|
|
|
|
assert.NotEmpty(t, first.PageGroups())
|
|
|
|
assert.Empty(t, first.Pages())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestPages(t *testing.T, paginator *paginator) {
|
|
|
|
|
2014-12-27 08:11:19 -05:00
|
|
|
paginatorPages := paginator.Pagers()
|
|
|
|
|
|
|
|
assert.Equal(t, 5, len(paginatorPages))
|
|
|
|
assert.Equal(t, 21, paginator.TotalNumberOfElements())
|
|
|
|
assert.Equal(t, 5, paginator.PageSize())
|
|
|
|
assert.Equal(t, 5, paginator.TotalPages())
|
|
|
|
|
|
|
|
first := paginatorPages[0]
|
2015-04-22 12:59:46 -04:00
|
|
|
assert.Equal(t, template.HTML("page/1/"), first.URL())
|
2014-12-27 08:11:19 -05:00
|
|
|
assert.Equal(t, first, first.First())
|
2015-01-27 13:47:22 -05:00
|
|
|
assert.True(t, first.HasNext())
|
2015-01-26 19:27:07 -05:00
|
|
|
assert.Equal(t, paginatorPages[1], first.Next())
|
2015-01-27 13:47:22 -05:00
|
|
|
assert.False(t, first.HasPrev())
|
2015-01-26 19:27:07 -05:00
|
|
|
assert.Nil(t, first.Prev())
|
2014-12-27 08:11:19 -05:00
|
|
|
assert.Equal(t, 5, first.NumberOfElements())
|
|
|
|
assert.Equal(t, 1, first.PageNumber())
|
|
|
|
|
|
|
|
third := paginatorPages[2]
|
2015-01-27 13:47:22 -05:00
|
|
|
assert.True(t, third.HasNext())
|
|
|
|
assert.True(t, third.HasPrev())
|
2015-01-26 19:27:07 -05:00
|
|
|
assert.Equal(t, paginatorPages[1], third.Prev())
|
2014-12-27 08:11:19 -05:00
|
|
|
|
|
|
|
last := paginatorPages[4]
|
2015-04-22 12:59:46 -04:00
|
|
|
assert.Equal(t, template.HTML("page/5/"), last.URL())
|
2014-12-27 08:11:19 -05:00
|
|
|
assert.Equal(t, last, last.Last())
|
2015-01-27 13:47:22 -05:00
|
|
|
assert.False(t, last.HasNext())
|
2015-01-26 19:27:07 -05:00
|
|
|
assert.Nil(t, last.Next())
|
2015-01-27 13:47:22 -05:00
|
|
|
assert.True(t, last.HasPrev())
|
2014-12-27 08:11:19 -05:00
|
|
|
assert.Equal(t, 1, last.NumberOfElements())
|
|
|
|
assert.Equal(t, 5, last.PageNumber())
|
2015-01-26 19:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPagerNoPages(t *testing.T) {
|
|
|
|
pages := createTestPages(0)
|
2015-07-16 07:17:54 -04:00
|
|
|
groups, _ := pages.GroupBy("Weight", "desc")
|
|
|
|
|
2015-01-26 19:27:07 -05:00
|
|
|
urlFactory := func(page int) string {
|
|
|
|
return fmt.Sprintf("page/%d/", page)
|
|
|
|
}
|
2014-12-27 08:11:19 -05:00
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
paginator, _ := newPaginatorFromPages(pages, 5, urlFactory)
|
|
|
|
doTestPagerNoPages(t, paginator)
|
|
|
|
|
|
|
|
first := paginator.Pagers()[0].First()
|
|
|
|
assert.Empty(t, first.PageGroups())
|
|
|
|
assert.Empty(t, first.Pages())
|
|
|
|
|
|
|
|
paginator, _ = newPaginatorFromPageGroups(groups, 5, urlFactory)
|
|
|
|
doTestPagerNoPages(t, paginator)
|
|
|
|
|
|
|
|
first = paginator.Pagers()[0].First()
|
|
|
|
assert.Empty(t, first.PageGroups())
|
|
|
|
assert.Empty(t, first.Pages())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestPagerNoPages(t *testing.T, paginator *paginator) {
|
2015-01-26 19:27:07 -05:00
|
|
|
paginatorPages := paginator.Pagers()
|
|
|
|
|
2015-03-05 18:30:51 -05:00
|
|
|
assert.Equal(t, 1, len(paginatorPages))
|
2015-01-26 19:27:07 -05:00
|
|
|
assert.Equal(t, 0, paginator.TotalNumberOfElements())
|
|
|
|
assert.Equal(t, 5, paginator.PageSize())
|
|
|
|
assert.Equal(t, 0, paginator.TotalPages())
|
2015-03-05 18:30:51 -05:00
|
|
|
|
|
|
|
// pageOne should be nothing but the first
|
|
|
|
pageOne := paginatorPages[0]
|
|
|
|
assert.NotNil(t, pageOne.First())
|
|
|
|
assert.False(t, pageOne.HasNext())
|
|
|
|
assert.False(t, pageOne.HasPrev())
|
|
|
|
assert.Nil(t, pageOne.Next())
|
|
|
|
assert.Equal(t, 1, len(pageOne.Pagers()))
|
2015-07-16 07:17:54 -04:00
|
|
|
assert.Equal(t, 0, pageOne.Pages().Len())
|
2015-03-05 18:30:51 -05:00
|
|
|
assert.Equal(t, 0, pageOne.NumberOfElements())
|
|
|
|
assert.Equal(t, 0, pageOne.TotalNumberOfElements())
|
|
|
|
assert.Equal(t, 0, pageOne.TotalPages())
|
|
|
|
assert.Equal(t, 1, pageOne.PageNumber())
|
|
|
|
assert.Equal(t, 5, pageOne.PageSize())
|
|
|
|
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
func TestPaginationURLFactory(t *testing.T) {
|
2016-07-28 03:30:58 -04:00
|
|
|
testCommonResetState()
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2016-10-24 14:56:00 -04:00
|
|
|
viper.Set("paginatePath", "zoo")
|
2015-03-11 13:34:57 -04:00
|
|
|
unicode := newPaginationURLFactory("новости проекта")
|
|
|
|
fooBar := newPaginationURLFactory("foo", "bar")
|
2014-12-27 08:11:19 -05:00
|
|
|
|
|
|
|
assert.Equal(t, "/foo/bar/", fooBar(1))
|
|
|
|
assert.Equal(t, "/%D0%BD%D0%BE%D0%B2%D0%BE%D1%81%D1%82%D0%B8-%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B0/zoo/4/", unicode(4))
|
|
|
|
assert.Equal(t, "/foo/bar/zoo/12345/", fooBar(12345))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:27:07 -05:00
|
|
|
func TestPaginator(t *testing.T) {
|
2016-07-28 03:30:58 -04:00
|
|
|
testCommonResetState()
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2015-03-31 13:12:54 -04:00
|
|
|
for _, useViper := range []bool{false, true} {
|
|
|
|
doTestPaginator(t, useViper)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestPaginator(t *testing.T, useViper bool) {
|
2016-07-28 03:30:58 -04:00
|
|
|
testCommonResetState()
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2015-03-31 13:12:54 -04:00
|
|
|
pagerSize := 5
|
|
|
|
if useViper {
|
|
|
|
viper.Set("paginate", pagerSize)
|
|
|
|
} else {
|
|
|
|
viper.Set("paginate", -1)
|
|
|
|
}
|
2015-01-26 19:27:07 -05:00
|
|
|
pages := createTestPages(12)
|
2017-01-03 11:28:51 -05:00
|
|
|
s := NewSiteDefaultLang()
|
2016-11-07 14:24:37 -05:00
|
|
|
n1 := s.newHomePage()
|
|
|
|
n2 := s.newHomePage()
|
2015-01-26 19:27:07 -05:00
|
|
|
n1.Data["Pages"] = pages
|
|
|
|
|
2015-04-21 15:25:42 -04:00
|
|
|
var paginator1 *Pager
|
2015-03-31 13:12:54 -04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if useViper {
|
|
|
|
paginator1, err = n1.Paginator()
|
|
|
|
} else {
|
|
|
|
paginator1, err = n1.Paginator(pagerSize)
|
|
|
|
}
|
2015-01-26 19:27:07 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-01-03 11:28:51 -05:00
|
|
|
p, _ := pageTestSite.NewPage("test")
|
2015-01-26 19:27:07 -05:00
|
|
|
_, err = p.Paginator()
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPaginatorWithNegativePaginate(t *testing.T) {
|
2016-07-28 03:30:58 -04:00
|
|
|
testCommonResetState()
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2015-01-26 19:27:07 -05:00
|
|
|
viper.Set("paginate", -1)
|
2017-01-03 11:28:51 -05:00
|
|
|
s := NewSiteDefaultLang()
|
2016-11-07 14:24:37 -05:00
|
|
|
_, err := s.newHomePage().Paginator()
|
2015-01-26 19:27:07 -05:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPaginate(t *testing.T) {
|
2016-07-28 03:30:58 -04:00
|
|
|
testCommonResetState()
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2015-03-31 13:12:54 -04:00
|
|
|
for _, useViper := range []bool{false, true} {
|
|
|
|
doTestPaginate(t, useViper)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 10:54:05 -05:00
|
|
|
func TestPaginatorURL(t *testing.T) {
|
|
|
|
|
|
|
|
testCommonResetState()
|
|
|
|
viper.Set("paginate", 2)
|
|
|
|
viper.Set("paginatePath", "testing")
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
// Issue #2177, do not double encode URLs
|
|
|
|
writeSource(t, filepath.Join("content", "阅读", fmt.Sprintf("page%d.md", (i+1))),
|
|
|
|
fmt.Sprintf(`---
|
|
|
|
title: Page%d
|
|
|
|
---
|
|
|
|
Conten%d
|
|
|
|
`, (i+1), i+1))
|
|
|
|
|
|
|
|
}
|
|
|
|
writeSource(t, filepath.Join("layouts", "_default", "single.html"), "<html><body>{{.Content}}</body></html>")
|
|
|
|
writeSource(t, filepath.Join("layouts", "_default", "list.html"),
|
|
|
|
`
|
|
|
|
<html><body>
|
|
|
|
Count: {{ .Paginator.TotalNumberOfElements }}
|
|
|
|
Pages: {{ .Paginator.TotalPages }}
|
|
|
|
{{ range .Paginator.Pagers -}}
|
|
|
|
{{ .PageNumber }}: {{ .URL }}
|
|
|
|
{{ end }}
|
|
|
|
</body></html>`)
|
|
|
|
|
|
|
|
if err := buildAndRenderSite(NewSiteDefaultLang()); err != nil {
|
|
|
|
t.Fatalf("Failed to build site: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertFileContent(t, filepath.Join("public", "阅读", "testing", "2", "index.html"), false, "2: /%E9%98%85%E8%AF%BB/testing/2/")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-31 13:12:54 -04:00
|
|
|
func doTestPaginate(t *testing.T, useViper bool) {
|
|
|
|
pagerSize := 5
|
|
|
|
if useViper {
|
|
|
|
viper.Set("paginate", pagerSize)
|
|
|
|
} else {
|
|
|
|
viper.Set("paginate", -1)
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:27:07 -05:00
|
|
|
pages := createTestPages(6)
|
2017-01-03 11:28:51 -05:00
|
|
|
s := NewSiteDefaultLang()
|
2016-11-07 14:24:37 -05:00
|
|
|
n1 := s.newHomePage()
|
|
|
|
n2 := s.newHomePage()
|
2015-01-26 19:27:07 -05:00
|
|
|
|
2015-04-21 15:25:42 -04:00
|
|
|
var paginator1, paginator2 *Pager
|
2015-03-31 13:12:54 -04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if useViper {
|
|
|
|
paginator1, err = n1.Paginate(pages)
|
|
|
|
} else {
|
|
|
|
paginator1, err = n1.Paginate(pages, pagerSize)
|
|
|
|
}
|
2015-01-26 19:27:07 -05:00
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, paginator1)
|
|
|
|
assert.Equal(t, 2, paginator1.TotalPages())
|
|
|
|
assert.Equal(t, 6, paginator1.TotalNumberOfElements())
|
|
|
|
|
|
|
|
n2.paginator = paginator1.Next()
|
2015-03-31 16:33:24 -04:00
|
|
|
if useViper {
|
|
|
|
paginator2, err = n2.Paginate(pages)
|
|
|
|
} else {
|
|
|
|
paginator2, err = n2.Paginate(pages, pagerSize)
|
|
|
|
}
|
2015-01-26 19:27:07 -05:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, paginator2, paginator1.Next())
|
|
|
|
|
2017-01-03 11:28:51 -05:00
|
|
|
p, _ := pageTestSite.NewPage("test")
|
2015-01-26 19:27:07 -05:00
|
|
|
_, err = p.Paginate(pages)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
2015-03-31 13:12:54 -04:00
|
|
|
func TestInvalidOptions(t *testing.T) {
|
2017-01-03 11:28:51 -05:00
|
|
|
s := NewSiteDefaultLang()
|
2016-11-07 14:24:37 -05:00
|
|
|
n1 := s.newHomePage()
|
2015-03-31 13:12:54 -04:00
|
|
|
_, err := n1.Paginate(createTestPages(1), 1, 2)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
_, err = n1.Paginator(1, 2)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
_, err = n1.Paginator(-1)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:27:07 -05:00
|
|
|
func TestPaginateWithNegativePaginate(t *testing.T) {
|
2016-07-28 03:30:58 -04:00
|
|
|
testCommonResetState()
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2015-01-26 19:27:07 -05:00
|
|
|
viper.Set("paginate", -1)
|
2017-01-03 11:28:51 -05:00
|
|
|
s := NewSiteDefaultLang()
|
2016-11-07 14:24:37 -05:00
|
|
|
_, err := s.newHomePage().Paginate(createTestPages(2))
|
2015-01-26 19:27:07 -05:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPaginatePages(t *testing.T) {
|
2015-07-16 07:17:54 -04:00
|
|
|
groups, _ := createTestPages(31).GroupBy("Weight", "desc")
|
|
|
|
for i, seq := range []interface{}{createTestPages(11), groups, WeightedPages{}, PageGroup{}, &Pages{}} {
|
2015-03-31 13:12:54 -04:00
|
|
|
v, err := paginatePages(seq, 11, "t")
|
2015-01-26 19:27:07 -05:00
|
|
|
assert.NotNil(t, v, "Val %d", i)
|
|
|
|
assert.Nil(t, err, "Err %d", i)
|
|
|
|
}
|
2015-03-31 13:12:54 -04:00
|
|
|
_, err := paginatePages(Site{}, 11, "t")
|
2015-01-26 19:27:07 -05:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-31 16:33:24 -04:00
|
|
|
// Issue #993
|
|
|
|
func TestPaginatorFollowedByPaginateShouldFail(t *testing.T) {
|
2016-07-28 03:30:58 -04:00
|
|
|
testCommonResetState()
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2015-03-31 16:33:24 -04:00
|
|
|
viper.Set("paginate", 10)
|
2017-01-03 11:28:51 -05:00
|
|
|
s := NewSiteDefaultLang()
|
2016-11-07 14:24:37 -05:00
|
|
|
n1 := s.newHomePage()
|
|
|
|
n2 := s.newHomePage()
|
2015-03-31 16:33:24 -04:00
|
|
|
|
|
|
|
_, err := n1.Paginator()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
_, err = n1.Paginate(createTestPages(2))
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
_, err = n2.Paginate(createTestPages(2))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPaginateFollowedByDifferentPaginateShouldFail(t *testing.T) {
|
2016-07-28 03:30:58 -04:00
|
|
|
testCommonResetState()
|
2015-03-31 16:33:24 -04:00
|
|
|
|
|
|
|
viper.Set("paginate", 10)
|
2017-01-03 11:28:51 -05:00
|
|
|
s := NewSiteDefaultLang()
|
2016-11-07 14:24:37 -05:00
|
|
|
n1 := s.newHomePage()
|
|
|
|
n2 := s.newHomePage()
|
2015-03-31 16:33:24 -04:00
|
|
|
|
|
|
|
p1 := createTestPages(2)
|
|
|
|
p2 := createTestPages(10)
|
|
|
|
|
|
|
|
_, err := n1.Paginate(p1)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
_, err = n1.Paginate(p1)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
_, err = n1.Paginate(p2)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
_, err = n2.Paginate(p2)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProbablyEqualPageLists(t *testing.T) {
|
|
|
|
fivePages := createTestPages(5)
|
|
|
|
zeroPages := createTestPages(0)
|
2015-07-16 07:17:54 -04:00
|
|
|
zeroPagesByWeight, _ := createTestPages(0).GroupBy("Weight", "asc")
|
|
|
|
fivePagesByWeight, _ := createTestPages(5).GroupBy("Weight", "asc")
|
|
|
|
ninePagesByWeight, _ := createTestPages(9).GroupBy("Weight", "asc")
|
|
|
|
|
2015-03-31 16:33:24 -04:00
|
|
|
for i, this := range []struct {
|
|
|
|
v1 interface{}
|
|
|
|
v2 interface{}
|
|
|
|
expect bool
|
|
|
|
}{
|
|
|
|
{nil, nil, true},
|
|
|
|
{"a", "b", true},
|
|
|
|
{"a", fivePages, false},
|
|
|
|
{fivePages, "a", false},
|
|
|
|
{fivePages, createTestPages(2), false},
|
|
|
|
{fivePages, fivePages, true},
|
|
|
|
{zeroPages, zeroPages, true},
|
2015-07-16 07:17:54 -04:00
|
|
|
{fivePagesByWeight, fivePagesByWeight, true},
|
|
|
|
{zeroPagesByWeight, fivePagesByWeight, false},
|
|
|
|
{zeroPagesByWeight, zeroPagesByWeight, true},
|
|
|
|
{fivePagesByWeight, fivePages, false},
|
|
|
|
{fivePagesByWeight, ninePagesByWeight, false},
|
2015-03-31 16:33:24 -04:00
|
|
|
} {
|
|
|
|
result := probablyEqualPageLists(this.v1, this.v2)
|
|
|
|
|
|
|
|
if result != this.expect {
|
2015-04-05 15:03:16 -04:00
|
|
|
t.Errorf("[%d] got %t but expected %t", i, result, this.expect)
|
2015-03-31 16:33:24 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
func TestPage(t *testing.T) {
|
|
|
|
urlFactory := func(page int) string {
|
|
|
|
return fmt.Sprintf("page/%d/", page)
|
|
|
|
}
|
|
|
|
|
|
|
|
fivePages := createTestPages(7)
|
|
|
|
fivePagesFuzzyWordCount, _ := createTestPages(7).GroupBy("FuzzyWordCount", "asc")
|
|
|
|
|
|
|
|
p1, _ := newPaginatorFromPages(fivePages, 2, urlFactory)
|
|
|
|
p2, _ := newPaginatorFromPageGroups(fivePagesFuzzyWordCount, 2, urlFactory)
|
|
|
|
|
|
|
|
f1 := p1.pagers[0].First()
|
|
|
|
f2 := p2.pagers[0].First()
|
|
|
|
|
|
|
|
page11, _ := f1.page(1)
|
|
|
|
page1Nil, _ := f1.page(3)
|
|
|
|
|
|
|
|
page21, _ := f2.page(1)
|
|
|
|
page2Nil, _ := f2.page(3)
|
|
|
|
|
2016-08-17 07:41:48 -04:00
|
|
|
assert.Equal(t, 3, page11.fuzzyWordCount)
|
2015-07-16 07:17:54 -04:00
|
|
|
assert.Nil(t, page1Nil)
|
|
|
|
|
2016-08-17 07:41:48 -04:00
|
|
|
assert.Equal(t, 3, page21.fuzzyWordCount)
|
2015-07-16 07:17:54 -04:00
|
|
|
assert.Nil(t, page2Nil)
|
|
|
|
}
|
|
|
|
|
2014-12-27 08:11:19 -05:00
|
|
|
func createTestPages(num int) Pages {
|
|
|
|
pages := make(Pages, num)
|
|
|
|
|
2016-10-24 07:45:30 -04:00
|
|
|
info := newSiteInfo(siteBuilderCfg{baseURL: "http://base/", language: helpers.NewDefaultLanguage()})
|
2014-12-27 08:11:19 -05:00
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
pages[i] = &Page{
|
2016-11-16 14:32:38 -05:00
|
|
|
pageInit: &pageInit{},
|
2016-11-13 08:27:10 -05:00
|
|
|
URLPath: URLPath{
|
|
|
|
Section: "z",
|
|
|
|
URL: fmt.Sprintf("http://base/x/y/p%d.html", i),
|
2014-12-27 08:11:19 -05:00
|
|
|
},
|
2016-11-13 08:27:10 -05:00
|
|
|
Site: &info,
|
2015-07-16 07:17:54 -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
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
2016-08-17 07:41:48 -04:00
|
|
|
pages[i].fuzzyWordCount = i + 2
|
2015-07-16 07:17:54 -04:00
|
|
|
pages[i].Weight = w
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return pages
|
|
|
|
}
|