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