shortcodes_test.go (3267B)
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
15
16 import (
17 "testing"
18
19 qt "github.com/frankban/quicktest"
20 )
21
22 func TestShortcodesTemplate(t *testing.T) {
23 t.Run("isShortcode", func(t *testing.T) {
24 c := qt.New(t)
25 c.Assert(isShortcode("shortcodes/figures.html"), qt.Equals, true)
26 c.Assert(isShortcode("_internal/shortcodes/figures.html"), qt.Equals, true)
27 c.Assert(isShortcode("shortcodes\\figures.html"), qt.Equals, false)
28 c.Assert(isShortcode("myshortcodes"), qt.Equals, false)
29 })
30
31 t.Run("variantsFromName", func(t *testing.T) {
32 c := qt.New(t)
33 c.Assert(templateVariants("figure.html"), qt.DeepEquals, []string{"", "html", "html"})
34 c.Assert(templateVariants("figure.no.html"), qt.DeepEquals, []string{"no", "no", "html"})
35 c.Assert(templateVariants("figure.no.amp.html"), qt.DeepEquals, []string{"no", "amp", "html"})
36 c.Assert(templateVariants("figure.amp.html"), qt.DeepEquals, []string{"amp", "amp", "html"})
37
38 name, variants := templateNameAndVariants("figure.html")
39 c.Assert(name, qt.Equals, "figure")
40 c.Assert(variants, qt.DeepEquals, []string{"", "html", "html"})
41 })
42
43 t.Run("compareVariants", func(t *testing.T) {
44 c := qt.New(t)
45 var s *shortcodeTemplates
46
47 tests := []struct {
48 name string
49 name1 string
50 name2 string
51 expected int
52 }{
53 {"Same suffix", "figure.html", "figure.html", 6},
54 {"Same suffix and output format", "figure.html.html", "figure.html.html", 6},
55 {"Same suffix, output format and language", "figure.no.html.html", "figure.no.html.html", 6},
56 {"No suffix", "figure", "figure", 6},
57 {"Different output format", "figure.amp.html", "figure.html.html", -1},
58 {"One with output format, one without", "figure.amp.html", "figure.html", -1},
59 }
60
61 for _, test := range tests {
62 w := s.compareVariants(templateVariants(test.name1), templateVariants(test.name2))
63 c.Assert(w, qt.Equals, test.expected)
64 }
65 })
66
67 t.Run("indexOf", func(t *testing.T) {
68 c := qt.New(t)
69
70 s := &shortcodeTemplates{
71 variants: []shortcodeVariant{
72 {variants: []string{"a", "b", "c"}},
73 {variants: []string{"a", "b", "d"}},
74 },
75 }
76
77 c.Assert(s.indexOf([]string{"a", "b", "c"}), qt.Equals, 0)
78 c.Assert(s.indexOf([]string{"a", "b", "d"}), qt.Equals, 1)
79 c.Assert(s.indexOf([]string{"a", "b", "x"}), qt.Equals, -1)
80 })
81
82 t.Run("Name", func(t *testing.T) {
83 c := qt.New(t)
84
85 c.Assert(templateBaseName(templateShortcode, "shortcodes/foo.html"), qt.Equals, "foo.html")
86 c.Assert(templateBaseName(templateShortcode, "_internal/shortcodes/foo.html"), qt.Equals, "foo.html")
87 c.Assert(templateBaseName(templateShortcode, "shortcodes/test/foo.html"), qt.Equals, "test/foo.html")
88
89 c.Assert(true, qt.Equals, true)
90 })
91 }