github.com/kristoff-it/hugo@v0.47.1/hugolib/template_engines_test.go (about)

     1  // Copyright 2017 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  	"path/filepath"
    19  	"testing"
    20  
    21  	"strings"
    22  
    23  	"github.com/gohugoio/hugo/deps"
    24  )
    25  
    26  func TestAllTemplateEngines(t *testing.T) {
    27  	t.Parallel()
    28  	noOp := func(s string) string {
    29  		return s
    30  	}
    31  
    32  	amberFixer := func(s string) string {
    33  		fixed := strings.Replace(s, "{{ .Title", "{{ Title", -1)
    34  		fixed = strings.Replace(fixed, ".Content", "Content", -1)
    35  		fixed = strings.Replace(fixed, ".IsNamedParams", "IsNamedParams", -1)
    36  		fixed = strings.Replace(fixed, "{{", "#{", -1)
    37  		fixed = strings.Replace(fixed, "}}", "}", -1)
    38  		fixed = strings.Replace(fixed, `title "hello world"`, `title("hello world")`, -1)
    39  
    40  		return fixed
    41  	}
    42  
    43  	for _, config := range []struct {
    44  		suffix        string
    45  		templateFixer func(s string) string
    46  	}{
    47  		{"amber", amberFixer},
    48  		{"html", noOp},
    49  		{"ace", noOp},
    50  	} {
    51  		t.Run(config.suffix,
    52  			func(t *testing.T) {
    53  				doTestTemplateEngine(t, config.suffix, config.templateFixer)
    54  			})
    55  	}
    56  
    57  }
    58  
    59  func doTestTemplateEngine(t *testing.T, suffix string, templateFixer func(s string) string) {
    60  
    61  	cfg, fs := newTestCfg()
    62  
    63  	t.Log("Testing", suffix)
    64  
    65  	templTemplate := `
    66  p
    67  	|
    68  	| Page Title: {{ .Title }}
    69  	br
    70  	| Page Content: {{ .Content }}
    71  	br
    72  	| {{ title "hello world" }}
    73  
    74  `
    75  
    76  	templShortcodeTemplate := `
    77  p
    78  	|
    79  	| Shortcode: {{ .IsNamedParams }}
    80  `
    81  
    82  	templ := templateFixer(templTemplate)
    83  	shortcodeTempl := templateFixer(templShortcodeTemplate)
    84  
    85  	writeSource(t, fs, filepath.Join("content", "p.md"), `
    86  ---
    87  title: My Title 
    88  ---
    89  My Content
    90  
    91  Shortcode: {{< myShort >}}
    92  
    93  `)
    94  
    95  	writeSource(t, fs, filepath.Join("layouts", "_default", fmt.Sprintf("single.%s", suffix)), templ)
    96  	writeSource(t, fs, filepath.Join("layouts", "shortcodes", fmt.Sprintf("myShort.%s", suffix)), shortcodeTempl)
    97  
    98  	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
    99  	th := testHelper{s.Cfg, s.Fs, t}
   100  
   101  	th.assertFileContent(filepath.Join("public", "p", "index.html"),
   102  		"Page Title: My Title",
   103  		"My Content",
   104  		"Hello World",
   105  		"Shortcode: false",
   106  	)
   107  
   108  }