Debian Codegraph @

tpl/collections/collections_integration_test.go

raw
1 // Copyright 2025 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 collections_test
15
16 import (
17 "context"
18 "fmt"
19 "strings"
20 "testing"
21
22 "github.com/gohugoio/hugo/hugolib"
23 "github.com/gohugoio/hugo/resources/page"
24 "github.com/gohugoio/hugo/tpl/collections"
25 )
26
27 // Issue 9585
28 func TestApplyWithContext(t *testing.T) {
29 t.Parallel()
30
31 files := `
32 -- hugo.toml --
33 baseURL = 'http://example.com/'
34 -- layouts/home.html --
35 {{ apply (seq 3) "partial" "foo.html"}}
36 -- layouts/_partials/foo.html --
37 {{ return "foo"}}
38 `
39
40 b := hugolib.Test(t, files)
41
42 b.AssertFileContent("public/index.html", `
43 [foo foo foo]
44 `)
45 }
46
47 func TestApplyBuiltInIssue13418(t *testing.T) {
48 t.Parallel()
49
50 files := `
51 -- hugo.toml --
52 baseURL = 'http://example.com/'
53 -- layouts/home.html --
54 len: {{ apply (slice "hello") "len" "." }}
55 not: {{ apply (slice "hello") "not" "." }}
56 `
57 b := hugolib.Test(t, files)
58
59 b.AssertFileContent("public/index.html", "len: [5]", "not: [false]")
60 }
61
62 // Issue 9865
63 func TestSortStable(t *testing.T) {
64 t.Parallel()
65
66 files := `
67 -- hugo.toml --
68 -- layouts/home.html --
69 {{ $values := slice (dict "a" 1 "b" 2) (dict "a" 3 "b" 1) (dict "a" 2 "b" 0) (dict "a" 1 "b" 0) (dict "a" 3 "b" 1) (dict "a" 2 "b" 2) (dict "a" 2 "b" 1) (dict "a" 0 "b" 3) (dict "a" 3 "b" 3) (dict "a" 0 "b" 0) (dict "a" 0 "b" 0) (dict "a" 2 "b" 0) (dict "a" 1 "b" 2) (dict "a" 1 "b" 1) (dict "a" 3 "b" 0) (dict "a" 2 "b" 0) (dict "a" 3 "b" 0) (dict "a" 3 "b" 0) (dict "a" 3 "b" 0) (dict "a" 3 "b" 1) }}
70 Asc: {{ sort (sort $values "b" "asc") "a" "asc" }}
71 Desc: {{ sort (sort $values "b" "desc") "a" "desc" }}
72
73 `
74
75 for range 4 {
76
77 b := hugolib.Test(t, files)
78
79 b.AssertFileContent("public/index.html", `
80 Asc: [map[a:0 b:0] map[a:0 b:0] map[a:0 b:3] map[a:1 b:0] map[a:1 b:1] map[a:1 b:2] map[a:1 b:2] map[a:2 b:0] map[a:2 b:0] map[a:2 b:0] map[a:2 b:1] map[a:2 b:2] map[a:3 b:0] map[a:3 b:0] map[a:3 b:0] map[a:3 b:0] map[a:3 b:1] map[a:3 b:1] map[a:3 b:1] map[a:3 b:3]]
81 Desc: [map[a:3 b:3] map[a:3 b:1] map[a:3 b:1] map[a:3 b:1] map[a:3 b:0] map[a:3 b:0] map[a:3 b:0] map[a:3 b:0] map[a:2 b:2] map[a:2 b:1] map[a:2 b:0] map[a:2 b:0] map[a:2 b:0] map[a:1 b:2] map[a:1 b:2] map[a:1 b:1] map[a:1 b:0] map[a:0 b:3] map[a:0 b:0] map[a:0 b:0]]
82 `)
83
84 }
85 }
86
87 // Issue #11004.
88 func TestAppendSliceToASliceOfSlices(t *testing.T) {
89 t.Parallel()
90
91 files := `
92 -- hugo.toml --
93 -- layouts/home.html --
94 {{ $obj := slice (slice "a") }}
95 {{ $obj = $obj | append (slice "b") }}
96 {{ $obj = $obj | append (slice "c") }}
97
98 {{ $obj }}
99
100 `
101
102 b := hugolib.Test(t, files)
103
104 b.AssertFileContent("public/index.html", "[[a] [b] [c]]")
105 }
106
107 func TestAppendNilToSlice(t *testing.T) {
108 t.Parallel()
109
110 files := `
111 -- hugo.toml --
112 -- layouts/home.html --
113 {{ $obj := (slice "a") }}
114 {{ $obj = $obj | append nil }}
115
116 {{ $obj }}
117
118
119 `
120
121 b := hugolib.Test(t, files)
122
123 b.AssertFileContent("public/index.html", "[a <nil>]")
124 }
125
126 func TestAppendNilsToSliceWithNils(t *testing.T) {
127 t.Parallel()
128
129 files := `
130 -- hugo.toml --
131 -- layouts/home.html --
132 {{ $obj := (slice "a" nil "c") }}
133 {{ $obj = $obj | append nil }}
134
135 {{ $obj }}
136
137
138 `
139
140 for range 4 {
141
142 b := hugolib.Test(t, files)
143
144 b.AssertFileContent("public/index.html", "[a <nil> c <nil>]")
145
146 }
147 }
148
149 // Issue 11234.
150 func TestWhereWithWordCount(t *testing.T) {
151 t.Parallel()
152
153 files := `
154 -- hugo.toml --
155 baseURL = 'http://example.com/'
156 -- layouts/home.html --
157 Home: {{ range where site.RegularPages "WordCount" "gt" 50 }}{{ .Title }}|{{ end }}
158 -- layouts/_shortcodes/lorem.html --
159 {{ "ipsum " | strings.Repeat (.Get 0 | int) }}
160
161 -- content/p1.md --
162 ---
163 title: "p1"
164 ---
165 {{< lorem 100 >}}
166 -- content/p2.md --
167 ---
168 title: "p2"
169 ---
170 {{< lorem 20 >}}
171 -- content/p3.md --
172 ---
173 title: "p3"
174 ---
175 {{< lorem 60 >}}
176 `
177
178 b := hugolib.Test(t, files)
179
180 b.AssertFileContent("public/index.html", `
181 Home: p1|p3|
182 `)
183 }
184
185 // Issue #11279
186 func TestWhereLikeOperator(t *testing.T) {
187 t.Parallel()
188 files := `
189 -- content/p1.md --
190 ---
191 title: P1
192 foo: ab
193 ---
194 -- content/p2.md --
195 ---
196 title: P2
197 foo: abc
198 ---
199 -- content/p3.md --
200 ---
201 title: P3
202 foo: bc
203 ---
204 -- layouts/home.html --
205 <ul>
206 {{- range where site.RegularPages "Params.foo" "like" "^ab" -}}
207 <li>{{ .Title }}</li>
208 {{- end -}}
209 </ul>
210 `
211 b := hugolib.Test(t, files)
212 b.AssertFileContent("public/index.html", "<ul><li>P1</li><li>P2</li></ul>")
213 }
214
215 func TestTermEntriesCollectionsIssue12254(t *testing.T) {
216 t.Parallel()
217
218 files := `
219 -- hugo.toml --
220 capitalizeListTitles = false
221 disableKinds = ['rss','sitemap']
222 -- content/p1.md --
223 ---
224 title: p1
225 categories: [cat-a]
226 tags: ['tag-b','tag-a','tag-c']
227 ---
228 -- content/p2.md --
229 ---
230 title: p2
231 categories: [cat-a]
232 tags: ['tag-b','tag-a']
233 ---
234 -- content/p3.md --
235 ---
236 title: p3
237 categories: [cat-a]
238 tags: ['tag-b']
239 ---
240 -- layouts/term.html --
241 {{ $list1 := .Pages }}
242 {{ range $i, $e := site.Taxonomies.tags.ByCount }}
243 {{ $list2 := .Pages }}
244 {{ $i }}: List1: {{ len $list1 }}|
245 {{ $i }}: List2: {{ len $list2 }}|
246 {{ $i }}: Intersect: {{ intersect $.Pages .Pages | len }}|
247 {{ $i }}: Union: {{ union $.Pages .Pages | len }}|
248 {{ $i }}: SymDiff: {{ symdiff $.Pages .Pages | len }}|
249 {{ $i }}: Uniq: {{ append $.Pages .Pages | uniq | len }}|
250 {{ end }}
251
252
253 `
254 b := hugolib.Test(t, files)
255
256 b.AssertFileContent("public/categories/cat-a/index.html",
257 "0: List1: 3|\n0: List2: 3|\n0: Intersect: 3|\n0: Union: 3|\n0: SymDiff: 0|\n0: Uniq: 3|\n\n\n1: List1: 3|",
258 "1: List2: 2|\n1: Intersect: 2|\n1: Union: 3|\n1: SymDiff: 1|\n1: Uniq: 3|\n\n\n2: List1: 3|\n2: List2: 1|",
259 "2: Intersect: 1|\n2: Union: 3|\n2: SymDiff: 2|\n2: Uniq: 3|",
260 )
261 }
262
263 // Issue #13181
264 func TestUnionResourcesMatch(t *testing.T) {
265 t.Parallel()
266
267 files := `
268 -- hugo.toml --
269 disableKinds = ['rss','sitemap', 'taxonomy', 'term', 'page']
270 -- layouts/home.html --
271 {{ $a := resources.Match "*a*" }}
272 {{ $b := resources.Match "*b*" }}
273 {{ $union := $a | union $b }}
274 {{ range $i, $e := $union }}
275 {{ $i }}: {{ .Name }}
276 {{ end }}$
277 -- assets/a1.html --
278 <div>file1</div>
279 -- assets/a2.html --
280 <div>file2</div>
281 -- assets/a3_b1.html --
282 <div>file3</div>
283 -- assets/b2.html --
284 <div>file4</div>
285 `
286
287 b := hugolib.Test(t, files)
288
289 b.AssertFileContentExact("public/index.html", "0: /a3_b1.html\n\n1: /b2.html\n\n2: /a1.html\n\n3: /a2.html\n$")
290 }
291
292 // Issue 14777.
293 func TestWhereWithPageEqualityIssue14777(t *testing.T) {
294 t.Parallel()
295
296 files := `
297 -- hugo.toml --
298 baseURL = 'http://example.com/'
299 disableKinds = ['rss','sitemap','taxonomy','term']
300 -- content/s1/_index.md --
301 ---
302 title: S1
303 ---
304 -- content/s1/p1.md --
305 ---
306 title: P1
307 ---
308 -- content/s1/p2.md --
309 ---
310 title: P2
311 ---
312 -- content/s2/_index.md --
313 ---
314 title: S2
315 ---
316 -- content/s2/p3.md --
317 ---
318 title: P3
319 ---
320 -- layouts/section.html --
321 {{ $page := . }}
322 WhereEq: {{ range where site.Pages "Parent" "eq" $page }}{{ .Title }}|{{ end }}$
323 WhereDefault: {{ range where site.Pages "Parent" $page }}{{ .Title }}|{{ end }}$
324 RangeIf: {{ range site.Pages }}{{ if eq .Parent $page }}{{ .Title }}|{{ end }}{{ end }}$
325 WhereNe: {{ range where site.Pages "Parent" "ne" $page }}{{ .Title }}|{{ end }}$
326 `
327
328 b := hugolib.Test(t, files)
329
330 b.AssertFileContent("public/s1/index.html",
331 "WhereEq: P1|P2|$",
332 "WhereDefault: P1|P2|$",
333 "RangeIf: P1|P2|$",
334 )
335 }
336
337 // Issue 13621.
338 func TestWhereNotInEmptySlice(t *testing.T) {
339 t.Parallel()
340
341 files := `
342 -- hugo.toml --
343 -- layouts/home.html --
344 {{- $pages := where site.RegularPages "Kind" "not in" (slice) -}}
345 Len: {{ $pages | len }}|
346 -- layouts/all.html --
347 All|{{ .Title }}|
348 -- content/p1.md --
349
350 `
351
352 b := hugolib.Test(t, files)
353
354 b.AssertFileContent("public/index.html", "Len: 1|")
355 }
356
357 func TestD(t *testing.T) {
358 t.Parallel()
359 files := `
360 -- hugo.toml --
361 -- layouts/home.html --
362 {{ $p := site.RegularPages }}
363 5 random pages: {{ range collections.D 42 5 ($p | len) }}{{ with (index $p .) }}{{ .RelPermalink }}|{{ end }}{{ end }}$
364 -- content/a.md --
365 -- content/b.md --
366 -- content/c.md --
367 -- content/d.md --
368 -- content/e.md --
369 -- content/f.md --
370 -- content/g.md --
371 -- content/h.md --
372 -- content/i.md --
373 -- content/j.md --
374 -- content/k.md --
375 -- content/l.md --
376 -- content/m.md --
377 -- content/n.md --
378 -- content/o.md --
379 -- content/p.md --
380
381 `
382 b := hugolib.Test(t, files)
383
384 b.AssertFileContentExact("public/index.html", "5 random pages: /b/|/g/|/j/|/k/|/l/|$")
385 }
386
387 func TestGroup(t *testing.T) {
388 t.Parallel()
389
390 files := `
391 -- hugo.toml --
392 disableKinds = ["rss", "sitemap", "taxonomy", "term"]
393 -- layouts/home.html --
394 {{ $cool := .Site.RegularPages | group "cool" }}
395 {{ $cool.Key }}: {{ len $cool.Pages }}
396 -- content/page1.md --
397 -- content/page2.md --
398 `
399
400 hugolib.Test(t, files).AssertFileContent("public/index.html", "cool: 2")
401 }
402
403 func TestSlice(t *testing.T) {
404 t.Parallel()
405
406 files := `
407 -- hugo.toml --
408 disableKinds = ["rss", "sitemap"]
409 baseURL = "http://example.com/"
410 -- layouts/home.html --
411 {{ $cool := .Site.RegularPages | group "cool" }}
412 {{ $cool.Key }}: {{ len $cool.Pages }}
413 -- content/page1.md --
414 ---
415 title: "Page 1"
416 tags: ["blue", "green"]
417 tags_weight: 10
418 ---
419 -- content/page2.md --
420 ---
421 title: "Page 2"
422 tags: ["blue", "green"]
423 tags_weight: 20
424 ---
425 -- layouts/home.html --
426 {{ $cool := first 1 .Site.RegularPages | group "cool" }}
427 {{ $blue := after 1 .Site.RegularPages | group "blue" }}
428 {{ $weightedPages := index (index .Site.Taxonomies "tags") "blue" }}
429
430 {{ $p1 := index .Site.RegularPages 0 }}{{ $p2 := index .Site.RegularPages 1 }}
431 {{ $wp1 := index $weightedPages 0 }}{{ $wp2 := index $weightedPages 1 }}
432
433 {{ $pages := slice $p1 $p2 }}
434 {{ $pageGroups := slice $cool $blue }}
435 {{ $weighted := slice $wp1 $wp2 }}
436
437 {{ printf "pages:%d:%T:%s|%s" (len $pages) $pages (index $pages 0).Path (index $pages 1).Path }}
438 {{ printf "pageGroups:%d:%T:%s|%s" (len $pageGroups) $pageGroups (index (index $pageGroups 0).Pages 0).Path (index (index $pageGroups 1).Pages 0).Path}}
439 {{ printf "weightedPages:%d:%T" (len $weighted) $weighted | safeHTML }}
440 `
441
442 hugolib.Test(t, files).AssertFileContent("public/index.html",
443 "pages:2:page.Pages:/page1|/page2",
444 "pageGroups:2:page.PagesGroup:/page1|/page2",
445 `weightedPages:2:page.WeightedPages`)
446 }
447
448 func TestUnion(t *testing.T) {
449 t.Parallel()
450
451 files := `
452 -- hugo.toml --
453 disableKinds = ["rss", "sitemap"]
454 baseURL = "http://example.com/"
455 -- layouts/home.html --
456 {{ $cool := .Site.RegularPages | group "cool" }}
457 {{ $cool.Key }}: {{ len $cool.Pages }}
458 -- content/page1.md --
459 ---
460 title: "Page 1"
461 tags: ["blue", "green"]
462 tags_weight: 10
463 ---
464 -- content/page2.md --
465 ---
466 title: "Page 2"
467 tags: ["blue", "green"]
468 tags_weight: 20
469 ---
470 -- content/page3.md --
471 ---
472 title: "Page 3"
473 tags: ["blue", "green"]
474 tags_weight: 30
475 ---
476 -- layouts/home.html --
477 {{ $unionPages := first 2 .Site.RegularPages | union .Site.RegularPages }}
478 {{ $unionWeightedPages := .Site.Taxonomies.tags.blue | union .Site.Taxonomies.tags.green }}
479 {{ printf "unionPages: %T %d" $unionPages (len $unionPages) }}
480 {{ printf "unionWeightedPages: %T %d" $unionWeightedPages (len $unionWeightedPages) }}
481 `
482
483 hugolib.Test(t, files).AssertFileContent("public/index.html",
484 "unionPages: page.Pages 3",
485 "unionWeightedPages: page.WeightedPages 6",
486 )
487 }
488
489 func TestCollectionsFuncs(t *testing.T) {
490 t.Parallel()
491
492 files := `
493 -- hugo.toml --
494 disableKinds = ["rss", "sitemap"]
495 baseURL = "http://example.com/"
496 -- layouts/home.html --
497 {{ $cool := .Site.RegularPages | group "cool" }}
498 {{ $cool.Key }}: {{ len $cool.Pages }}
499 -- content/page1.md --
500 ---
501 title: "Page 1"
502 tags: ["blue", "green"]
503 tags_weight: 10
504 ---
505 -- content/page2.md --
506 ---
507 title: "Page 2"
508 tags: ["blue", "green"]
509 tags_weight: 20
510 ---
511 -- content/page3.md --
512 ---
513 title: "Page 3"
514 tags: ["blue", "green"]
515 tags_weight: 30
516 ---
517 -- layouts/home.html --
518 {{ $uniqPages := first 2 .Site.RegularPages | append .Site.RegularPages | uniq }}
519 {{ $inTrue := in .Site.RegularPages (index .Site.RegularPages 1) }}
520 {{ $inFalse := in .Site.RegularPages (.Site.Home) }}
521
522 {{ printf "uniqPages: %T %d" $uniqPages (len $uniqPages) }}
523 {{ printf "inTrue: %t" $inTrue }}
524 {{ printf "inFalse: %t" $inFalse }}
525 -- layouts/single.html --
526 {{ $related := .Site.RegularPages.Related . }}
527 {{ $symdiff := $related | symdiff .Site.RegularPages }}
528 Related: {{ range $related }}{{ .RelPermalink }}|{{ end }}
529 Symdiff: {{ range $symdiff }}{{ .RelPermalink }}|{{ end }}
530 `
531
532 b := hugolib.Test(t, files)
533
534 b.AssertFileContent("public/index.html",
535 "uniqPages: page.Pages 3",
536 "inTrue: true",
537 "inFalse: false",
538 )
539
540 b.AssertFileContent("public/page1/index.html", `Related: /page2/|/page3/|`, `Symdiff: /page1/|`)
541 }
542
543 func TestAppend(t *testing.T) {
544 t.Parallel()
545
546 files := `
547 -- hugo.toml --
548 disableKinds = ["rss", "sitemap"]
549 baseURL = "http://example.com/"
550 -- layouts/home.html --
551 {{ $cool := .Site.RegularPages | group "cool" }}
552 {{ $cool.Key }}: {{ len $cool.Pages }}
553 -- content/page1.md --
554 ---
555 title: "Page 1"
556 tags: ["blue", "green"]
557 tags_weight: 10
558 ---
559 -- content/page2.md --
560 ---
561 title: "Page 2"
562 tags: ["blue", "green"]
563 tags_weight: 20
564 ---
565 -- layouts/home.html --
566 {{ $p1 := index .Site.RegularPages 0 }}{{ $p2 := index .Site.RegularPages 1 }}
567
568 {{ $pages := slice }}
569
570 {{ if true }}
571 {{ $pages = $pages | append $p2 $p1 }}
572 {{ end }}
573 {{ $appendPages := .Site.Pages | append .Site.RegularPages }}
574 {{ $appendStrings := slice "a" "b" | append "c" "d" "e" }}
575 {{ $appendStringsSlice := slice "a" "b" "c" | append (slice "c" "d") }}
576
577 {{ printf "pages:%d:%T:%s|%s" (len $pages) $pages (index $pages 0).Path (index $pages 1).Path }}
578 {{ printf "appendPages:%d:%T:%v/%v" (len $appendPages) $appendPages (index $appendPages 0).Kind (index $appendPages 8).Kind }}
579 {{ printf "appendStrings:%T:%v" $appendStrings $appendStrings }}
580 {{ printf "appendStringsSlice:%T:%v" $appendStringsSlice $appendStringsSlice }}
581
582 {{/* add some slightly related funcs to check what types we get */}}
583 {{ $u := $appendStrings | union $appendStringsSlice }}
584 {{ $i := $appendStrings | intersect $appendStringsSlice }}
585 {{ printf "union:%T:%v" $u $u }}
586 {{ printf "intersect:%T:%v" $i $i }}
587 `
588
589 hugolib.Test(t, files).AssertFileContent("public/index.html",
590 "pages:2:page.Pages:/page2|/page1",
591 "appendPages:9:page.Pages:home/page",
592 "appendStrings:[]string:[a b c d e]",
593 "appendStringsSlice:[]string:[a b c c d]",
594 "union:[]string:[a b c d e]",
595 "intersect:[]string:[a b c d]",
596 )
597 }
598
599 func BenchmarkWhereAndSortPages(b *testing.B) {
600 pageTemplate := `
601 -- content/page%04d.md --
602 ---
603 title: Page%04d
604 ---
605 `
606
607 var files strings.Builder
608 files.WriteString(`
609 -- hugo.toml --
610 -- layouts/all.html --
611 All.
612 `)
613
614 for i := range 500 {
615 files.WriteString(fmt.Sprintf(pageTemplate, i, i))
616 }
617
618 bb := hugolib.Test(b, files.String(), hugolib.TestOptWithConfig(func(conf *hugolib.IntegrationTestConfig) {
619 conf.BuildCfg = hugolib.BuildCfg{
620 SkipRender: true,
621 }
622 }))
623
624 s := bb.H.Sites[0]
625 seq := s.RegularPages()
626 ns := s.TemplateStore.GetTemplateFuncsNamespace("collections").(*collections.Namespace)
627
628 b.ResetTimer()
629
630 b.Run("Where", func(b *testing.B) {
631 for b.Loop() {
632 v, err := ns.Where(context.Background(), seq, "Title", "ge", "Page0480")
633 if err != nil {
634 b.Fatal(err)
635 }
636 res := v.(page.Pages)
637 if len(res) != 20 {
638 b.Fatalf("Where didn't return an expected result, got %d", len(res))
639 }
640 }
641 })
642
643 b.Run("Sort", func(b *testing.B) {
644 for b.Loop() {
645 v, err := ns.Sort(context.Background(), s.RegularPages(), "Title", "desc")
646 if err != nil {
647 b.Fatal(err)
648 }
649 res := v.(page.Pages)
650 if len(res) != 500 {
651 b.Fatalf("Sort didn't return an expected result, got %d", len(res))
652 }
653 }
654 })
655 }