github.com/dirkolbrich/hugo@v0.47.1/tpl/transform/transform_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 transform
    15  
    16  import (
    17  	"fmt"
    18  	"html/template"
    19  	"testing"
    20  
    21  	"github.com/gohugoio/hugo/config"
    22  	"github.com/gohugoio/hugo/deps"
    23  	"github.com/gohugoio/hugo/helpers"
    24  	"github.com/gohugoio/hugo/hugofs"
    25  	"github.com/gohugoio/hugo/langs"
    26  	"github.com/spf13/viper"
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  type tstNoStringer struct{}
    32  
    33  func TestEmojify(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	v := viper.New()
    37  	v.Set("contentDir", "content")
    38  	ns := New(newDeps(v))
    39  
    40  	for i, test := range []struct {
    41  		s      interface{}
    42  		expect interface{}
    43  	}{
    44  		{":notamoji:", template.HTML(":notamoji:")},
    45  		{"I :heart: Hugo", template.HTML("I ❤️ Hugo")},
    46  		// errors
    47  		{tstNoStringer{}, false},
    48  	} {
    49  		errMsg := fmt.Sprintf("[%d] %s", i, test.s)
    50  
    51  		result, err := ns.Emojify(test.s)
    52  
    53  		if b, ok := test.expect.(bool); ok && !b {
    54  			require.Error(t, err, errMsg)
    55  			continue
    56  		}
    57  
    58  		require.NoError(t, err, errMsg)
    59  		assert.Equal(t, test.expect, result, errMsg)
    60  	}
    61  }
    62  
    63  func TestHighlight(t *testing.T) {
    64  	t.Parallel()
    65  
    66  	v := viper.New()
    67  	v.Set("contentDir", "content")
    68  	ns := New(newDeps(v))
    69  
    70  	for i, test := range []struct {
    71  		s      interface{}
    72  		lang   string
    73  		opts   string
    74  		expect interface{}
    75  	}{
    76  		{"func boo() {}", "go", "", "boo"},
    77  		// Issue #4179
    78  		{`<Foo attr=" &lt; "></Foo>`, "xml", "", `&amp;lt;`},
    79  		{tstNoStringer{}, "go", "", false},
    80  	} {
    81  		errMsg := fmt.Sprintf("[%d]", i)
    82  
    83  		result, err := ns.Highlight(test.s, test.lang, test.opts)
    84  
    85  		if b, ok := test.expect.(bool); ok && !b {
    86  			require.Error(t, err, errMsg)
    87  			continue
    88  		}
    89  
    90  		require.NoError(t, err, errMsg)
    91  		assert.Contains(t, result, test.expect.(string), errMsg)
    92  	}
    93  }
    94  
    95  func TestHTMLEscape(t *testing.T) {
    96  	t.Parallel()
    97  
    98  	v := viper.New()
    99  	v.Set("contentDir", "content")
   100  	ns := New(newDeps(v))
   101  
   102  	for i, test := range []struct {
   103  		s      interface{}
   104  		expect interface{}
   105  	}{
   106  		{`"Foo & Bar's Diner" <y@z>`, `&#34;Foo &amp; Bar&#39;s Diner&#34; &lt;y@z&gt;`},
   107  		{"Hugo & Caddy > Wordpress & Apache", "Hugo &amp; Caddy &gt; Wordpress &amp; Apache"},
   108  		// errors
   109  		{tstNoStringer{}, false},
   110  	} {
   111  		errMsg := fmt.Sprintf("[%d] %s", i, test.s)
   112  
   113  		result, err := ns.HTMLEscape(test.s)
   114  
   115  		if b, ok := test.expect.(bool); ok && !b {
   116  			require.Error(t, err, errMsg)
   117  			continue
   118  		}
   119  
   120  		require.NoError(t, err, errMsg)
   121  		assert.Equal(t, test.expect, result, errMsg)
   122  	}
   123  }
   124  
   125  func TestHTMLUnescape(t *testing.T) {
   126  	t.Parallel()
   127  
   128  	v := viper.New()
   129  	v.Set("contentDir", "content")
   130  	ns := New(newDeps(v))
   131  
   132  	for i, test := range []struct {
   133  		s      interface{}
   134  		expect interface{}
   135  	}{
   136  		{`&quot;Foo &amp; Bar&#39;s Diner&quot; &lt;y@z&gt;`, `"Foo & Bar's Diner" <y@z>`},
   137  		{"Hugo &amp; Caddy &gt; Wordpress &amp; Apache", "Hugo & Caddy > Wordpress & Apache"},
   138  		// errors
   139  		{tstNoStringer{}, false},
   140  	} {
   141  		errMsg := fmt.Sprintf("[%d] %s", i, test.s)
   142  
   143  		result, err := ns.HTMLUnescape(test.s)
   144  
   145  		if b, ok := test.expect.(bool); ok && !b {
   146  			require.Error(t, err, errMsg)
   147  			continue
   148  		}
   149  
   150  		require.NoError(t, err, errMsg)
   151  		assert.Equal(t, test.expect, result, errMsg)
   152  	}
   153  }
   154  
   155  func TestMarkdownify(t *testing.T) {
   156  	t.Parallel()
   157  
   158  	v := viper.New()
   159  	v.Set("contentDir", "content")
   160  	ns := New(newDeps(v))
   161  
   162  	for i, test := range []struct {
   163  		s      interface{}
   164  		expect interface{}
   165  	}{
   166  		{"Hello **World!**", template.HTML("Hello <strong>World!</strong>")},
   167  		{[]byte("Hello Bytes **World!**"), template.HTML("Hello Bytes <strong>World!</strong>")},
   168  		{tstNoStringer{}, false},
   169  	} {
   170  		errMsg := fmt.Sprintf("[%d] %s", i, test.s)
   171  
   172  		result, err := ns.Markdownify(test.s)
   173  
   174  		if b, ok := test.expect.(bool); ok && !b {
   175  			require.Error(t, err, errMsg)
   176  			continue
   177  		}
   178  
   179  		require.NoError(t, err, errMsg)
   180  		assert.Equal(t, test.expect, result, errMsg)
   181  	}
   182  }
   183  
   184  // Issue #3040
   185  func TestMarkdownifyBlocksOfText(t *testing.T) {
   186  	t.Parallel()
   187  
   188  	assert := require.New(t)
   189  
   190  	v := viper.New()
   191  	v.Set("contentDir", "content")
   192  	ns := New(newDeps(v))
   193  
   194  	text := `
   195  #First 
   196  
   197  This is some *bold* text.
   198  
   199  ## Second
   200  
   201  This is some more text.
   202  
   203  And then some.
   204  `
   205  
   206  	result, err := ns.Markdownify(text)
   207  	assert.NoError(err)
   208  	assert.Equal(template.HTML(
   209  		"<p>#First</p>\n\n<p>This is some <em>bold</em> text.</p>\n\n<h2 id=\"second\">Second</h2>\n\n<p>This is some more text.</p>\n\n<p>And then some.</p>\n"),
   210  		result)
   211  
   212  }
   213  
   214  func TestPlainify(t *testing.T) {
   215  	t.Parallel()
   216  
   217  	v := viper.New()
   218  	v.Set("contentDir", "content")
   219  	ns := New(newDeps(v))
   220  
   221  	for i, test := range []struct {
   222  		s      interface{}
   223  		expect interface{}
   224  	}{
   225  		{"<em>Note:</em> blah <b>blah</b>", "Note: blah blah"},
   226  		// errors
   227  		{tstNoStringer{}, false},
   228  	} {
   229  		errMsg := fmt.Sprintf("[%d] %s", i, test.s)
   230  
   231  		result, err := ns.Plainify(test.s)
   232  
   233  		if b, ok := test.expect.(bool); ok && !b {
   234  			require.Error(t, err, errMsg)
   235  			continue
   236  		}
   237  
   238  		require.NoError(t, err, errMsg)
   239  		assert.Equal(t, test.expect, result, errMsg)
   240  	}
   241  }
   242  
   243  func newDeps(cfg config.Provider) *deps.Deps {
   244  	l := langs.NewLanguage("en", cfg)
   245  	l.Set("i18nDir", "i18n")
   246  	cs, err := helpers.NewContentSpec(l)
   247  	if err != nil {
   248  		panic(err)
   249  	}
   250  
   251  	return &deps.Deps{
   252  		Cfg:         cfg,
   253  		Fs:          hugofs.NewMem(l),
   254  		ContentSpec: cs,
   255  	}
   256  }