`, wt)
}
func TestInnerSC(t *testing.T) {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("inside.html", `
{{ .Inner }}
`)
return nil
}
CheckShortCodeMatch(t, `{{< inside class="aspen" >}}`, `
`, wt)
CheckShortCodeMatch(t, `{{< inside class="aspen" >}}More Here{{< /inside >}}`, "
More Here
", wt)
CheckShortCodeMatch(t, `{{< inside >}}More Here{{< /inside >}}`, "
More Here
", wt)
}
func TestInnerSCWithMarkdown(t *testing.T) {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("inside.html", `
{{ .Inner }}
`)
return nil
}
CheckShortCodeMatch(t, `{{% inside %}}
# More Here
[link](http://spf13.com) and text
{{% /inside %}}`, "
More Here \n\n
link and text
\n
", wt)
}
func TestInnerSCWithAndWithoutMarkdown(t *testing.T) {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("inside.html", `
{{ .Inner }}
`)
return nil
}
CheckShortCodeMatch(t, `{{% inside %}}
# More Here
[link](http://spf13.com) and text
{{% /inside %}}
And then:
{{< inside >}}
# More Here
This is **plain** text.
{{< /inside >}}
`, "
More Here \n\n
link and text
\n
\n\n
And then:
\n\n
\n# More Here\n\nThis is **plain** text.\n\n
", wt)
}
func TestEmbeddedSC(t *testing.T) {
CheckShortCodeMatch(t, "{{% test %}}", "This is a simple Test", nil)
CheckShortCodeMatch(t, `{{% figure src="/found/here" class="bananas orange" %}}`, "\n
\n \n \n \n \n \n", nil)
CheckShortCodeMatch(t, `{{% figure src="/found/here" class="bananas orange" caption="This is a caption" %}}`, "\n
\n \n \n \n \n \n \n This is a caption\n \n \n \n
\n \n \n \n", nil)
}
func TestNestedSC(t *testing.T) {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("scn1.html", `
Outer, inner is {{ .Inner }}
`)
tem.AddInternalShortcode("scn2.html", `
SC2
`)
return nil
}
CheckShortCodeMatch(t, `{{% scn1 %}}{{% scn2 %}}{{% /scn1 %}}`, "
", wt)
CheckShortCodeMatch(t, `{{< scn1 >}}{{% scn2 %}}{{< /scn1 >}}`, "
", wt)
}
func TestNestedComplexSC(t *testing.T) {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("row.html", `-row-{{ .Inner}}-rowStop-`)
tem.AddInternalShortcode("column.html", `-col-{{.Inner }}-colStop-`)
tem.AddInternalShortcode("aside.html", `-aside-{{ .Inner }}-asideStop-`)
return nil
}
CheckShortCodeMatch(t, `{{< row >}}1-s{{% column %}}2-**s**{{< aside >}}3-**s**{{< /aside >}}4-s{{% /column %}}5-s{{< /row >}}6-s`,
"-row-1-s-col-2-
s -aside-3-
s -asideStop-4-s-colStop-5-s-rowStop-6-s", wt)
// turn around the markup flag
CheckShortCodeMatch(t, `{{% row %}}1-s{{< column >}}2-**s**{{% aside %}}3-**s**{{% /aside %}}4-s{{< /column >}}5-s{{% /row %}}6-s`,
"-row-1-s-col-2-
s -aside-3-
s -asideStop-4-s-colStop-5-s-rowStop-6-s", wt)
}
func TestParentShortcode(t *testing.T) {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("r1.html", `1: {{ .Get "pr1" }} {{ .Inner }}`)
tem.AddInternalShortcode("r2.html", `2: {{ .Parent.Get "pr1" }}{{ .Get "pr2" }} {{ .Inner }}`)
tem.AddInternalShortcode("r3.html", `3: {{ .Parent.Parent.Get "pr1" }}{{ .Parent.Get "pr2" }}{{ .Get "pr3" }} {{ .Inner }}`)
return nil
}
CheckShortCodeMatch(t, `{{< r1 pr1="p1" >}}1: {{< r2 pr2="p2" >}}2: {{< r3 pr3="p3" >}}{{< /r3 >}}{{< /r2 >}}{{< /r1 >}}`,
"1: p1 1: 2: p1p2 2: 3: p1p2p3 ", wt)
}
func TestFigureImgWidth(t *testing.T) {
CheckShortCodeMatch(t, `{{% figure src="/found/here" class="bananas orange" alt="apple" width="100px" %}}`, "\n
\n \n \n \n \n \n", nil)
}
func TestHighlight(t *testing.T) {
testCommonResetState()
if !helpers.HasPygments() {
t.Skip("Skip test as Pygments is not installed")
}
viper.Set("PygmentsStyle", "bw")
viper.Set("PygmentsUseClasses", false)
templ := tpl.New()
code := `
{{< highlight java >}}
void do();
{{< /highlight >}}`
p, _ := pageFromString(simplePage, "simple.md")
output, err := HandleShortcodes(code, p, templ)
if err != nil {
t.Fatal("Handle shortcode error", err)
}
matched, err := regexp.MatchString("(?s)^\n
\n$", output)
if err != nil {
t.Fatal("Regexp error", err)
}
if !matched {
t.Error("Hightlight mismatch, got\n", output)
}
}
const testScPlaceholderRegexp = "{-{-HUGOSHORTCODE-\\d+-}-}"
func TestExtractShortcodes(t *testing.T) {
for i, this := range []struct {
name string
input string
expectShortCodes string
expect interface{}
expectErrorMsg string
}{
{"text", "Some text.", "map[]", "Some text.", ""},
{"invalid right delim", "{{< tag }}", "", false, "simple:4:.*unrecognized character.*}"},
{"invalid close", "\n{{< /tag >}}", "", false, "simple:5:.*got closing shortcode, but none is open"},
{"invalid close2", "\n\n{{< tag >}}{{< /anotherTag >}}", "", false, "simple:6: closing tag for shortcode 'anotherTag' does not match start tag"},
{"unterminated quote 1", `{{< figure src="im caption="S" >}}`, "", false, "simple:4:.got pos.*"},
{"unterminated quote 1", `{{< figure src="im" caption="S >}}`, "", false, "simple:4:.*unterm.*}"},
{"one shortcode, no markup", "{{< tag >}}", "", testScPlaceholderRegexp, ""},
{"one shortcode, markup", "{{% tag %}}", "", testScPlaceholderRegexp, ""},
{"one pos param", "{{% tag param1 %}}", `tag([\"param1\"], true){[]}"]`, testScPlaceholderRegexp, ""},
{"two pos params", "{{< tag param1 param2>}}", `tag([\"param1\" \"param2\"], false){[]}"]`, testScPlaceholderRegexp, ""},
{"one named param", `{{% tag param1="value" %}}`, `tag([\"param1:value\"], true){[]}`, testScPlaceholderRegexp, ""},
{"two named params", `{{< tag param1="value1" param2="value2" >}}`, `tag([\"param1:value1\" \"param2:value2\"], false){[]}"]`,
testScPlaceholderRegexp, ""},
{"inner", `Some text. {{< inner >}}Inner Content{{< / inner >}}. Some more text.`, `inner([], false){[Inner Content]}`,
fmt.Sprintf("Some text. %s. Some more text.", testScPlaceholderRegexp), ""},
// issue #934
{"inner self-closing", `Some text. {{< inner />}}. Some more text.`, `inner([], false){[]}`,
fmt.Sprintf("Some text. %s. Some more text.", testScPlaceholderRegexp), ""},
{"close, but not inner", "{{< tag >}}foo{{< /tag >}}", "", false, "Shortcode 'tag' in page 'simple.md' has no .Inner.*"},
{"nested inner", `Inner->{{< inner >}}Inner Content->{{% inner2 param1 %}}inner2txt{{% /inner2 %}}Inner close->{{< / inner >}}<-done`,
`inner([], false){[Inner Content-> inner2([\"param1\"], true){[inner2txt]} Inner close->]}`,
fmt.Sprintf("Inner->%s<-done", testScPlaceholderRegexp), ""},
{"nested, nested inner", `Inner->{{< inner >}}inner2->{{% inner2 param1 %}}inner2txt->inner3{{< inner3>}}inner3txt{{ inner3 >}}{{% /inner2 %}}final close->{{< / inner >}}<-done`,
`inner([], false){[inner2-> inner2([\"param1\"], true){[inner2txt->inner3 inner3(%!q(
), false){[inner3txt]}]} final close->`,
fmt.Sprintf("Inner->%s<-done", testScPlaceholderRegexp), ""},
{"two inner", `Some text. {{% inner %}}First **Inner** Content{{% / inner %}} {{< inner >}}Inner **Content**{{< / inner >}}. Some more text.`,
`map["{-{-HUGOSHORTCODE-1-}-}:inner([], true){[First **Inner** Content]}" "{-{-HUGOSHORTCODE-2-}-}:inner([], false){[Inner **Content**]}"]`,
fmt.Sprintf("Some text. %s %s. Some more text.", testScPlaceholderRegexp, testScPlaceholderRegexp), ""},
{"closed without content", `Some text. {{< inner param1 >}}{{< / inner >}}. Some more text.`, `inner([\"param1\"], false){[]}`,
fmt.Sprintf("Some text. %s. Some more text.", testScPlaceholderRegexp), ""},
{"two shortcodes", "{{< sc1 >}}{{< sc2 >}}",
`map["{-{-HUGOSHORTCODE-1-}-}:sc1([], false){[]}" "{-{-HUGOSHORTCODE-2-}-}:sc2([], false){[]}"]`,
testScPlaceholderRegexp + testScPlaceholderRegexp, ""},
{"mix of shortcodes", `Hello {{< sc1 >}}world{{% sc2 p2="2"%}}. And that's it.`,
`map["{-{-HUGOSHORTCODE-1-}-}:sc1([], false){[]}" "{-{-HUGOSHORTCODE-2-}-}:sc2([\"p2:2\"]`,
fmt.Sprintf("Hello %sworld%s. And that's it.", testScPlaceholderRegexp, testScPlaceholderRegexp), ""},
{"mix with inner", `Hello {{< sc1 >}}world{{% inner p2="2"%}}Inner{{%/ inner %}}. And that's it.`,
`map["{-{-HUGOSHORTCODE-1-}-}:sc1([], false){[]}" "{-{-HUGOSHORTCODE-2-}-}:inner([\"p2:2\"], true){[Inner]}"]`,
fmt.Sprintf("Hello %sworld%s. And that's it.", testScPlaceholderRegexp, testScPlaceholderRegexp), ""},
} {
p, _ := pageFromString(simplePage, "simple.md")
tem := tpl.New()
tem.AddInternalShortcode("tag.html", `tag`)
tem.AddInternalShortcode("sc1.html", `sc1`)
tem.AddInternalShortcode("sc2.html", `sc2`)
tem.AddInternalShortcode("inner.html", `{{with .Inner }}{{ . }}{{ end }}`)
tem.AddInternalShortcode("inner2.html", `{{.Inner}}`)
tem.AddInternalShortcode("inner3.html", `{{.Inner}}`)
content, shortCodes, err := extractShortcodes(this.input, p, tem)
if b, ok := this.expect.(bool); ok && !b {
if err == nil {
t.Fatalf("[%d] %s: ExtractShortcodes didn't return an expected error", i, this.name)
} else {
r, _ := regexp.Compile(this.expectErrorMsg)
if !r.MatchString(err.Error()) {
t.Fatalf("[%d] %s: ExtractShortcodes didn't return an expected error message, got %s but expected %s",
i, this.name, err.Error(), this.expectErrorMsg)
}
}
continue
} else {
if err != nil {
t.Fatalf("[%d] %s: failed: %q", i, this.name, err)
}
}
var expected string
av := reflect.ValueOf(this.expect)
switch av.Kind() {
case reflect.String:
expected = av.String()
}
r, err := regexp.Compile(expected)
if err != nil {
t.Fatalf("[%d] %s: Failed to compile regexp %q: %q", i, this.name, expected, err)
}
if strings.Count(content, shortcodePlaceholderPrefix) != len(shortCodes) {
t.Fatalf("[%d] %s: Not enough placeholders, found %d", i, this.name, len(shortCodes))
}
if !r.MatchString(content) {
t.Fatalf("[%d] %s: Shortcode extract didn't match. got %q but expected %q", i, this.name, content, expected)
}
for placeHolder, sc := range shortCodes {
if !strings.Contains(content, placeHolder) {
t.Fatalf("[%d] %s: Output does not contain placeholder %q", i, this.name, placeHolder)
}
if sc.params == nil {
t.Fatalf("[%d] %s: Params is nil for shortcode '%s'", i, this.name, sc.name)
}
}
if this.expectShortCodes != "" {
shortCodesAsStr := fmt.Sprintf("map%q", collectAndSortShortcodes(shortCodes))
if !strings.Contains(shortCodesAsStr, this.expectShortCodes) {
t.Fatalf("[%d] %s: Short codes not as expected, got %s but expected %s", i, this.name, shortCodesAsStr, this.expectShortCodes)
}
}
}
}
func TestShortcodesInSite(t *testing.T) {
testCommonResetState()
baseURL := "http://foo/bar"
viper.Set("DefaultExtension", "html")
viper.Set("DefaultContentLanguage", "en")
viper.Set("baseurl", baseURL)
viper.Set("UglyURLs", false)
viper.Set("verbose", true)
tests := []struct {
contentPath string
content string
outFile string
expected string
}{
{"sect/doc1.md", `a{{< b >}}c`,
filepath.FromSlash("sect/doc1/index.html"), "abc
\n"},
// Issue #1642: Multiple shortcodes wrapped in P
// Deliberately forced to pass even if they maybe shouldn't.
{"sect/doc2.md", `a
{{< b >}}
{{< c >}}
{{< d >}}
e`,
filepath.FromSlash("sect/doc2/index.html"),
"a
\n\nb \nc\nd
\n\ne
\n"},
{"sect/doc3.md", `a
{{< b >}}
{{< c >}}
{{< d >}}
e`,
filepath.FromSlash("sect/doc3/index.html"),
"a
\n\nb \nc
\n\nd\n\ne
\n"},
{"sect/doc4.md", `a
{{< b >}}
{{< b >}}
{{< b >}}
{{< b >}}
{{< b >}}
`,
filepath.FromSlash("sect/doc4/index.html"),
"a\nb\nb\nb\nb\nb
\n"},
// #2192 #2209: Shortcodes in markdown headers
{"sect/doc5.md", `# {{< b >}}
## {{% c %}}`,
filepath.FromSlash("sect/doc5/index.html"), "\n\nb \n\nc \n"},
{"sect/doc6.md", "\n```bash\n{{< b >}}\n{{% c %}}\n```\n",
filepath.FromSlash("sect/doc6/index.html"),
"b\nc\n
\n"},
// #2249
{"sect/doc7.ad", `_Shortcodes:_ *b: {{< b >}} c: {{% c %}}*`,
filepath.FromSlash("sect/doc7/index.html"),
"\n
Shortcodes: b: b c: c
\n
\n"},
{"sect/doc8.rst", `**Shortcodes:** *b: {{< b >}} c: {{% c %}}*`,
filepath.FromSlash("sect/doc8/index.html"),
"\n\n\n
Shortcodes: b: b c: c
\n
"},
{"sect/doc9.mmark", `
---
menu:
main:
parent: 'parent'
---
**Shortcodes:** *b: {{< b >}} c: {{% c %}}*`,
filepath.FromSlash("sect/doc9/index.html"),
"Shortcodes: b: b c: c
\n"},
// Issue #1229: Menus not available in shortcode.
{"sect/doc10.md", `---
menu:
main:
identifier: 'parent'
tags:
- Menu
---
**Menus:** {{< menu >}}`,
filepath.FromSlash("sect/doc10/index.html"),
"Menus: 1
\n"},
// Issue #2323: Taxonomies not available in shortcode.
{"sect/doc11.md", `---
tags:
- Bugs
---
**Tags:** {{< tags >}}`,
filepath.FromSlash("sect/doc11/index.html"),
"Tags: 2
\n"},
}
sources := make([]source.ByteSource, len(tests))
for i, test := range tests {
sources[i] = source.ByteSource{Name: filepath.FromSlash(test.contentPath), Content: []byte(test.content)}
}
s := &Site{
Source: &source.InMemorySource{ByteSource: sources},
targets: targetList{page: &target.PagePub{UglyURLs: false}},
Language: helpers.NewDefaultLanguage(),
}
addTemplates := func(templ tpl.Template) error {
templ.AddTemplate("_default/single.html", "{{.Content}}")
templ.AddInternalShortcode("b.html", `b`)
templ.AddInternalShortcode("c.html", `c`)
templ.AddInternalShortcode("d.html", `d`)
templ.AddInternalShortcode("menu.html", `{{ len (index .Page.Menus "main").Children }}`)
templ.AddInternalShortcode("tags.html", `{{ len .Page.Site.Taxonomies.tags }}`)
return nil
}
sites, err := newHugoSites(s)
if err != nil {
t.Fatalf("Failed to build site: %s", err)
}
if err = sites.Build(BuildCfg{withTemplate: addTemplates}); err != nil {
t.Fatalf("Failed to build site: %s", err)
}
for _, test := range tests {
if strings.HasSuffix(test.contentPath, ".ad") && !helpers.HasAsciidoc() {
fmt.Println("Skip Asciidoc test case as no Asciidoc present.")
continue
} else if strings.HasSuffix(test.contentPath, ".rst") && !helpers.HasRst() {
fmt.Println("Skip Rst test case as no rst2html present.")
continue
}
file, err := hugofs.Destination().Open(test.outFile)
if err != nil {
t.Fatalf("Did not find %s in target: %s", test.outFile, err)
}
content := helpers.ReaderToString(file)
if content != test.expected {
t.Fatalf("%s content expected:\n%q\ngot:\n%q", test.outFile, test.expected, content)
}
}
}
func collectAndSortShortcodes(shortcodes map[string]shortcode) []string {
var asArray []string
for key, sc := range shortcodes {
asArray = append(asArray, fmt.Sprintf("%s:%s", key, sc))
}
sort.Strings(asArray)
return asArray
}
func BenchmarkReplaceShortcodeTokens(b *testing.B) {
type input struct {
in []byte
replacements map[string]string
expect []byte
}
data := []struct {
input string
replacements map[string]string
expect []byte
}{
{"Hello {-{-HUGOSHORTCODE-1-}-}.", map[string]string{"{-{-HUGOSHORTCODE-1-}-}": "World"}, []byte("Hello World.")},
{strings.Repeat("A", 100) + " {-{-HUGOSHORTCODE-1-}-}.", map[string]string{"{-{-HUGOSHORTCODE-1-}-}": "Hello World"}, []byte(strings.Repeat("A", 100) + " Hello World.")},
{strings.Repeat("A", 500) + " {-{-HUGOSHORTCODE-1-}-}.", map[string]string{"{-{-HUGOSHORTCODE-1-}-}": "Hello World"}, []byte(strings.Repeat("A", 500) + " Hello World.")},
{strings.Repeat("ABCD ", 500) + " {-{-HUGOSHORTCODE-1-}-}.", map[string]string{"{-{-HUGOSHORTCODE-1-}-}": "Hello World"}, []byte(strings.Repeat("ABCD ", 500) + " Hello World.")},
{strings.Repeat("A ", 3000) + " {-{-HUGOSHORTCODE-1-}-}." + strings.Repeat("BC ", 1000) + " {-{-HUGOSHORTCODE-1-}-}.", map[string]string{"{-{-HUGOSHORTCODE-1-}-}": "Hello World"}, []byte(strings.Repeat("A ", 3000) + " Hello World." + strings.Repeat("BC ", 1000) + " Hello World.")},
}
var in = make([]input, b.N*len(data))
var cnt = 0
for i := 0; i < b.N; i++ {
for _, this := range data {
in[cnt] = input{[]byte(this.input), this.replacements, this.expect}
cnt++
}
}
b.ResetTimer()
cnt = 0
for i := 0; i < b.N; i++ {
for j := range data {
currIn := in[cnt]
cnt++
results, err := replaceShortcodeTokens(currIn.in, "HUGOSHORTCODE", currIn.replacements)
if err != nil {
b.Fatalf("[%d] failed: %s", i, err)
continue
}
if len(results) != len(currIn.expect) {
b.Fatalf("[%d] replaceShortcodeTokens, got \n%q but expected \n%q", j, results, currIn.expect)
}
}
}
}
func TestReplaceShortcodeTokens(t *testing.T) {
for i, this := range []struct {
input string
prefix string
replacements map[string]string
expect interface{}
}{
{"Hello {-{-PREFIX-1-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "Hello World."},
{"Hello {-{-PREFIX-1@}@.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, false},
{"{-{-PREFIX2-1-}-}", "PREFIX2", map[string]string{"{-{-PREFIX2-1-}-}": "World"}, "World"},
{"Hello World!", "PREFIX2", map[string]string{}, "Hello World!"},
{"!{-{-PREFIX-1-}-}", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "!World"},
{"{-{-PREFIX-1-}-}!", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "World!"},
{"!{-{-PREFIX-1-}-}!", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "!World!"},
{"_{_PREFIX-1-}-}", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "_{_PREFIX-1-}-}"},
{"Hello {-{-PREFIX-1-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "To You My Old Friend Who Told Me This Fantastic Story"}, "Hello To You My Old Friend Who Told Me This Fantastic Story."},
{"A {-{-A-1-}-} asdf {-{-A-2-}-}.", "A", map[string]string{"{-{-A-1-}-}": "v1", "{-{-A-2-}-}": "v2"}, "A v1 asdf v2."},
{"Hello {-{-PREFIX2-1-}-}. Go {-{-PREFIX2-2-}-}, Go, Go {-{-PREFIX2-3-}-} Go Go!.", "PREFIX2", map[string]string{"{-{-PREFIX2-1-}-}": "Europe", "{-{-PREFIX2-2-}-}": "Jonny", "{-{-PREFIX2-3-}-}": "Johnny"}, "Hello Europe. Go Jonny, Go, Go Johnny Go Go!."},
{"A {-{-PREFIX-2-}-} {-{-PREFIX-1-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A", "{-{-PREFIX-2-}-}": "B"}, "A B A."},
{"A {-{-PREFIX-1-}-} {-{-PREFIX-2", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A"}, false},
{"A {-{-PREFIX-1-}-} but not the second.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A", "{-{-PREFIX-2-}-}": "B"}, "A A but not the second."},
{"An {-{-PREFIX-1-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A", "{-{-PREFIX-2-}-}": "B"}, "An A."},
{"An {-{-PREFIX-1-}-} {-{-PREFIX-2-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A", "{-{-PREFIX-2-}-}": "B"}, "An A B."},
{"A {-{-PREFIX-1-}-} {-{-PREFIX-2-}-} {-{-PREFIX-3-}-} {-{-PREFIX-1-}-} {-{-PREFIX-3-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A", "{-{-PREFIX-2-}-}": "B", "{-{-PREFIX-3-}-}": "C"}, "A A B C A C."},
{"A {-{-PREFIX-1-}-} {-{-PREFIX-2-}-} {-{-PREFIX-3-}-} {-{-PREFIX-1-}-} {-{-PREFIX-3-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A", "{-{-PREFIX-2-}-}": "B", "{-{-PREFIX-3-}-}": "C"}, "A A B C A C."},
// Issue #1148 remove p-tags 10 =>
{"Hello {-{-PREFIX-1-}-}
. END.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "Hello World. END."},
{"Hello {-{-PREFIX-1-}-}
. {-{-PREFIX-2-}-}
END.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World", "{-{-PREFIX-2-}-}": "THE"}, "Hello World. THE END."},
{"Hello {-{-PREFIX-1-}-}. END
.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "Hello World. END
."},
{"Hello {-{-PREFIX-1-}-}
. END.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "Hello World
. END."},
{"Hello {-{-PREFIX-1-}-}12", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "Hello
World12"},
{"Hello {-{-P-1-}-}. {-{-P-1-}-}-{-{-P-1-}-} {-{-P-1-}-} {-{-P-1-}-} {-{-P-1-}-} END", "P", map[string]string{"{-{-P-1-}-}": strings.Repeat("BC", 100)},
fmt.Sprintf("Hello %s. %s-%s %s %s %s END",
strings.Repeat("BC", 100), strings.Repeat("BC", 100), strings.Repeat("BC", 100), strings.Repeat("BC", 100), strings.Repeat("BC", 100), strings.Repeat("BC", 100))},
} {
results, err := replaceShortcodeTokens([]byte(this.input), this.prefix, this.replacements)
if b, ok := this.expect.(bool); ok && !b {
if err == nil {
t.Errorf("[%d] replaceShortcodeTokens didn't return an expected error", i)
}
} else {
if err != nil {
t.Errorf("[%d] failed: %s", i, err)
continue
}
if !reflect.DeepEqual(results, []byte(this.expect.(string))) {
t.Errorf("[%d] replaceShortcodeTokens, got \n%q but expected \n%q", i, results, this.expect)
}
}
}
}