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