github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/tpl/transform/transform_test.go (about) 1 // Copyright 2017 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 transform_test 15 16 import ( 17 "context" 18 "html/template" 19 "strings" 20 "testing" 21 22 "github.com/gohugoio/hugo/hugolib" 23 "github.com/gohugoio/hugo/tpl/transform" 24 25 qt "github.com/frankban/quicktest" 26 ) 27 28 type tstNoStringer struct{} 29 30 func TestEmojify(t *testing.T) { 31 t.Parallel() 32 b := hugolib.NewIntegrationTestBuilder( 33 hugolib.IntegrationTestConfig{T: t}, 34 ).Build() 35 36 ns := transform.New(b.H.Deps) 37 38 for _, test := range []struct { 39 s any 40 expect any 41 }{ 42 {":notamoji:", template.HTML(":notamoji:")}, 43 {"I :heart: Hugo", template.HTML("I ❤️ Hugo")}, 44 // errors 45 {tstNoStringer{}, false}, 46 } { 47 48 result, err := ns.Emojify(test.s) 49 50 if bb, ok := test.expect.(bool); ok && !bb { 51 b.Assert(err, qt.Not(qt.IsNil)) 52 continue 53 } 54 55 b.Assert(err, qt.IsNil) 56 b.Assert(result, qt.Equals, test.expect) 57 } 58 } 59 60 func TestHighlight(t *testing.T) { 61 t.Parallel() 62 b := hugolib.NewIntegrationTestBuilder( 63 hugolib.IntegrationTestConfig{T: t}, 64 ).Build() 65 66 ns := transform.New(b.H.Deps) 67 68 for _, test := range []struct { 69 s any 70 lang string 71 opts any 72 expect any 73 }{ 74 {"func boo() {}", "go", "", "boo"}, 75 {"func boo() {}", "go", nil, "boo"}, 76 // Issue #4179 77 {`<Foo attr=" < "></Foo>`, "xml", "", `&lt;`}, 78 {tstNoStringer{}, "go", "", false}, 79 // Issue #9591 80 {strings.Repeat("AAA \n", 10), "bash", template.HTML("linenos=true,noClasses=false"), "line"}, 81 } { 82 83 result, err := ns.Highlight(test.s, test.lang, test.opts) 84 85 if bb, ok := test.expect.(bool); ok && !bb { 86 b.Assert(err, qt.Not(qt.IsNil)) 87 continue 88 } 89 90 b.Assert(err, qt.IsNil) 91 b.Assert(string(result), qt.Contains, test.expect.(string)) 92 } 93 } 94 95 func TestCanHighlight(t *testing.T) { 96 t.Parallel() 97 98 c := qt.New(t) 99 ns := &transform.Namespace{} 100 101 c.Assert(ns.CanHighlight("go"), qt.Equals, true) 102 c.Assert(ns.CanHighlight("foo"), qt.Equals, false) 103 } 104 105 func TestHTMLEscape(t *testing.T) { 106 t.Parallel() 107 b := hugolib.NewIntegrationTestBuilder( 108 hugolib.IntegrationTestConfig{T: t}, 109 ).Build() 110 111 ns := transform.New(b.H.Deps) 112 113 for _, test := range []struct { 114 s any 115 expect any 116 }{ 117 {`"Foo & Bar's Diner" <y@z>`, `"Foo & Bar's Diner" <y@z>`}, 118 {"Hugo & Caddy > Wordpress & Apache", "Hugo & Caddy > Wordpress & Apache"}, 119 // errors 120 {tstNoStringer{}, false}, 121 } { 122 123 result, err := ns.HTMLEscape(test.s) 124 125 if bb, ok := test.expect.(bool); ok && !bb { 126 b.Assert(err, qt.Not(qt.IsNil)) 127 continue 128 } 129 130 b.Assert(err, qt.IsNil) 131 b.Assert(result, qt.Equals, test.expect) 132 } 133 } 134 135 func TestHTMLUnescape(t *testing.T) { 136 t.Parallel() 137 b := hugolib.NewIntegrationTestBuilder( 138 hugolib.IntegrationTestConfig{T: t}, 139 ).Build() 140 141 ns := transform.New(b.H.Deps) 142 143 for _, test := range []struct { 144 s any 145 expect any 146 }{ 147 {`"Foo & Bar's Diner" <y@z>`, `"Foo & Bar's Diner" <y@z>`}, 148 {"Hugo & Caddy > Wordpress & Apache", "Hugo & Caddy > Wordpress & Apache"}, 149 // errors 150 {tstNoStringer{}, false}, 151 } { 152 153 result, err := ns.HTMLUnescape(test.s) 154 155 if bb, ok := test.expect.(bool); ok && !bb { 156 b.Assert(err, qt.Not(qt.IsNil)) 157 continue 158 } 159 160 b.Assert(err, qt.IsNil) 161 b.Assert(result, qt.Equals, test.expect) 162 } 163 } 164 165 func TestMarkdownify(t *testing.T) { 166 t.Parallel() 167 b := hugolib.NewIntegrationTestBuilder( 168 hugolib.IntegrationTestConfig{T: t}, 169 ).Build() 170 171 ns := transform.New(b.H.Deps) 172 173 for _, test := range []struct { 174 s any 175 expect any 176 }{ 177 {"Hello **World!**", template.HTML("Hello <strong>World!</strong>")}, 178 {[]byte("Hello Bytes **World!**"), template.HTML("Hello Bytes <strong>World!</strong>")}, 179 {tstNoStringer{}, false}, 180 } { 181 182 result, err := ns.Markdownify(context.Background(), test.s) 183 184 if bb, ok := test.expect.(bool); ok && !bb { 185 b.Assert(err, qt.Not(qt.IsNil)) 186 continue 187 } 188 189 b.Assert(err, qt.IsNil) 190 b.Assert(result, qt.Equals, test.expect) 191 } 192 } 193 194 // Issue #3040 195 func TestMarkdownifyBlocksOfText(t *testing.T) { 196 t.Parallel() 197 b := hugolib.NewIntegrationTestBuilder( 198 hugolib.IntegrationTestConfig{T: t}, 199 ).Build() 200 201 ns := transform.New(b.H.Deps) 202 203 text := ` 204 #First 205 206 This is some *bold* text. 207 208 ## Second 209 210 This is some more text. 211 212 And then some. 213 ` 214 215 result, err := ns.Markdownify(context.Background(), text) 216 b.Assert(err, qt.IsNil) 217 b.Assert(result, qt.Equals, template.HTML( 218 "<p>#First</p>\n<p>This is some <em>bold</em> text.</p>\n<h2 id=\"second\">Second</h2>\n<p>This is some more text.</p>\n<p>And then some.</p>\n")) 219 } 220 221 func TestPlainify(t *testing.T) { 222 t.Parallel() 223 b := hugolib.NewIntegrationTestBuilder( 224 hugolib.IntegrationTestConfig{T: t}, 225 ).Build() 226 227 ns := transform.New(b.H.Deps) 228 229 for _, test := range []struct { 230 s any 231 expect any 232 }{ 233 {"<em>Note:</em> blah <b>blah</b>", "Note: blah blah"}, 234 {"<div data-action='click->my-controller#doThing'>qwe</div>", "qwe"}, 235 // errors 236 {tstNoStringer{}, false}, 237 } { 238 239 result, err := ns.Plainify(test.s) 240 241 if bb, ok := test.expect.(bool); ok && !bb { 242 b.Assert(err, qt.Not(qt.IsNil)) 243 continue 244 } 245 246 b.Assert(err, qt.IsNil) 247 b.Assert(result, qt.Equals, test.expect) 248 } 249 }