tpl/tplimpl: Improve shortcode test coverage

This commit is contained in:
Joe Mooring 2025-01-23 15:35:20 -08:00 committed by Bjørn Erik Pedersen
parent f42a4b6af5
commit 873a5cda1a
2 changed files with 332 additions and 93 deletions

View file

@ -1,93 +0,0 @@
// 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 (
"testing"
"github.com/gohugoio/hugo/htesting"
)
func TestEmbeddedShortcodes(t *testing.T) {
if !htesting.IsCI() {
t.Skip("skip on non-CI for now")
}
t.Run("with theme", func(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
baseURL = "https://example.com"
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT", "page", "section"]
ignoreErrors = ["error-missing-instagram-accesstoken"]
[params]
foo = "bar"
-- content/_index.md --
---
title: "Home"
---
## Figure
{{< figure src="image.png" >}}
## Gist
{{< gist spf13 7896402 >}}
## Highlight
{{< highlight go >}}
package main
{{< /highlight >}}
## Instagram
{{< instagram BWNjjyYFxVx >}}
## Tweet
{{< tweet user="1626985695280603138" id="877500564405444608" >}}
## Vimeo
{{< vimeo 20097015 >}}
## YouTube
{{< youtube 0RKpf3rK57I >}}
## Param
Foo: {{< param foo >}}
-- layouts/index.html --
Content: {{ .Content }}|
`
b := Test(t, files)
b.AssertFileContent("public/index.html", `
<figure>
https://gist.github.com/spf13/7896402.js
<span style="color:#a6e22e">main</span></span>
https://t.co/X94FmYDEZJ
https://www.youtube.com/embed/0RKpf3rK57I
Foo: bar
`)
})
}

View file

@ -90,6 +90,40 @@ E: An _emphasized_ word.
) )
} }
func TestFigureShortcode(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
-- content/_index.md --
---
title: home
---
{{< figure
src="a.jpg"
alt="alternate text"
width=600
height=400
loading="lazy"
class="my-class"
link="https://example.org"
target="_blank"
rel="noopener"
title="my-title"
caption="a **bold** word"
attr="an _emphasized_ word"
attrlink="https://example.org/foo"
>}}
-- layouts/index.html --
Hash: {{ .Content | hash.XxHash }}
Content: {{ .Content }}
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/index.html", "35b077dcb9887a84")
}
func TestGistShortcode(t *testing.T) { func TestGistShortcode(t *testing.T) {
t.Parallel() t.Parallel()
@ -110,6 +144,99 @@ title: home
b.AssertLogContains(`WARN The "gist" shortcode was deprecated in v0.143.0 and will be removed in a future release. See https://gohugo.io/shortcodes/gist for instructions to create a replacement.`) b.AssertLogContains(`WARN The "gist" shortcode was deprecated in v0.143.0 and will be removed in a future release. See https://gohugo.io/shortcodes/gist for instructions to create a replacement.`)
} }
func TestHighlightShortcode(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
-- layouts/_default/single.html --
Hash: {{ .Content | hash.XxHash }}
Content: {{ .Content }}
-- content/p1.md --
---
title: p1
---
{{< highlight go >}}
func main() {
for i := 0; i < 3; i++ {
fmt.Println("Value of i:", i)
}
}
{{< /highlight >}}
-- content/p2.md --
---
title: p2
---
{{< highlight go "noClasses=false" >}}
func main() {
for i := 0; i < 3; i++ {
fmt.Println("Value of i:", i)
}
}
{{< /highlight >}}
-- content/p3.md --
---
title: p3
---
{{< highlight go "lineNos=inline" >}}
func main() {
for i := 0; i < 3; i++ {
fmt.Println("Value of i:", i)
}
}
{{< /highlight >}}
-- content/p4.md --
---
title: p4
---
{{< highlight go "lineNos=table" >}}
func main() {
for i := 0; i < 3; i++ {
fmt.Println("Value of i:", i)
}
}
{{< /highlight >}}
-- content/p5.md --
---
title: p5
---
{{< highlight go "anchorLineNos=true, hl_Lines=2-4, lineAnchors=foo, lineNoStart=42, lineNos=true, lineNumbersInTable=false, style=emacs, wrapperClass=my-class" >}}
func main() {
for i := 0; i < 3; i++ {
fmt.Println("Value of i:", i)
}
}
{{< /highlight >}}
-- content/p6.md --
---
title: p6
---
{{< highlight go "anchorlinenos=true, hl_lines=2-4, lineanchors=foo, linenostart=42, linenos=true, linenumbersintable=false, style=emacs, wrapperclass=my-class" >}}
func main() {
for i := 0; i < 3; i++ {
fmt.Println("Value of i:", i)
}
}
{{< /highlight >}}
-- content/p7.md --
---
title: p7
---
An inline {{< highlight go "hl_inline=true" >}}fmt.Println("Value of i:", i)Hello world!{{< /highlight >}} statement.
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/p1/index.html", "576ee13be18ddba2")
b.AssertFileContent("public/p2/index.html", "8774e8b4bf60aa9e")
b.AssertFileContent("public/p3/index.html", "7634b47df1859f58")
b.AssertFileContent("public/p4/index.html", "385a15e400df4e39")
b.AssertFileContent("public/p5/index.html", "b3a73f3eddc6e0c1")
b.AssertFileContent("public/p6/index.html", "b3a73f3eddc6e0c1")
b.AssertFileContent("public/p7/index.html", "f12eeaa4d6d9c7ac")
}
func TestInstagramShortcode(t *testing.T) { func TestInstagramShortcode(t *testing.T) {
t.Parallel() t.Parallel()
@ -137,6 +264,35 @@ Content: {{ .Content }}
b.AssertFileContent("public/index.html", "2c1dce3881be0513") b.AssertFileContent("public/index.html", "2c1dce3881be0513")
} }
func TestParamShortcode(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
[params]
b = 2
-- layouts/index.html --
{{ .Content }}
-- content/_index.md --
---
title: home
params:
a: 1
---
A: {{% param "a" %}}
B: {{% param "b" %}}
`
b := hugolib.Test(t, files)
b.AssertFileExists("public/index.html", true)
b.AssertFileContent("public/index.html",
"A: 1",
"B: 2",
)
}
func TestQRShortcode(t *testing.T) { func TestQRShortcode(t *testing.T) {
t.Parallel() t.Parallel()
@ -173,6 +329,136 @@ https://gohugo.io"
) )
} }
func TestRefShortcode(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
baseURL = 'https://example.org/'
disableKinds = ['rss','section','sitemap','taxonomy','term']
defaultContentLanguageInSubdir = true
[markup.goldmark.extensions]
linkify = false
[languages.en]
weight = 1
[languages.de]
weight = 2
[outputs]
page = ['html','json']
-- layouts/_default/home.html --
{{ .Content }}
-- layouts/_default/single.html.html --
{{ .Title }}
-- layouts/_default/single.json.json --
{{ .Title }}
-- content/_index.en.md --
---
title: home
---
A: {{% ref "/s1/p1.md" %}}
B: {{% ref path="/s1/p1.md" %}}
C: {{% ref path="/s1/p1.md" lang="en" %}}
D: {{% ref path="/s1/p1.md" lang="de" %}}
E: {{% ref path="/s1/p1.md" lang="en" outputFormat="html" %}}
F: {{% ref path="/s1/p1.md" lang="de" outputFormat="html" %}}
G: {{% ref path="/s1/p1.md" lang="en" outputFormat="json" %}}
H: {{% ref path="/s1/p1.md" lang="de" outputFormat="json" %}}
-- content/s1/p1.en.md --
---
title: p1 (en)
---
-- content/s1/p1.de.md --
---
title: p1 (de)
---
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/en/index.html",
`p>A: https://example.org/en/s1/p1/</p>`,
`p>B: https://example.org/en/s1/p1/</p>`,
`p>C: https://example.org/en/s1/p1/</p>`,
`p>D: https://example.org/de/s1/p1/</p>`,
`p>E: https://example.org/en/s1/p1/</p>`,
`p>F: https://example.org/de/s1/p1/</p>`,
`p>G: https://example.org/en/s1/p1/index.json</p>`,
`p>H: https://example.org/de/s1/p1/index.json</p>`,
)
}
func TestRelRefShortcode(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
baseURL = 'https://example.org/'
disableKinds = ['rss','section','sitemap','taxonomy','term']
defaultContentLanguageInSubdir = true
[markup.goldmark.extensions]
linkify = false
[languages.en]
weight = 1
[languages.de]
weight = 2
[outputs]
page = ['html','json']
-- layouts/_default/home.html --
{{ .Content }}
-- layouts/_default/single.html.html --
{{ .Title }}
-- layouts/_default/single.json.json --
{{ .Title }}
-- content/_index.en.md --
---
title: home
---
A: {{% relref "/s1/p1.md" %}}
B: {{% relref path="/s1/p1.md" %}}
C: {{% relref path="/s1/p1.md" lang="en" %}}
D: {{% relref path="/s1/p1.md" lang="de" %}}
E: {{% relref path="/s1/p1.md" lang="en" outputFormat="html" %}}
F: {{% relref path="/s1/p1.md" lang="de" outputFormat="html" %}}
G: {{% relref path="/s1/p1.md" lang="en" outputFormat="json" %}}
H: {{% relref path="/s1/p1.md" lang="de" outputFormat="json" %}}
-- content/s1/p1.en.md --
---
title: p1 (en)
---
-- content/s1/p1.de.md --
---
title: p1 (de)
---
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/en/index.html",
`p>A: /en/s1/p1/</p>`,
`p>B: /en/s1/p1/</p>`,
`p>C: /en/s1/p1/</p>`,
`p>D: /de/s1/p1/</p>`,
`p>E: /en/s1/p1/</p>`,
`p>F: /de/s1/p1/</p>`,
`p>G: /en/s1/p1/index.json</p>`,
`p>H: /de/s1/p1/index.json</p>`,
)
}
func TestVimeoShortcode(t *testing.T) { func TestVimeoShortcode(t *testing.T) {
t.Parallel() t.Parallel()
@ -352,3 +638,49 @@ title: home
`WARN The "twitter_simple" shortcode was unable to retrieve the remote data.`, `WARN The "twitter_simple" shortcode was unable to retrieve the remote data.`,
) )
} }
func TestYouTubeShortcode(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
privacy.youtube.privacyEnhanced = false
-- layouts/_default/single.html --
Hash: {{ .Content | hash.XxHash }}
Content: {{ .Content }}
-- content/p1.md --
---
title: p1
---
{{< youtube 0RKpf3rK57I >}}
-- content/p2.md --
---
title: p2
---
{{< youtube
id="0RKpf3rK57I"
allowFullScreen=false
autoplay=true
class="my-class"
controls="false"
end=42
loading="lazy"
loop=true
mute=true
start=6
title="my title"
>}}
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/p1/index.html", "515600e76b272f51")
b.AssertFileContent("public/p2/index.html", "b5ceeace7dfa797a")
files = strings.ReplaceAll(files, "privacy.youtube.privacyEnhanced = false", "privacy.youtube.privacyEnhanced = true")
b = hugolib.Test(t, files)
b.AssertFileContent("public/p1/index.html", "e92c7f4b768d7e23")
b.AssertFileContent("public/p2/index.html", "c384e83e035b71d9")
}