// Copyright 2019 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package hugolib import ( "fmt" "path/filepath" "reflect" "strings" "testing" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/markup/asciidocext" "github.com/gohugoio/hugo/markup/rst" "github.com/gohugoio/hugo/parser/pageparser" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/tpl" "github.com/spf13/cast" qt "github.com/frankban/quicktest" ) func CheckShortCodeMatch(t *testing.T, input, expected string, withTemplate func(templ tpl.TemplateManager) error) { t.Helper() CheckShortCodeMatchAndError(t, input, expected, withTemplate, false) } func CheckShortCodeMatchAndError(t *testing.T, input, expected string, withTemplate func(templ tpl.TemplateManager) error, expectError bool) { t.Helper() cfg, fs := newTestCfg() cfg.Set("markup", map[string]interface{}{ "defaultMarkdownHandler": "blackfriday", // TODO(bep) }) c := qt.New(t) // Need some front matter, see https://github.com/gohugoio/hugo/issues/2337 contentFile := `--- title: "Title" --- ` + input writeSource(t, fs, "content/simple.md", contentFile) b := newTestSitesBuilderFromDepsCfg(t, deps.DepsCfg{Fs: fs, Cfg: cfg, WithTemplate: withTemplate}).WithNothingAdded() err := b.BuildE(BuildCfg{}) if err != nil && !expectError { t.Fatalf("Shortcode rendered error %s.", err) } if expectError { c.Assert(err, qt.ErrorMatches, expected) return } h := b.H c.Assert(len(h.Sites), qt.Equals, 1) c.Assert(len(h.Sites[0].RegularPages()), qt.Equals, 1) output := strings.TrimSpace(content(h.Sites[0].RegularPages()[0])) output = strings.TrimPrefix(output, "

") output = strings.TrimSuffix(output, "

") expected = strings.TrimSpace(expected) if output != expected { t.Fatalf("Shortcode render didn't match. got \n%q but expected \n%q", output, expected) } } func TestNonSC(t *testing.T) { t.Parallel() // notice the syntax diff from 0.12, now comment delims must be added CheckShortCodeMatch(t, "{{%/* movie 47238zzb */%}}", "{{% movie 47238zzb %}}", nil) } // Issue #929 func TestHyphenatedSC(t *testing.T) { t.Parallel() wt := func(tem tpl.TemplateManager) error { tem.AddTemplate("_internal/shortcodes/hyphenated-video.html", `Playing Video {{ .Get 0 }}`) return nil } CheckShortCodeMatch(t, "{{< hyphenated-video 47238zzb >}}", "Playing Video 47238zzb", wt) } // Issue #1753 func TestNoTrailingNewline(t *testing.T) { t.Parallel() wt := func(tem tpl.TemplateManager) error { tem.AddTemplate("_internal/shortcodes/a.html", `{{ .Get 0 }}`) return nil } CheckShortCodeMatch(t, "ab{{< a c >}}d", "abcd", wt) } func TestPositionalParamSC(t *testing.T) { t.Parallel() wt := func(tem tpl.TemplateManager) error { tem.AddTemplate("_internal/shortcodes/video.html", `Playing Video {{ .Get 0 }}`) return nil } CheckShortCodeMatch(t, "{{< video 47238zzb >}}", "Playing Video 47238zzb", wt) CheckShortCodeMatch(t, "{{< video 47238zzb 132 >}}", "Playing Video 47238zzb", wt) CheckShortCodeMatch(t, "{{