github.com/pietrocarrara/hugo@v0.47.1/hugolib/pagesPrevNext_test.go (about)

     1  // Copyright 2015 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package hugolib
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/spf13/cast"
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  type pagePNTestObject struct {
    24  	path   string
    25  	weight int
    26  	date   string
    27  }
    28  
    29  var pagePNTestSources = []pagePNTestObject{
    30  	{"/section1/testpage1.md", 5, "2012-04-06"},
    31  	{"/section1/testpage2.md", 4, "2012-01-01"},
    32  	{"/section1/testpage3.md", 3, "2012-04-06"},
    33  	{"/section2/testpage4.md", 2, "2012-03-02"},
    34  	{"/section2/testpage5.md", 1, "2012-04-06"},
    35  }
    36  
    37  func TestPrev(t *testing.T) {
    38  	t.Parallel()
    39  	pages := preparePageGroupTestPages(t)
    40  	assert.Equal(t, pages.Prev(pages[0]), pages[4])
    41  	assert.Equal(t, pages.Prev(pages[1]), pages[0])
    42  	assert.Equal(t, pages.Prev(pages[4]), pages[3])
    43  }
    44  
    45  func TestNext(t *testing.T) {
    46  	t.Parallel()
    47  	pages := preparePageGroupTestPages(t)
    48  	assert.Equal(t, pages.Next(pages[0]), pages[1])
    49  	assert.Equal(t, pages.Next(pages[1]), pages[2])
    50  	assert.Equal(t, pages.Next(pages[4]), pages[0])
    51  }
    52  
    53  func prepareWeightedPagesPrevNext(t *testing.T) WeightedPages {
    54  	s := newTestSite(t)
    55  	w := WeightedPages{}
    56  
    57  	for _, src := range pagePNTestSources {
    58  		p, err := s.NewPage(src.path)
    59  		if err != nil {
    60  			t.Fatalf("failed to prepare test page %s", src.path)
    61  		}
    62  		p.Weight = src.weight
    63  		p.Date = cast.ToTime(src.date)
    64  		p.PublishDate = cast.ToTime(src.date)
    65  		w = append(w, WeightedPage{p.Weight, p})
    66  	}
    67  
    68  	w.Sort()
    69  	return w
    70  }
    71  
    72  func TestWeightedPagesPrev(t *testing.T) {
    73  	t.Parallel()
    74  	w := prepareWeightedPagesPrevNext(t)
    75  	assert.Equal(t, w.Prev(w[0].Page), w[4].Page)
    76  	assert.Equal(t, w.Prev(w[1].Page), w[0].Page)
    77  	assert.Equal(t, w.Prev(w[4].Page), w[3].Page)
    78  }
    79  
    80  func TestWeightedPagesNext(t *testing.T) {
    81  	t.Parallel()
    82  	w := prepareWeightedPagesPrevNext(t)
    83  	assert.Equal(t, w.Next(w[0].Page), w[1].Page)
    84  	assert.Equal(t, w.Next(w[1].Page), w[2].Page)
    85  	assert.Equal(t, w.Next(w[4].Page), w[0].Page)
    86  }