hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

collections_test.go (6761B)

    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 hugolib
   15 
   16 import (
   17 	"fmt"
   18 	"testing"
   19 
   20 	qt "github.com/frankban/quicktest"
   21 )
   22 
   23 func TestGroupFunc(t *testing.T) {
   24 	c := qt.New(t)
   25 
   26 	pageContent := `
   27 ---
   28 title: "Page"
   29 ---
   30 
   31 `
   32 	b := newTestSitesBuilder(t)
   33 	b.WithSimpleConfigFile().
   34 		WithContent("page1.md", pageContent, "page2.md", pageContent).
   35 		WithTemplatesAdded("index.html", `
   36 {{ $cool := .Site.RegularPages | group "cool" }}
   37 {{ $cool.Key }}: {{ len $cool.Pages }}
   38 
   39 `)
   40 	b.CreateSites().Build(BuildCfg{})
   41 
   42 	c.Assert(len(b.H.Sites), qt.Equals, 1)
   43 	c.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 2)
   44 
   45 	b.AssertFileContent("public/index.html", "cool: 2")
   46 }
   47 
   48 func TestSliceFunc(t *testing.T) {
   49 	c := qt.New(t)
   50 
   51 	pageContent := `
   52 ---
   53 title: "Page"
   54 tags: ["blue", "green"]
   55 tags_weight: %d
   56 ---
   57 
   58 `
   59 	b := newTestSitesBuilder(t)
   60 	b.WithSimpleConfigFile().
   61 		WithContent("page1.md", fmt.Sprintf(pageContent, 10), "page2.md", fmt.Sprintf(pageContent, 20)).
   62 		WithTemplatesAdded("index.html", `
   63 {{ $cool := first 1 .Site.RegularPages | group "cool" }}
   64 {{ $blue := after 1 .Site.RegularPages | group "blue" }}
   65 {{ $weightedPages := index (index .Site.Taxonomies "tags") "blue" }}
   66 
   67 {{ $p1 := index .Site.RegularPages 0 }}{{ $p2 := index .Site.RegularPages 1 }}
   68 {{ $wp1 := index $weightedPages 0 }}{{ $wp2 := index $weightedPages 1 }}
   69 
   70 {{ $pages := slice $p1 $p2 }}
   71 {{ $pageGroups := slice $cool $blue }}
   72 {{ $weighted := slice $wp1 $wp2 }}
   73 
   74 {{ printf "pages:%d:%T:%v/%v" (len $pages) $pages (index $pages 0) (index $pages 1) }}
   75 {{ printf "pageGroups:%d:%T:%v/%v" (len $pageGroups) $pageGroups (index (index $pageGroups 0).Pages 0) (index (index $pageGroups 1).Pages 0)}}
   76 {{ printf "weightedPages:%d::%T:%v" (len $weighted) $weighted $weighted | safeHTML }}
   77 
   78 `)
   79 	b.CreateSites().Build(BuildCfg{})
   80 
   81 	c.Assert(len(b.H.Sites), qt.Equals, 1)
   82 	c.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 2)
   83 
   84 	b.AssertFileContent("public/index.html",
   85 		"pages:2:page.Pages:Page(/page1.md)/Page(/page2.md)",
   86 		"pageGroups:2:page.PagesGroup:Page(/page1.md)/Page(/page2.md)",
   87 		`weightedPages:2::page.WeightedPages:[WeightedPage(10,"Page") WeightedPage(20,"Page")]`)
   88 }
   89 
   90 func TestUnionFunc(t *testing.T) {
   91 	c := qt.New(t)
   92 
   93 	pageContent := `
   94 ---
   95 title: "Page"
   96 tags: ["blue", "green"]
   97 tags_weight: %d
   98 ---
   99 
  100 `
  101 	b := newTestSitesBuilder(t)
  102 	b.WithSimpleConfigFile().
  103 		WithContent("page1.md", fmt.Sprintf(pageContent, 10), "page2.md", fmt.Sprintf(pageContent, 20),
  104 			"page3.md", fmt.Sprintf(pageContent, 30)).
  105 		WithTemplatesAdded("index.html", `
  106 {{ $unionPages := first 2 .Site.RegularPages | union .Site.RegularPages  }}
  107 {{ $unionWeightedPages := .Site.Taxonomies.tags.blue | union .Site.Taxonomies.tags.green }}
  108 {{ printf "unionPages: %T %d" $unionPages (len $unionPages) }} 
  109 {{ printf "unionWeightedPages: %T %d" $unionWeightedPages (len $unionWeightedPages) }}
  110 `)
  111 	b.CreateSites().Build(BuildCfg{})
  112 
  113 	c.Assert(len(b.H.Sites), qt.Equals, 1)
  114 	c.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 3)
  115 
  116 	b.AssertFileContent("public/index.html",
  117 		"unionPages: page.Pages 3",
  118 		"unionWeightedPages: page.WeightedPages 6")
  119 }
  120 
  121 func TestCollectionsFuncs(t *testing.T) {
  122 	c := qt.New(t)
  123 
  124 	pageContent := `
  125 ---
  126 title: "Page %d"
  127 tags: ["blue", "green"]
  128 tags_weight: %d
  129 ---
  130 
  131 `
  132 	b := newTestSitesBuilder(t)
  133 	b.WithSimpleConfigFile().
  134 		WithContent("page1.md", fmt.Sprintf(pageContent, 10, 10), "page2.md", fmt.Sprintf(pageContent, 20, 20),
  135 			"page3.md", fmt.Sprintf(pageContent, 30, 30)).
  136 		WithTemplatesAdded("index.html", `
  137 {{ $uniqPages := first 2 .Site.RegularPages | append .Site.RegularPages | uniq  }}
  138 {{ $inTrue := in .Site.RegularPages (index .Site.RegularPages 1)  }}
  139 {{ $inFalse := in .Site.RegularPages (.Site.Home)  }}
  140 
  141 {{ printf "uniqPages: %T %d" $uniqPages (len $uniqPages) }}
  142 {{ printf "inTrue: %t" $inTrue }}
  143 {{ printf "inFalse: %t" $inFalse  }}
  144 `)
  145 
  146 	b.WithTemplatesAdded("_default/single.html", `
  147 {{ $related := .Site.RegularPages.Related . }}
  148 {{ $symdiff := $related | symdiff .Site.RegularPages }}
  149 Related: {{ range $related }}{{ .RelPermalink }}|{{ end }}
  150 Symdiff: {{ range $symdiff }}{{ .RelPermalink }}|{{ end }}
  151 `)
  152 	b.CreateSites().Build(BuildCfg{})
  153 
  154 	c.Assert(len(b.H.Sites), qt.Equals, 1)
  155 	c.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 3)
  156 
  157 	b.AssertFileContent("public/index.html",
  158 		"uniqPages: page.Pages 3",
  159 		"inTrue: true",
  160 		"inFalse: false",
  161 	)
  162 
  163 	b.AssertFileContent("public/page1/index.html", `Related: /page2/|/page3/|`, `Symdiff: /page1/|`)
  164 }
  165 
  166 func TestAppendFunc(t *testing.T) {
  167 	c := qt.New(t)
  168 
  169 	pageContent := `
  170 ---
  171 title: "Page"
  172 tags: ["blue", "green"]
  173 tags_weight: %d
  174 ---
  175 
  176 `
  177 	b := newTestSitesBuilder(t)
  178 	b.WithSimpleConfigFile().
  179 		WithContent("page1.md", fmt.Sprintf(pageContent, 10), "page2.md", fmt.Sprintf(pageContent, 20)).
  180 		WithTemplatesAdded("index.html", `
  181 {{ $p1 := index .Site.RegularPages 0 }}{{ $p2 := index .Site.RegularPages 1 }}
  182 
  183 {{ $pages := slice }}
  184 
  185 {{ if true }}
  186 	{{ $pages = $pages | append $p2 $p1 }}
  187 {{ end }}
  188 {{ $appendPages := .Site.Pages | append .Site.RegularPages }}
  189 {{ $appendStrings := slice "a" "b" | append "c" "d" "e" }}
  190 {{ $appendStringsSlice := slice "a" "b" "c" | append (slice "c" "d") }}
  191 
  192 {{ printf "pages:%d:%T:%v/%v" (len $pages) $pages (index $pages 0) (index $pages 1)  }}
  193 {{ printf "appendPages:%d:%T:%v/%v" (len $appendPages) $appendPages (index $appendPages 0).Kind (index $appendPages 8).Kind  }}
  194 {{ printf "appendStrings:%T:%v"  $appendStrings $appendStrings  }}
  195 {{ printf "appendStringsSlice:%T:%v"  $appendStringsSlice $appendStringsSlice }}
  196 
  197 {{/* add some slightly related funcs to check what types we get */}}
  198 {{ $u :=  $appendStrings | union $appendStringsSlice }}
  199 {{ $i :=  $appendStrings | intersect $appendStringsSlice }}
  200 {{ printf "union:%T:%v" $u $u  }}
  201 {{ printf "intersect:%T:%v" $i $i }}
  202 
  203 `)
  204 	b.CreateSites().Build(BuildCfg{})
  205 
  206 	c.Assert(len(b.H.Sites), qt.Equals, 1)
  207 	c.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 2)
  208 
  209 	b.AssertFileContent("public/index.html",
  210 		"pages:2:page.Pages:Page(/page2.md)/Page(/page1.md)",
  211 		"appendPages:9:page.Pages:home/page",
  212 		"appendStrings:[]string:[a b c d e]",
  213 		"appendStringsSlice:[]string:[a b c c d]",
  214 		"union:[]string:[a b c d e]",
  215 		"intersect:[]string:[a b c d]",
  216 	)
  217 }