URI: 
       paginator_test.go - hugo - [fork] hugo port for 9front
  HTML git clone https://git.drkhsh.at/hugo.git
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
   DIR README
   DIR LICENSE
       ---
       paginator_test.go (4972B)
       ---
            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 TestPaginator(t *testing.T) {
           24         configFile := `
           25 baseURL = "https://example.com/foo/"
           26 
           27 [pagination]
           28 pagerSize = 3
           29 path = "thepage"
           30 
           31 [languages.en]
           32 weight = 1
           33 contentDir = "content/en"
           34 
           35 [languages.nn]
           36 weight = 2
           37 contentDir = "content/nn"
           38 
           39 `
           40         b := newTestSitesBuilder(t).WithConfigFile("toml", configFile)
           41         var content []string
           42         for i := range 9 {
           43                 for _, contentDir := range []string{"content/en", "content/nn"} {
           44                         content = append(content, fmt.Sprintf(contentDir+"/blog/page%d.md", i), fmt.Sprintf(`---
           45 title: Page %d
           46 ---
           47 
           48 Content.
           49 `, i))
           50                 }
           51         }
           52 
           53         b.WithContent(content...)
           54 
           55         pagTemplate := `
           56 {{ $pag := $.Paginator }}
           57 Total: {{ $pag.TotalPages }}
           58 First: {{ $pag.First.URL }}
           59 Page Number: {{ $pag.PageNumber }}
           60 URL: {{ $pag.URL }}
           61 {{ with $pag.Next }}Next: {{ .URL }}{{ end }}
           62 {{ with $pag.Prev }}Prev: {{ .URL }}{{ end }}
           63 {{ range $i, $e := $pag.Pagers }}
           64 {{ printf "%d: %d/%d  %t" $i $pag.PageNumber .PageNumber (eq . $pag) -}}
           65 {{ end }}
           66 `
           67 
           68         b.WithTemplatesAdded("index.html", pagTemplate)
           69         b.WithTemplatesAdded("index.xml", pagTemplate)
           70 
           71         b.Build(BuildCfg{})
           72 
           73         b.AssertFileContent("public/index.html",
           74                 "Page Number: 1",
           75                 "0: 1/1  true")
           76 
           77         b.AssertFileContent("public/thepage/2/index.html",
           78                 "Total: 3",
           79                 "Page Number: 2",
           80                 "URL: /foo/thepage/2/",
           81                 "Next: /foo/thepage/3/",
           82                 "Prev: /foo/",
           83                 "1: 2/2  true",
           84         )
           85 
           86         b.AssertFileContent("public/index.xml",
           87                 "Page Number: 1",
           88                 "0: 1/1  true")
           89         b.AssertFileContent("public/thepage/2/index.xml",
           90                 "Page Number: 2",
           91                 "1: 2/2  true")
           92 
           93         b.AssertFileContent("public/nn/index.html",
           94                 "Page Number: 1",
           95                 "0: 1/1  true")
           96 
           97         b.AssertFileContent("public/nn/index.xml",
           98                 "Page Number: 1",
           99                 "0: 1/1  true")
          100 }
          101 
          102 // Issue 6023
          103 func TestPaginateWithSort(t *testing.T) {
          104         files := `
          105 -- hugo.toml --
          106 -- content/a/a.md --
          107 -- content/z/b.md --
          108 -- content/x/b.md --
          109 -- content/x/a.md --
          110 -- layouts/home.html --
          111 Paginate: {{ range (.Paginate (sort .Site.RegularPages ".File.Filename" "desc")).Pages }}|{{ .Path }}{{ end }}
          112 `
          113         b := Test(t, files)
          114 
          115         b.AssertFileContent("public/index.html", "Paginate: |/z/b|/x/b|/x/a|/a/a")
          116 }
          117 
          118 // https://github.com/gohugoio/hugo/issues/6797
          119 func TestPaginateOutputFormat(t *testing.T) {
          120         b := newTestSitesBuilder(t).WithSimpleConfigFile()
          121         b.WithContent("_index.md", `---
          122 title: "Home"
          123 cascade:
          124   outputs:
          125     - JSON
          126 ---`)
          127 
          128         for i := range 22 {
          129                 b.WithContent(fmt.Sprintf("p%d.md", i+1), fmt.Sprintf(`---
          130 title: "Page"
          131 weight: %d
          132 ---`, i+1))
          133         }
          134 
          135         b.WithTemplatesAdded("index.json", `JSON: {{ .Paginator.TotalNumberOfElements }}: {{ range .Paginator.Pages }}|{{ .RelPermalink }}{{ end }}:DONE`)
          136         b.Build(BuildCfg{})
          137 
          138         b.AssertFileContent("public/index.json",
          139                 `JSON: 22
          140 |/p1/index.json|/p2/index.json|
          141 `)
          142 
          143         // This looks odd, so are most bugs.
          144         b.Assert(b.CheckExists("public/page/1/index.json/index.html"), qt.Equals, false)
          145         b.Assert(b.CheckExists("public/page/1/index.json"), qt.Equals, false)
          146         b.AssertFileContent("public/page/2/index.json", `JSON: 22: |/p11/index.json|/p12/index.json`)
          147 }
          148 
          149 // Issue 10802
          150 func TestPaginatorEmptyPageGroups(t *testing.T) {
          151         t.Parallel()
          152 
          153         files := `
          154 -- config.toml --
          155 baseURL = "https://example.com/"
          156 -- content/p1.md --
          157 -- content/p2.md --
          158 -- layouts/index.html --
          159 {{ $empty := site.RegularPages | complement site.RegularPages }}
          160 Len: {{ len $empty }}: Type: {{ printf "%T" $empty }}
          161 {{ $pgs := $empty.GroupByPublishDate "January 2006" }}
          162 {{ $pag := .Paginate $pgs }}
          163 Len Pag: {{ len $pag.Pages }}
          164 `
          165         b := Test(t, files)
          166 
          167         b.AssertFileContent("public/index.html", "Len: 0", "Len Pag: 0")
          168 }
          169 
          170 func TestPaginatorNodePagesOnly(t *testing.T) {
          171         files := `
          172 -- hugo.toml --
          173 [pagination]
          174 pagerSize = 1
          175 -- content/p1.md --
          176 -- layouts/_default/single.html --
          177 Paginator: {{ .Paginator }}
          178 `
          179         b, err := TestE(t, files)
          180         b.Assert(err, qt.IsNotNil)
          181         b.Assert(err.Error(), qt.Contains, `error calling Paginator: pagination not supported for this page: kind: "page"`)
          182 }
          183 
          184 func TestNilPointerErrorMessage(t *testing.T) {
          185         files := `
          186 -- hugo.toml --        
          187 -- content/p1.md --
          188 -- layouts/_default/single.html --
          189 Home Filename: {{ site.Home.File.Filename }}
          190 `
          191         b, err := TestE(t, files)
          192         b.Assert(err, qt.IsNotNil)
          193         b.Assert(err.Error(), qt.Contains, `single.html:1:22: executing "single.html" – File is nil; wrap it in if or with: {{ with site.Home.File }}{{ .Filename }}{{ end }}`)
          194 }