github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/resources/page/pages_prev_next_test.go (about)

     1  // Copyright 2019 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 page
    15  
    16  import (
    17  	"testing"
    18  
    19  	qt "github.com/frankban/quicktest"
    20  	"github.com/spf13/cast"
    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  	c := qt.New(t)
    40  	pages := preparePageGroupTestPages(t)
    41  
    42  	c.Assert(pages.Prev(pages[3]), qt.Equals, pages[4])
    43  	c.Assert(pages.Prev(pages[1]), qt.Equals, pages[2])
    44  	c.Assert(pages.Prev(pages[4]), qt.IsNil)
    45  }
    46  
    47  func TestNext(t *testing.T) {
    48  	t.Parallel()
    49  	c := qt.New(t)
    50  	pages := preparePageGroupTestPages(t)
    51  
    52  	c.Assert(pages.Next(pages[0]), qt.IsNil)
    53  	c.Assert(pages.Next(pages[1]), qt.Equals, pages[0])
    54  	c.Assert(pages.Next(pages[4]), qt.Equals, pages[3])
    55  }
    56  
    57  func prepareWeightedPagesPrevNext(t *testing.T) WeightedPages {
    58  	w := WeightedPages{}
    59  
    60  	for _, src := range pagePNTestSources {
    61  		p := newTestPage()
    62  		p.path = src.path
    63  		p.weight = src.weight
    64  		p.date = cast.ToTime(src.date)
    65  		p.pubDate = cast.ToTime(src.date)
    66  		w = append(w, WeightedPage{Weight: p.weight, Page: p})
    67  	}
    68  
    69  	w.Sort()
    70  	return w
    71  }
    72  
    73  func TestWeightedPagesPrev(t *testing.T) {
    74  	t.Parallel()
    75  	c := qt.New(t)
    76  	w := prepareWeightedPagesPrevNext(t)
    77  
    78  	c.Assert(w.Prev(w[0].Page), qt.Equals, w[1].Page)
    79  	c.Assert(w.Prev(w[1].Page), qt.Equals, w[2].Page)
    80  	c.Assert(w.Prev(w[4].Page), qt.IsNil)
    81  }
    82  
    83  func TestWeightedPagesNext(t *testing.T) {
    84  	t.Parallel()
    85  	c := qt.New(t)
    86  	w := prepareWeightedPagesPrevNext(t)
    87  
    88  	c.Assert(w.Next(w[0].Page), qt.IsNil)
    89  	c.Assert(w.Next(w[1].Page), qt.Equals, w[0].Page)
    90  	c.Assert(w.Next(w[4].Page), qt.Equals, w[3].Page)
    91  }