github.com/neohugo/neohugo@v0.123.8/tpl/tplimpl/template_funcs_test.go (about)

     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 tplimpl_test
    15  
    16  import (
    17  	"fmt"
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/neohugo/neohugo/hugolib"
    22  
    23  	"github.com/neohugo/neohugo/tpl/internal"
    24  )
    25  
    26  func TestTemplateFuncsExamples(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	files := `
    30  -- config.toml --
    31  disableKinds=["home", "section", "taxonomy", "term", "sitemap", "robotsTXT"]
    32  ignoreErrors = ["my-err-id"]
    33  [outputs]
    34  home=["HTML"]
    35  -- layouts/partials/header.html --
    36  <title>Hugo Rocks!</title>
    37  -- files/README.txt --
    38  Hugo Rocks!
    39  -- content/blog/hugo-rocks.md --
    40  ---
    41  title: "**BatMan**"
    42  ---
    43  `
    44  
    45  	b := hugolib.NewIntegrationTestBuilder(
    46  		hugolib.IntegrationTestConfig{
    47  			T:           t,
    48  			TxtarString: files,
    49  			NeedsOsFS:   true,
    50  		},
    51  	).Build()
    52  
    53  	d := b.H.Sites[0].Deps
    54  
    55  	var (
    56  		templates []string
    57  		expected  []string
    58  	)
    59  
    60  	for _, nsf := range internal.TemplateFuncsNamespaceRegistry {
    61  		ns := nsf(d)
    62  		for _, mm := range ns.MethodMappings {
    63  			for _, example := range mm.Examples {
    64  				// These will fail the build, so skip.
    65  				if strings.Contains(example[0], "errorf") ||
    66  					strings.Contains(example[0], "transform.XMLEscape") ||
    67  					strings.Contains(example[0], "math.Rand") {
    68  					continue
    69  				}
    70  				templates = append(templates, example[0])
    71  				expected = append(expected, example[1])
    72  			}
    73  		}
    74  	}
    75  
    76  	files += fmt.Sprintf("-- layouts/_default/single.html --\n%s\n", strings.Join(templates, "\n"))
    77  	b = hugolib.NewIntegrationTestBuilder(
    78  		hugolib.IntegrationTestConfig{
    79  			T:           t,
    80  			TxtarString: files,
    81  			NeedsOsFS:   true,
    82  		},
    83  	).Build()
    84  
    85  	b.AssertFileContent("public/blog/hugo-rocks/index.html", expected...)
    86  }