content_test.go - hugo - [fork] hugo port for 9front
HTML git clone https://git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
content_test.go (4936B)
---
1 // Copyright 2024 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 helpers_test
15
16 import (
17 "bytes"
18 "html/template"
19 "strings"
20 "testing"
21
22 qt "github.com/frankban/quicktest"
23 "github.com/gohugoio/hugo/helpers"
24 )
25
26 func TestTrimShortHTML(t *testing.T) {
27 tests := []struct {
28 markup string
29 input []byte
30 output []byte
31 }{
32 {"markdown", []byte(""), []byte("")},
33 {"markdown", []byte("Plain text"), []byte("Plain text")},
34 {"markdown", []byte("<p>Simple paragraph</p>"), []byte("Simple paragraph")},
35 {"markdown", []byte("\n \n \t <p> \t Whitespace\nHTML \n\t </p>\n\t"), []byte("Whitespace\nHTML")},
36 {"markdown", []byte("<p>Multiple</p><p>paragraphs</p>"), []byte("<p>Multiple</p><p>paragraphs</p>")},
37 {"markdown", []byte("<p>Nested<p>paragraphs</p></p>"), []byte("<p>Nested<p>paragraphs</p></p>")},
38 {"markdown", []byte("<p>Hello</p>\n<ul>\n<li>list1</li>\n<li>list2</li>\n</ul>"), []byte("<p>Hello</p>\n<ul>\n<li>list1</li>\n<li>list2</li>\n</ul>")},
39 // Issue 11698
40 {"markdown", []byte("<h2 id=`a`>b</h2>\n\n<p>c</p>"), []byte("<h2 id=`a`>b</h2>\n\n<p>c</p>")},
41 // Issue 12369
42 {"markdown", []byte("<div class=\"paragraph\">\n<p>foo</p>\n</div>"), []byte("<div class=\"paragraph\">\n<p>foo</p>\n</div>")},
43 {"asciidoc", []byte("<div class=\"paragraph\">\n<p>foo</p>\n</div>"), []byte("foo")},
44 }
45
46 c := newTestContentSpec(nil)
47 for i, test := range tests {
48 output := c.TrimShortHTML(test.input, test.markup)
49 if !bytes.Equal(test.output, output) {
50 t.Errorf("Test %d failed. Expected %q got %q", i, test.output, output)
51 }
52 }
53 }
54
55 func BenchmarkTrimShortHTML(b *testing.B) {
56 c := newTestContentSpec(nil)
57 b.ResetTimer()
58 for i := 0; i < b.N; i++ {
59 c.TrimShortHTML([]byte("<p>Simple paragraph</p>"), "markdown")
60 }
61 }
62
63 func TestBytesToHTML(t *testing.T) {
64 c := qt.New(t)
65 c.Assert(helpers.BytesToHTML([]byte("dobedobedo")), qt.Equals, template.HTML("dobedobedo"))
66 }
67
68 func TestExtractTOCNormalContent(t *testing.T) {
69 content := []byte("<nav>\n<ul>\nTOC<li><a href=\"#")
70
71 actualTocLessContent, actualToc := helpers.ExtractTOC(content)
72 expectedTocLess := []byte("TOC<li><a href=\"#")
73 expectedToc := []byte("<nav id=\"TableOfContents\">\n<ul>\n")
74
75 if !bytes.Equal(actualTocLessContent, expectedTocLess) {
76 t.Errorf("Actual tocless (%s) did not equal expected (%s) tocless content", actualTocLessContent, expectedTocLess)
77 }
78
79 if !bytes.Equal(actualToc, expectedToc) {
80 t.Errorf("Actual toc (%s) did not equal expected (%s) toc content", actualToc, expectedToc)
81 }
82 }
83
84 func TestExtractTOCGreaterThanSeventy(t *testing.T) {
85 content := []byte("<nav>\n<ul>\nTOC This is a very long content which will definitely be greater than seventy, I promise you that.<li><a href=\"#")
86
87 actualTocLessContent, actualToc := helpers.ExtractTOC(content)
88 // Because the start of Toc is greater than 70+startpoint of <li> content and empty TOC will be returned
89 expectedToc := []byte("")
90
91 if !bytes.Equal(actualTocLessContent, content) {
92 t.Errorf("Actual tocless (%s) did not equal expected (%s) tocless content", actualTocLessContent, content)
93 }
94
95 if !bytes.Equal(actualToc, expectedToc) {
96 t.Errorf("Actual toc (%s) did not equal expected (%s) toc content", actualToc, expectedToc)
97 }
98 }
99
100 func TestExtractNoTOC(t *testing.T) {
101 content := []byte("TOC")
102
103 actualTocLessContent, actualToc := helpers.ExtractTOC(content)
104 expectedToc := []byte("")
105
106 if !bytes.Equal(actualTocLessContent, content) {
107 t.Errorf("Actual tocless (%s) did not equal expected (%s) tocless content", actualTocLessContent, content)
108 }
109
110 if !bytes.Equal(actualToc, expectedToc) {
111 t.Errorf("Actual toc (%s) did not equal expected (%s) toc content", actualToc, expectedToc)
112 }
113 }
114
115 var totalWordsBenchmarkString = strings.Repeat("Hugo Rocks ", 200)
116
117 func TestTotalWords(t *testing.T) {
118 for i, this := range []struct {
119 s string
120 words int
121 }{
122 {"Two, Words!", 2},
123 {"Word", 1},
124 {"", 0},
125 {"One, Two, Three", 3},
126 {totalWordsBenchmarkString, 400},
127 } {
128 actualWordCount := helpers.TotalWords(this.s)
129
130 if actualWordCount != this.words {
131 t.Errorf("[%d] Actual word count (%d) for test string (%s) did not match %d", i, actualWordCount, this.s, this.words)
132 }
133 }
134 }
135
136 func BenchmarkTotalWords(b *testing.B) {
137 b.ResetTimer()
138 for i := 0; i < b.N; i++ {
139 wordCount := helpers.TotalWords(totalWordsBenchmarkString)
140 if wordCount != 400 {
141 b.Fatal("Wordcount error")
142 }
143 }
144 }