Debian Codegraph @

resources/page/pages_sort_test.go

raw
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 "context"
18 "fmt"
19 "testing"
20 "time"
21
22 "github.com/gohugoio/hugo/resources/resource"
23 "github.com/google/go-cmp/cmp"
24
25 qt "github.com/frankban/quicktest"
26 )
27
28 var eq = qt.CmpEquals(
29 cmp.Comparer(func(p1, p2 testPage) bool {
30 return p1.path == p2.path && p1.weight == p2.weight
31 }),
32 )
33
34 func TestDefaultSort(t *testing.T) {
35 t.Parallel()
36 c := qt.New(t)
37 d1 := time.Now()
38 d2 := d1.Add(-1 * time.Hour)
39 d3 := d1.Add(-2 * time.Hour)
40 d4 := d1.Add(-3 * time.Hour)
41
42 p := createSortTestPages(4)
43
44 // first by weight
45 setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "a", "c", "d"}, [4]int{4, 3, 2, 1}, p)
46 SortByDefault(p)
47
48 c.Assert(p[0].Weight(), qt.Equals, 1)
49
50 // Consider zero weight, issue #2673
51 setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "a", "d", "c"}, [4]int{0, 0, 0, 1}, p)
52 SortByDefault(p)
53
54 c.Assert(p[0].Weight(), qt.Equals, 1)
55
56 // next by date
57 setSortVals([4]time.Time{d3, d4, d1, d2}, [4]string{"a", "b", "c", "d"}, [4]int{1, 1, 1, 1}, p)
58 SortByDefault(p)
59 c.Assert(p[0].Date(), qt.Equals, d1)
60
61 // finally by link title
62 setSortVals([4]time.Time{d3, d3, d3, d3}, [4]string{"b", "c", "a", "d"}, [4]int{1, 1, 1, 1}, p)
63 SortByDefault(p)
64 c.Assert(p[0].LinkTitle(), qt.Equals, "al")
65 c.Assert(p[1].LinkTitle(), qt.Equals, "bl")
66 c.Assert(p[2].LinkTitle(), qt.Equals, "cl")
67 }
68
69 // https://github.com/gohugoio/hugo/issues/4953
70 func TestSortByLinkTitle(t *testing.T) {
71 t.Parallel()
72 c := qt.New(t)
73 pages := createSortTestPages(6)
74
75 for i, p := range pages {
76 pp := p.(*testPage)
77 if i < 5 {
78 pp.title = fmt.Sprintf("title%d", i)
79 }
80
81 if i > 2 {
82 pp.linkTitle = fmt.Sprintf("linkTitle%d", i)
83 }
84
85 }
86
87 pages.shuffle()
88
89 bylt := pages.ByLinkTitle()
90
91 for i, p := range bylt {
92 if i < 3 {
93 c.Assert(p.LinkTitle(), qt.Equals, fmt.Sprintf("linkTitle%d", i+3))
94 } else {
95 c.Assert(p.LinkTitle(), qt.Equals, fmt.Sprintf("title%d", i-3))
96 }
97 }
98 }
99
100 func TestSortByN(t *testing.T) {
101 t.Parallel()
102 d1 := time.Now()
103 d2 := d1.Add(-2 * time.Hour)
104 d3 := d1.Add(-10 * time.Hour)
105 d4 := d1.Add(-20 * time.Hour)
106
107 p := createSortTestPages(4)
108 ctx := context.Background()
109
110 byLen := func(p Pages) Pages {
111 return p.ByLength(ctx)
112 }
113
114 for i, this := range []struct {
115 sortFunc func(p Pages) Pages
116 assertFunc func(p Pages) bool
117 }{
118 {(Pages).ByWeight, func(p Pages) bool { return p[0].Weight() == 1 }},
119 {(Pages).ByTitle, func(p Pages) bool { return p[0].Title() == "ab" }},
120 {(Pages).ByLinkTitle, func(p Pages) bool { return p[0].LinkTitle() == "abl" }},
121 {(Pages).ByDate, func(p Pages) bool { return p[0].Date() == d4 }},
122 {(Pages).ByPublishDate, func(p Pages) bool { return p[0].PublishDate() == d4 }},
123 {(Pages).ByExpiryDate, func(p Pages) bool { return p[0].ExpiryDate() == d4 }},
124 {(Pages).ByLastmod, func(p Pages) bool { return p[1].Lastmod() == d3 }},
125 {byLen, func(p Pages) bool { return p[0].(resource.LengthProvider).Len(ctx) == len(p[0].(*testPage).content) }},
126 } {
127 setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "ab", "cde", "fg"}, [4]int{0, 3, 2, 1}, p)
128
129 sorted := this.sortFunc(p)
130 if !this.assertFunc(sorted) {
131 t.Errorf("[%d] sort error", i)
132 }
133 }
134 }
135
136 func TestLimit(t *testing.T) {
137 t.Parallel()
138 c := qt.New(t)
139 p := createSortTestPages(10)
140 firstFive := p.Limit(5)
141 c.Assert(len(firstFive), qt.Equals, 5)
142 for i := range 5 {
143 c.Assert(firstFive[i], qt.Equals, p[i])
144 }
145 c.Assert(p.Limit(10), eq, p)
146 c.Assert(p.Limit(11), eq, p)
147 }
148
149 func TestPageSortReverse(t *testing.T) {
150 t.Parallel()
151 c := qt.New(t)
152 p1 := createSortTestPages(10)
153 c.Assert(p1[0].(*testPage).fuzzyWordCount, qt.Equals, 0)
154 c.Assert(p1[9].(*testPage).fuzzyWordCount, qt.Equals, 9)
155 p2 := p1.Reverse()
156 c.Assert(p2[0].(*testPage).fuzzyWordCount, qt.Equals, 9)
157 c.Assert(p2[9].(*testPage).fuzzyWordCount, qt.Equals, 0)
158 // cached
159 c.Assert(pagesEqual(p2, p1.Reverse()), qt.Equals, true)
160 }
161
162 func TestPageSortByParam(t *testing.T) {
163 t.Parallel()
164 c := qt.New(t)
165 var k any = "arbitrarily.nested"
166
167 unsorted := createSortTestPages(10)
168 delete(unsorted[9].Params(), "arbitrarily")
169
170 firstSetValue, _ := unsorted[0].Param(k)
171 secondSetValue, _ := unsorted[1].Param(k)
172 lastSetValue, _ := unsorted[8].Param(k)
173 unsetValue, _ := unsorted[9].Param(k)
174
175 c.Assert(firstSetValue, qt.Equals, "xyz100")
176 c.Assert(secondSetValue, qt.Equals, "xyz99")
177 c.Assert(lastSetValue, qt.Equals, "xyz92")
178 c.Assert(unsetValue, qt.Equals, nil)
179
180 sorted := unsorted.ByParam("arbitrarily.nested")
181 firstSetSortedValue, _ := sorted[0].Param(k)
182 secondSetSortedValue, _ := sorted[1].Param(k)
183 lastSetSortedValue, _ := sorted[8].Param(k)
184 unsetSortedValue, _ := sorted[9].Param(k)
185
186 c.Assert(firstSetSortedValue, qt.Equals, firstSetValue)
187 c.Assert(lastSetSortedValue, qt.Equals, secondSetValue)
188 c.Assert(secondSetSortedValue, qt.Equals, lastSetValue)
189 c.Assert(unsetSortedValue, qt.Equals, unsetValue)
190 }
191
192 func TestPageSortByParamNumeric(t *testing.T) {
193 t.Parallel()
194 c := qt.New(t)
195
196 var k any = "arbitrarily.nested"
197
198 n := 10
199 unsorted := createSortTestPages(n)
200 for i := range n {
201 v := 100 - i
202 if i%2 == 0 {
203 v = 100.0 - i
204 }
205
206 unsorted[i].(*testPage).params = map[string]any{
207 "arbitrarily": map[string]any{
208 "nested": v,
209 },
210 }
211 }
212 delete(unsorted[9].Params(), "arbitrarily")
213
214 firstSetValue, _ := unsorted[0].Param(k)
215 secondSetValue, _ := unsorted[1].Param(k)
216 lastSetValue, _ := unsorted[8].Param(k)
217 unsetValue, _ := unsorted[9].Param(k)
218
219 c.Assert(firstSetValue, qt.Equals, 100)
220 c.Assert(secondSetValue, qt.Equals, 99)
221 c.Assert(lastSetValue, qt.Equals, 92)
222 c.Assert(unsetValue, qt.Equals, nil)
223
224 sorted := unsorted.ByParam("arbitrarily.nested")
225 firstSetSortedValue, _ := sorted[0].Param(k)
226 secondSetSortedValue, _ := sorted[1].Param(k)
227 lastSetSortedValue, _ := sorted[8].Param(k)
228 unsetSortedValue, _ := sorted[9].Param(k)
229
230 c.Assert(firstSetSortedValue, qt.Equals, 92)
231 c.Assert(secondSetSortedValue, qt.Equals, 93)
232 c.Assert(lastSetSortedValue, qt.Equals, 100)
233 c.Assert(unsetSortedValue, qt.Equals, unsetValue)
234 }
235
236 func BenchmarkSortByWeightAndReverse(b *testing.B) {
237 p := createSortTestPages(300)
238
239 for b.Loop() {
240 p = p.ByWeight().Reverse()
241 }
242 }
243
244 func setSortVals(dates [4]time.Time, titles [4]string, weights [4]int, pages Pages) {
245 for i := range dates {
246 this := pages[i].(*testPage)
247 other := pages[len(dates)-1-i].(*testPage)
248
249 this.date = dates[i]
250 this.lastMod = dates[i]
251 this.weight = weights[i]
252 this.title = titles[i]
253 // make sure we compare apples and ... apples ...
254 other.linkTitle = this.Title() + "l"
255 other.pubDate = dates[i]
256 other.expiryDate = dates[i]
257 other.content = titles[i] + "_content"
258 }
259 lastLastMod := pages[2].Lastmod()
260 pages[2].(*testPage).lastMod = pages[1].Lastmod()
261 pages[1].(*testPage).lastMod = lastLastMod
262
263 for _, p := range pages {
264 p.(*testPage).content = ""
265 }
266 }
267
268 func createSortTestPages(num int) Pages {
269 pages := make(Pages, num)
270
271 for i := range num {
272 p := newTestPage()
273 p.path = fmt.Sprintf("/x/y/p%d.md", i)
274 p.title = fmt.Sprintf("Title %d", i%((num+1)/2))
275 p.params = map[string]any{
276 "arbitrarily": map[string]any{
277 "nested": ("xyz" + fmt.Sprintf("%v", 100-i)),
278 },
279 }
280
281 w := 5
282
283 if i%2 == 0 {
284 w = 10
285 }
286 p.fuzzyWordCount = i
287 p.weight = w
288 p.description = "initial"
289
290 pages[i] = p
291 }
292
293 return pages
294 }