github.com/olliephillips/hugo@v0.42.2/hugolib/pageSort_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  	"fmt"
    18  	"path/filepath"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestDefaultSort(t *testing.T) {
    26  	t.Parallel()
    27  	d1 := time.Now()
    28  	d2 := d1.Add(-1 * time.Hour)
    29  	d3 := d1.Add(-2 * time.Hour)
    30  	d4 := d1.Add(-3 * time.Hour)
    31  
    32  	s := newTestSite(t)
    33  
    34  	p := createSortTestPages(s, 4)
    35  
    36  	// first by weight
    37  	setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "a", "c", "d"}, [4]int{4, 3, 2, 1}, p)
    38  	p.Sort()
    39  
    40  	assert.Equal(t, 1, p[0].Weight)
    41  
    42  	// Consider zero weight, issue #2673
    43  	setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "a", "d", "c"}, [4]int{0, 0, 0, 1}, p)
    44  	p.Sort()
    45  
    46  	assert.Equal(t, 1, p[0].Weight)
    47  
    48  	// next by date
    49  	setSortVals([4]time.Time{d3, d4, d1, d2}, [4]string{"a", "b", "c", "d"}, [4]int{1, 1, 1, 1}, p)
    50  	p.Sort()
    51  	assert.Equal(t, d1, p[0].Date)
    52  
    53  	// finally by link title
    54  	setSortVals([4]time.Time{d3, d3, d3, d3}, [4]string{"b", "c", "a", "d"}, [4]int{1, 1, 1, 1}, p)
    55  	p.Sort()
    56  	assert.Equal(t, "al", p[0].LinkTitle())
    57  	assert.Equal(t, "bl", p[1].LinkTitle())
    58  	assert.Equal(t, "cl", p[2].LinkTitle())
    59  }
    60  
    61  func TestSortByN(t *testing.T) {
    62  	t.Parallel()
    63  	s := newTestSite(t)
    64  	d1 := time.Now()
    65  	d2 := d1.Add(-2 * time.Hour)
    66  	d3 := d1.Add(-10 * time.Hour)
    67  	d4 := d1.Add(-20 * time.Hour)
    68  
    69  	p := createSortTestPages(s, 4)
    70  
    71  	for i, this := range []struct {
    72  		sortFunc   func(p Pages) Pages
    73  		assertFunc func(p Pages) bool
    74  	}{
    75  		{(Pages).ByWeight, func(p Pages) bool { return p[0].Weight == 1 }},
    76  		{(Pages).ByTitle, func(p Pages) bool { return p[0].title == "ab" }},
    77  		{(Pages).ByLinkTitle, func(p Pages) bool { return p[0].LinkTitle() == "abl" }},
    78  		{(Pages).ByDate, func(p Pages) bool { return p[0].Date == d4 }},
    79  		{(Pages).ByPublishDate, func(p Pages) bool { return p[0].PublishDate == d4 }},
    80  		{(Pages).ByExpiryDate, func(p Pages) bool { return p[0].ExpiryDate == d4 }},
    81  		{(Pages).ByLastmod, func(p Pages) bool { return p[1].Lastmod == d3 }},
    82  		{(Pages).ByLength, func(p Pages) bool { return p[0].content() == "b_content" }},
    83  	} {
    84  		setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "ab", "cde", "fg"}, [4]int{0, 3, 2, 1}, p)
    85  
    86  		sorted := this.sortFunc(p)
    87  		if !this.assertFunc(sorted) {
    88  			t.Errorf("[%d] sort error", i)
    89  		}
    90  	}
    91  
    92  }
    93  
    94  func TestLimit(t *testing.T) {
    95  	t.Parallel()
    96  	s := newTestSite(t)
    97  	p := createSortTestPages(s, 10)
    98  	firstFive := p.Limit(5)
    99  	assert.Equal(t, 5, len(firstFive))
   100  	for i := 0; i < 5; i++ {
   101  		assert.Equal(t, p[i], firstFive[i])
   102  	}
   103  	assert.Equal(t, p, p.Limit(10))
   104  	assert.Equal(t, p, p.Limit(11))
   105  }
   106  
   107  func TestPageSortReverse(t *testing.T) {
   108  	t.Parallel()
   109  	s := newTestSite(t)
   110  	p1 := createSortTestPages(s, 10)
   111  	assert.Equal(t, 0, p1[0].fuzzyWordCount)
   112  	assert.Equal(t, 9, p1[9].fuzzyWordCount)
   113  	p2 := p1.Reverse()
   114  	assert.Equal(t, 9, p2[0].fuzzyWordCount)
   115  	assert.Equal(t, 0, p2[9].fuzzyWordCount)
   116  	// cached
   117  	assert.True(t, fastEqualPages(p2, p1.Reverse()))
   118  }
   119  
   120  func TestPageSortByParam(t *testing.T) {
   121  	t.Parallel()
   122  	var k interface{} = "arbitrarily.nested"
   123  	s := newTestSite(t)
   124  
   125  	unsorted := createSortTestPages(s, 10)
   126  	delete(unsorted[9].params, "arbitrarily")
   127  
   128  	firstSetValue, _ := unsorted[0].Param(k)
   129  	secondSetValue, _ := unsorted[1].Param(k)
   130  	lastSetValue, _ := unsorted[8].Param(k)
   131  	unsetValue, _ := unsorted[9].Param(k)
   132  
   133  	assert.Equal(t, "xyz100", firstSetValue)
   134  	assert.Equal(t, "xyz99", secondSetValue)
   135  	assert.Equal(t, "xyz92", lastSetValue)
   136  	assert.Equal(t, nil, unsetValue)
   137  
   138  	sorted := unsorted.ByParam("arbitrarily.nested")
   139  	firstSetSortedValue, _ := sorted[0].Param(k)
   140  	secondSetSortedValue, _ := sorted[1].Param(k)
   141  	lastSetSortedValue, _ := sorted[8].Param(k)
   142  	unsetSortedValue, _ := sorted[9].Param(k)
   143  
   144  	assert.Equal(t, firstSetValue, firstSetSortedValue)
   145  	assert.Equal(t, secondSetValue, lastSetSortedValue)
   146  	assert.Equal(t, lastSetValue, secondSetSortedValue)
   147  	assert.Equal(t, unsetValue, unsetSortedValue)
   148  }
   149  
   150  func BenchmarkSortByWeightAndReverse(b *testing.B) {
   151  	s := newTestSite(b)
   152  	p := createSortTestPages(s, 300)
   153  
   154  	b.ResetTimer()
   155  	for i := 0; i < b.N; i++ {
   156  		p = p.ByWeight().Reverse()
   157  	}
   158  }
   159  
   160  func setSortVals(dates [4]time.Time, titles [4]string, weights [4]int, pages Pages) {
   161  	for i := range dates {
   162  		pages[i].Date = dates[i]
   163  		pages[i].Lastmod = dates[i]
   164  		pages[i].Weight = weights[i]
   165  		pages[i].title = titles[i]
   166  		// make sure we compare apples and ... apples ...
   167  		pages[len(dates)-1-i].linkTitle = pages[i].title + "l"
   168  		pages[len(dates)-1-i].PublishDate = dates[i]
   169  		pages[len(dates)-1-i].ExpiryDate = dates[i]
   170  		pages[len(dates)-1-i].workContent = []byte(titles[i] + "_content")
   171  	}
   172  	lastLastMod := pages[2].Lastmod
   173  	pages[2].Lastmod = pages[1].Lastmod
   174  	pages[1].Lastmod = lastLastMod
   175  
   176  	for _, p := range pages {
   177  		p.resetContent()
   178  	}
   179  
   180  }
   181  
   182  func createSortTestPages(s *Site, num int) Pages {
   183  	pages := make(Pages, num)
   184  
   185  	for i := 0; i < num; i++ {
   186  		p := s.newPage(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))
   187  		p.params = map[string]interface{}{
   188  			"arbitrarily": map[string]interface{}{
   189  				"nested": ("xyz" + fmt.Sprintf("%v", 100-i)),
   190  			},
   191  		}
   192  
   193  		w := 5
   194  
   195  		if i%2 == 0 {
   196  			w = 10
   197  		}
   198  		p.fuzzyWordCount = i
   199  		p.Weight = w
   200  		p.Description = "initial"
   201  
   202  		pages[i] = p
   203  	}
   204  
   205  	return pages
   206  }