github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/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 "html/template" 18 "testing" 19 20 "github.com/gohugoio/hugo/common/loggers" 21 "github.com/gohugoio/hugo/hugolib" 22 "github.com/gohugoio/hugo/tpl/transform" 23 "github.com/spf13/afero" 24 25 qt "github.com/frankban/quicktest" 26 "github.com/gohugoio/hugo/config" 27 "github.com/gohugoio/hugo/deps" 28 "github.com/gohugoio/hugo/helpers" 29 "github.com/gohugoio/hugo/hugofs" 30 "github.com/gohugoio/hugo/langs" 31 ) 32 33 type tstNoStringer struct{} 34 35 func TestEmojify(t *testing.T) { 36 t.Parallel() 37 b := hugolib.NewIntegrationTestBuilder( 38 hugolib.IntegrationTestConfig{T: t}, 39 ).Build() 40 41 ns := transform.New(b.H.Deps) 42 43 for _, test := range []struct { 44 s interface{} 45 expect interface{} 46 }{ 47 {":notamoji:", template.HTML(":notamoji:")}, 48 {"I :heart: Hugo", template.HTML("I ❤️ Hugo")}, 49 // errors 50 {tstNoStringer{}, false}, 51 } { 52 53 result, err := ns.Emojify(test.s) 54 55 if bb, ok := test.expect.(bool); ok && !bb { 56 b.Assert(err, qt.Not(qt.IsNil)) 57 continue 58 } 59 60 b.Assert(err, qt.IsNil) 61 b.Assert(result, qt.Equals, test.expect) 62 } 63 } 64 65 func TestHighlight(t *testing.T) { 66 t.Parallel() 67 b := hugolib.NewIntegrationTestBuilder( 68 hugolib.IntegrationTestConfig{T: t}, 69 ).Build() 70 71 ns := transform.New(b.H.Deps) 72 73 for _, test := range []struct { 74 s interface{} 75 lang string 76 opts interface{} 77 expect interface{} 78 }{ 79 {"func boo() {}", "go", "", "boo"}, 80 {"func boo() {}", "go", nil, "boo"}, 81 // Issue #4179 82 {`<Foo attr=" < "></Foo>`, "xml", "", `&lt;`}, 83 {tstNoStringer{}, "go", "", false}, 84 } { 85 86 result, err := ns.Highlight(test.s, test.lang, test.opts) 87 88 if bb, ok := test.expect.(bool); ok && !bb { 89 b.Assert(err, qt.Not(qt.IsNil)) 90 continue 91 } 92 93 b.Assert(err, qt.IsNil) 94 b.Assert(string(result), qt.Contains, test.expect.(string)) 95 } 96 } 97 98 func TestHTMLEscape(t *testing.T) { 99 t.Parallel() 100 b := hugolib.NewIntegrationTestBuilder( 101 hugolib.IntegrationTestConfig{T: t}, 102 ).Build() 103 104 ns := transform.New(b.H.Deps) 105 106 for _, test := range []struct { 107 s interface{} 108 expect interface{} 109 }{ 110 {`"Foo & Bar's Diner" <y@z>`, `"Foo & Bar's Diner" <y@z>`}, 111 {"Hugo & Caddy > Wordpress & Apache", "Hugo & Caddy > Wordpress & Apache"}, 112 // errors 113 {tstNoStringer{}, false}, 114 } { 115 116 result, err := ns.HTMLEscape(test.s) 117 118 if bb, ok := test.expect.(bool); ok && !bb { 119 b.Assert(err, qt.Not(qt.IsNil)) 120 continue 121 } 122 123 b.Assert(err, qt.IsNil) 124 b.Assert(result, qt.Equals, test.expect) 125 } 126 } 127 128 func TestHTMLUnescape(t *testing.T) { 129 t.Parallel() 130 b := hugolib.NewIntegrationTestBuilder( 131 hugolib.IntegrationTestConfig{T: t}, 132 ).Build() 133 134 ns := transform.New(b.H.Deps) 135 136 for _, test := range []struct { 137 s interface{} 138 expect interface{} 139 }{ 140 {`"Foo & Bar's Diner" <y@z>`, `"Foo & Bar's Diner" <y@z>`}, 141 {"Hugo & Caddy > Wordpress & Apache", "Hugo & Caddy > Wordpress & Apache"}, 142 // errors 143 {tstNoStringer{}, false}, 144 } { 145 146 result, err := ns.HTMLUnescape(test.s) 147 148 if bb, ok := test.expect.(bool); ok && !bb { 149 b.Assert(err, qt.Not(qt.IsNil)) 150 continue 151 } 152 153 b.Assert(err, qt.IsNil) 154 b.Assert(result, qt.Equals, test.expect) 155 } 156 } 157 158 func TestMarkdownify(t *testing.T) { 159 t.Parallel() 160 b := hugolib.NewIntegrationTestBuilder( 161 hugolib.IntegrationTestConfig{T: t}, 162 ).Build() 163 164 ns := transform.New(b.H.Deps) 165 166 for _, test := range []struct { 167 s interface{} 168 expect interface{} 169 }{ 170 {"Hello **World!**", template.HTML("Hello <strong>World!</strong>")}, 171 {[]byte("Hello Bytes **World!**"), template.HTML("Hello Bytes <strong>World!</strong>")}, 172 {tstNoStringer{}, false}, 173 } { 174 175 result, err := ns.Markdownify(test.s) 176 177 if bb, ok := test.expect.(bool); ok && !bb { 178 b.Assert(err, qt.Not(qt.IsNil)) 179 continue 180 } 181 182 b.Assert(err, qt.IsNil) 183 b.Assert(result, qt.Equals, test.expect) 184 } 185 } 186 187 // Issue #3040 188 func TestMarkdownifyBlocksOfText(t *testing.T) { 189 t.Parallel() 190 b := hugolib.NewIntegrationTestBuilder( 191 hugolib.IntegrationTestConfig{T: t}, 192 ).Build() 193 194 ns := transform.New(b.H.Deps) 195 196 text := ` 197 #First 198 199 This is some *bold* text. 200 201 ## Second 202 203 This is some more text. 204 205 And then some. 206 ` 207 208 result, err := ns.Markdownify(text) 209 b.Assert(err, qt.IsNil) 210 b.Assert(result, qt.Equals, template.HTML( 211 "<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")) 212 } 213 214 func TestPlainify(t *testing.T) { 215 t.Parallel() 216 b := hugolib.NewIntegrationTestBuilder( 217 hugolib.IntegrationTestConfig{T: t}, 218 ).Build() 219 220 ns := transform.New(b.H.Deps) 221 222 for _, test := range []struct { 223 s interface{} 224 expect interface{} 225 }{ 226 {"<em>Note:</em> blah <b>blah</b>", "Note: blah blah"}, 227 // errors 228 {tstNoStringer{}, false}, 229 } { 230 231 result, err := ns.Plainify(test.s) 232 233 if bb, ok := test.expect.(bool); ok && !bb { 234 b.Assert(err, qt.Not(qt.IsNil)) 235 continue 236 } 237 238 b.Assert(err, qt.IsNil) 239 b.Assert(result, qt.Equals, test.expect) 240 } 241 } 242 243 func newDeps(cfg config.Provider) *deps.Deps { 244 cfg.Set("contentDir", "content") 245 cfg.Set("i18nDir", "i18n") 246 247 l := langs.NewLanguage("en", cfg) 248 249 cs, err := helpers.NewContentSpec(l, loggers.NewErrorLogger(), afero.NewMemMapFs(), nil) 250 if err != nil { 251 panic(err) 252 } 253 254 return &deps.Deps{ 255 Cfg: cfg, 256 Fs: hugofs.NewMem(l), 257 ContentSpec: cs, 258 } 259 }