github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/tpl/collections/integration_test.go (about)

     1  // Copyright 2022 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  	"testing"
    18  
    19  	"github.com/gohugoio/hugo/hugolib"
    20  )
    21  
    22  // Issue 9585
    23  func TestApplyWithContext(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	files := `
    27  -- config.toml --
    28  baseURL = 'http://example.com/'
    29  -- layouts/index.html --
    30  {{ apply (seq 3) "partial" "foo.html"}}
    31  -- layouts/partials/foo.html --
    32  {{ return "foo"}}
    33    `
    34  
    35  	b := hugolib.NewIntegrationTestBuilder(
    36  		hugolib.IntegrationTestConfig{
    37  			T:           t,
    38  			TxtarString: files,
    39  		},
    40  	).Build()
    41  
    42  	b.AssertFileContent("public/index.html", `
    43  	[foo foo foo]
    44  `)
    45  }
    46  
    47  // Issue 9865
    48  func TestSortStable(t *testing.T) {
    49  	t.Parallel()
    50  
    51  	files := `
    52  -- config.toml --
    53  -- layouts/index.html --
    54  {{ $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) }}
    55  Asc:  {{ sort (sort $values "b" "asc") "a" "asc" }}
    56  Desc: {{ sort (sort $values "b" "desc") "a" "desc" }}
    57  
    58    `
    59  
    60  	for i := 0; i < 4; i++ {
    61  
    62  		b := hugolib.NewIntegrationTestBuilder(
    63  			hugolib.IntegrationTestConfig{
    64  				T:           t,
    65  				TxtarString: files,
    66  			},
    67  		).Build()
    68  
    69  		b.AssertFileContent("public/index.html", `
    70  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]]
    71  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]]
    72  `)
    73  
    74  	}
    75  }
    76  
    77  // Issue #11004.
    78  func TestAppendSliceToASliceOfSlices(t *testing.T) {
    79  	t.Parallel()
    80  
    81  	files := `
    82  -- hugo.toml --
    83  -- layouts/index.html --
    84  {{ $obj := slice (slice "a") }}
    85  {{ $obj = $obj | append (slice "b") }}
    86  {{ $obj = $obj | append (slice "c") }}
    87  
    88  {{ $obj }}
    89  
    90    `
    91  
    92  	b := hugolib.NewIntegrationTestBuilder(
    93  		hugolib.IntegrationTestConfig{
    94  			T:           t,
    95  			TxtarString: files,
    96  		},
    97  	).Build()
    98  
    99  	b.AssertFileContent("public/index.html", "[[a] [b] [c]]")
   100  
   101  }
   102  
   103  func TestAppendNilToSlice(t *testing.T) {
   104  
   105  	t.Parallel()
   106  
   107  	files := `
   108  -- hugo.toml --
   109  -- layouts/index.html --
   110  {{ $obj := (slice "a") }}
   111  {{ $obj = $obj | append nil }}
   112  
   113  {{ $obj }}
   114  
   115  
   116    `
   117  
   118  	b := hugolib.NewIntegrationTestBuilder(
   119  		hugolib.IntegrationTestConfig{
   120  			T:           t,
   121  			TxtarString: files,
   122  		},
   123  	).Build()
   124  
   125  	b.AssertFileContent("public/index.html", "[a &lt;nil&gt;]")
   126  
   127  }
   128  
   129  func TestAppendNilsToSliceWithNils(t *testing.T) {
   130  
   131  	t.Parallel()
   132  
   133  	files := `
   134  -- hugo.toml --
   135  -- layouts/index.html --
   136  {{ $obj := (slice "a" nil "c") }}
   137  {{ $obj = $obj | append nil }}
   138  
   139  {{ $obj }}
   140  
   141  
   142    `
   143  
   144  	for i := 0; i < 4; i++ {
   145  
   146  		b := hugolib.NewIntegrationTestBuilder(
   147  			hugolib.IntegrationTestConfig{
   148  				T:           t,
   149  				TxtarString: files,
   150  			},
   151  		).Build()
   152  
   153  		b.AssertFileContent("public/index.html", "[a &lt;nil&gt; c &lt;nil&gt;]")
   154  
   155  	}
   156  
   157  }
   158  
   159  // Issue 11234.
   160  func TestWhereWithWordCount(t *testing.T) {
   161  	t.Parallel()
   162  
   163  	files := `
   164  -- config.toml --
   165  baseURL = 'http://example.com/'
   166  -- layouts/index.html --
   167  Home: {{ range where site.RegularPages "WordCount" "gt" 50 }}{{ .Title }}|{{ end }}
   168  -- layouts/shortcodes/lorem.html --
   169  {{ "ipsum " | strings.Repeat (.Get 0 | int) }}
   170  
   171  -- content/p1.md --
   172  ---
   173  title: "p1"
   174  ---
   175  {{< lorem 100 >}}
   176  -- content/p2.md --
   177  ---
   178  title: "p2"
   179  ---
   180  {{< lorem 20 >}}
   181  -- content/p3.md --
   182  ---
   183  title: "p3"
   184  ---
   185  {{< lorem 60 >}}
   186    `
   187  
   188  	b := hugolib.NewIntegrationTestBuilder(
   189  		hugolib.IntegrationTestConfig{
   190  			T:           t,
   191  			TxtarString: files,
   192  		},
   193  	).Build()
   194  
   195  	b.AssertFileContent("public/index.html", `
   196  Home: p1|p3|
   197  `)
   198  }
   199  
   200  // Issue #11279
   201  func TestWhereLikeOperator(t *testing.T) {
   202  	t.Parallel()
   203  	files := `
   204  -- content/p1.md --
   205  ---
   206  title: P1
   207  foo: ab
   208  ---
   209  -- content/p2.md --
   210  ---
   211  title: P2
   212  foo: abc
   213  ---
   214  -- content/p3.md --
   215  ---
   216  title: P3
   217  foo: bc
   218  ---
   219  -- layouts/index.html --
   220  <ul>
   221    {{- range where site.RegularPages "Params.foo" "like" "^ab" -}}
   222      <li>{{ .Title }}</li>
   223    {{- end -}}
   224  </ul>
   225    `
   226  	b := hugolib.NewIntegrationTestBuilder(
   227  		hugolib.IntegrationTestConfig{
   228  			T:           t,
   229  			TxtarString: files,
   230  		},
   231  	).Build()
   232  	b.AssertFileContent("public/index.html", "<ul><li>P1</li><li>P2</li></ul>")
   233  }
   234  
   235  // Issue #11498
   236  func TestEchoParams(t *testing.T) {
   237  	t.Parallel()
   238  	files := `
   239  -- hugo.toml --
   240  [params.footer]
   241  string = 'foo'
   242  int = 42
   243  float = 3.1415
   244  boolt = true
   245  boolf = false
   246  -- layouts/index.html --
   247  {{ echoParam .Site.Params.footer "string" }}
   248  {{ echoParam .Site.Params.footer "int" }}
   249  {{ echoParam .Site.Params.footer "float" }}
   250  {{ echoParam .Site.Params.footer "boolt" }}
   251  {{ echoParam .Site.Params.footer "boolf" }}
   252  	`
   253  
   254  	b := hugolib.NewIntegrationTestBuilder(
   255  		hugolib.IntegrationTestConfig{
   256  			T:           t,
   257  			TxtarString: files,
   258  		},
   259  	).Build()
   260  	b.AssertFileContent("public/index.html",
   261  		"foo",
   262  		"42",
   263  		"3.1415",
   264  		"true",
   265  		"false",
   266  	)
   267  }