| 1 | package format |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/stretchr/testify/assert" |
| 7 | "github.com/stretchr/testify/require" |
| 8 | ) |
| 9 | |
| 10 | func TestConvertFormatSameFormat(t *testing.T) { |
| 11 | t.Parallel() |
| 12 | |
| 13 | body := "hello world" |
| 14 | |
| 15 | got, err := ConvertFormat(body, "text", "text") |
| 16 | require.NoError(t, err) |
| 17 | assert.Equal(t, body, got) |
| 18 | } |
| 19 | |
| 20 | func TestConvertFormatMarkdownToText(t *testing.T) { |
| 21 | t.Parallel() |
| 22 | |
| 23 | tests := []struct { |
| 24 | name string |
| 25 | input string |
| 26 | expected string |
| 27 | }{ |
| 28 | {"heading", "# Heading", "Heading"}, |
| 29 | {"bold", "**bold**", "bold"}, |
| 30 | {"italic", "*italic*", "italic"}, |
| 31 | {"underscore italic", "_italic_", "italic"}, |
| 32 | {"code", "`code`", "code"}, |
| 33 | {"link", "[link](http://example.com)", "link"}, |
| 34 | {"image alt text", "", "alt text"}, |
| 35 | {"plain text", "plain text", "plain text"}, |
| 36 | {"list item", "- list item", "list item"}, |
| 37 | {"escaped asterisk", "\\*x\\*", "*x*"}, |
| 38 | {"escaped underscore", "\\_x\\_", "_x_"}, |
| 39 | {"unclosed italic", "*x", "*x"}, |
| 40 | {"unclosed bold", "**x", "**x"}, |
| 41 | {"empty emphasis", "****", "*"}, |
| 42 | {"hr", "---", ""}, |
| 43 | {"multiple headings", "# H1\n## H2\n### H3", "H1\nH2\nH3"}, |
| 44 | {"mixed formatting", "# Title\n\nHello **bold** and *italic* world!", "Title\n\nHello bold and italic world!"}, |
| 45 | {"unicode content", "# 你好\n\n*测试*", "你好\n\n测试"}, |
| 46 | {"special chars preserved", "a<b>c&d\"e", "a<b>c&d\"e"}, |
| 47 | {"only delimiters", "***", "*"}, |
| 48 | {"empty string", "", ""}, |
| 49 | {"nested escaped", "\\*\\*bold\\*\\*", "**bold**"}, |
| 50 | } |
| 51 | |
| 52 | for _, tt := range tests { |
| 53 | t.Run(tt.name, func(t *testing.T) { |
| 54 | t.Parallel() |
| 55 | |
| 56 | got, err := ConvertFormat(tt.input, "markdown", "text") |
| 57 | require.NoError(t, err) |
| 58 | assert.Equal(t, tt.expected, got) |
| 59 | }) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestConvertFormatHTMLToText(t *testing.T) { |
| 64 | t.Parallel() |
| 65 | |
| 66 | tests := []struct { |
| 67 | name string |
| 68 | input string |
| 69 | expected string |
| 70 | }{ |
| 71 | {"paragraph", "<p>hello</p>", "hello"}, |
| 72 | {"bold", "<b>bold</b>", "bold"}, |
| 73 | {"strong", "<strong>bold</strong>", "bold"}, |
| 74 | {"italic", "<i>italic</i>", "italic"}, |
| 75 | {"emphasis", "<em>italic</em>", "italic"}, |
| 76 | {"line break", "<br>", ""}, |
| 77 | {"self-closing br", "<br/>", ""}, |
| 78 | {"plain text", "plain text", "plain text"}, |
| 79 | {"escaped entities", "<escaped>", "<escaped>"}, |
| 80 | {"nested tags", "<div><p>nested</p></div>", "nested"}, |
| 81 | {"heading", "<h1>Title</h1>", "Title"}, |
| 82 | {"multiple headings", "<h1>A</h1><h2>B</h2>", "AB"}, |
| 83 | {"unicode content", "<p>你好</p>", "你好"}, |
| 84 | {"empty string", "", ""}, |
| 85 | {"script tag", "<script>alert('xss')</script>", "alert('xss')"}, |
| 86 | {"link tag", "<a href=\"http://example.com\">link</a>", "link"}, |
| 87 | {"image tag", "<img src=\"test.png\" alt=\"alt text\"/>", ""}, |
| 88 | {"list items", "<ul><li>A</li><li>B</li></ul>", "AB"}, |
| 89 | {"horizontal rule", "<hr>", ""}, |
| 90 | {"comment", "<!-- comment -->", ""}, |
| 91 | {"multiple paragraphs", "<p>A</p><p>B</p>", "AB"}, |
| 92 | } |
| 93 | |
| 94 | for _, tt := range tests { |
| 95 | t.Run(tt.name, func(t *testing.T) { |
| 96 | t.Parallel() |
| 97 | |
| 98 | got, err := ConvertFormat(tt.input, "html", "text") |
| 99 | require.NoError(t, err) |
| 100 | assert.Equal(t, tt.expected, got) |
| 101 | }) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestConvertFormatTextToMarkdown(t *testing.T) { |
| 106 | t.Parallel() |
| 107 | |
| 108 | tests := []struct { |
| 109 | name string |
| 110 | input string |
| 111 | expected string |
| 112 | }{ |
| 113 | {"plain text", "plain text", "plain text"}, |
| 114 | {"asterisk", "use * for emphasis", "use \\* for emphasis"}, |
| 115 | {"underscore", "use _ for emphasis", "use \\_ for emphasis"}, |
| 116 | {"backtick", "code: `cmd`", "code: \\`cmd\\`"}, |
| 117 | {"bracket", "[not a link]", "\\[not a link]"}, |
| 118 | {"multiple special", "*_`[", "\\*\\_\\`\\["}, |
| 119 | {"empty string", "", ""}, |
| 120 | {"unicode safe", "你好世界", "你好世界"}, |
| 121 | {"mixed safe and special", "hello * world", "hello \\* world"}, |
| 122 | } |
| 123 | |
| 124 | for _, tt := range tests { |
| 125 | t.Run(tt.name, func(t *testing.T) { |
| 126 | t.Parallel() |
| 127 | |
| 128 | got, err := ConvertFormat(tt.input, "text", "markdown") |
| 129 | require.NoError(t, err) |
| 130 | assert.Equal(t, tt.expected, got) |
| 131 | }) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestConvertFormatTextToHTML(t *testing.T) { |
| 136 | t.Parallel() |
| 137 | |
| 138 | tests := []struct { |
| 139 | name string |
| 140 | input string |
| 141 | expected string |
| 142 | }{ |
| 143 | {"angle brackets", "<script>", "<script>"}, |
| 144 | {"ampersand", "a&b", "a&b"}, |
| 145 | {"quotes", `a"b'c`, "a"b'c"}, |
| 146 | {"mixed entities", "<>&\"'", "<>&"'"}, |
| 147 | {"plain text", "hello world", "hello world"}, |
| 148 | {"empty string", "", ""}, |
| 149 | {"unicode content", "你好世界", "你好世界"}, |
| 150 | {"newlines preserved", "line1\nline2", "line1\nline2"}, |
| 151 | } |
| 152 | |
| 153 | for _, tt := range tests { |
| 154 | t.Run(tt.name, func(t *testing.T) { |
| 155 | t.Parallel() |
| 156 | |
| 157 | got, err := ConvertFormat(tt.input, "text", "html") |
| 158 | require.NoError(t, err) |
| 159 | assert.Equal(t, tt.expected, got) |
| 160 | }) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | func TestConvertFormatHTMLToMarkdown(t *testing.T) { |
| 165 | t.Parallel() |
| 166 | |
| 167 | tests := []struct { |
| 168 | name string |
| 169 | input string |
| 170 | expected string |
| 171 | }{ |
| 172 | {"strong", "<strong>bold</strong>", "**bold**"}, |
| 173 | {"b tag", "<b>bold</b>", "**bold**"}, |
| 174 | {"em tag", "<em>italic</em>", "_italic_"}, |
| 175 | {"i tag", "<i>italic</i>", "_italic_"}, |
| 176 | {"h1 and h2", "<h1>A</h1><h2>B</h2>", "# A\n## B"}, |
| 177 | {"h3 through h6", "<h3>A</h3><h4>B</h4><h5>C</h5><h6>D</h6>", "### A\n#### B\n##### C\n###### D"}, |
| 178 | {"paragraphs", "<p>A</p><p>B</p>", "A\n\nB"}, |
| 179 | {"line break", "<br>", ""}, |
| 180 | {"self-closing br", "<br/>", ""}, |
| 181 | {"nested emphasis", "<p><strong>bold</strong> and <em>italic</em></p>", "**bold** and _italic_"}, |
| 182 | {"empty strong", "<strong></strong>", "****"}, |
| 183 | {"empty em", "<em></em>", "__"}, |
| 184 | {"unknown tags ignored", "<div>text</div>", "text"}, |
| 185 | {"self-closing img", "<img src=\"test.png\" alt=\"alt text\"/>", ""}, |
| 186 | {"unicode content", "<p>你好</p>", "你好"}, |
| 187 | {"empty string", "", ""}, |
| 188 | {"mixed headings and paragraphs", "<h1>Title</h1><p>Body</p>", "# Title\nBody"}, |
| 189 | {"multiple list items", "<ul><li>A</li><li>B</li></ul>", "AB"}, |
| 190 | {"horizontal rule", "<hr>", ""}, |
| 191 | {"comment", "<!-- comment -->", ""}, |
| 192 | {"heading with nested bold", "<h1><strong>Title</strong></h1>", "# **Title**"}, |
| 193 | {"paragraph with nested italic", "<p>text <em>italic</em> text</p>", "text _italic_ text"}, |
| 194 | {"anchor link", `<a href="http://example.com">link</a>`, "[link](http://example.com)"}, |
| 195 | {"anchor single quotes", `<a href='http://example.com'>link</a>`, "[link](http://example.com)"}, |
| 196 | {"html entities decoded", "<script>", "<script>"}, |
| 197 | {"anchor with nested bold", `<a href="http://example.com"><strong>bold</strong> link</a>`, "[**bold** link](http://example.com)"}, |
| 198 | {"anchor without href", "<a>link</a>", "[link]"}, |
| 199 | {"anchor preserves href case", `<a href="http://example.com/Path?Token=Abc">link</a>`, "[link](http://example.com/Path?Token=Abc)"}, |
| 200 | {"mixed entities and tags", "<p>a&b</p>", "a&b"}, |
| 201 | } |
| 202 | |
| 203 | for _, tt := range tests { |
| 204 | t.Run(tt.name, func(t *testing.T) { |
| 205 | t.Parallel() |
| 206 | |
| 207 | got, err := ConvertFormat(tt.input, "html", "markdown") |
| 208 | require.NoError(t, err) |
| 209 | assert.Equal(t, tt.expected, got) |
| 210 | }) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | func TestConvertFormatMarkdownToHTML(t *testing.T) { |
| 215 | t.Parallel() |
| 216 | |
| 217 | tests := []struct { |
| 218 | name string |
| 219 | input string |
| 220 | expected string |
| 221 | }{ |
| 222 | {"h1", "# Heading", "<h1>Heading</h1>"}, |
| 223 | {"h2", "## Heading", "<h2>Heading</h2>"}, |
| 224 | {"h3", "### Heading", "<h3>Heading</h3>"}, |
| 225 | {"h4", "#### Heading", "<h4>Heading</h4>"}, |
| 226 | {"h5", "##### Heading", "<h5>Heading</h5>"}, |
| 227 | {"h6", "###### Heading", "<h6>Heading</h6>"}, |
| 228 | {"too many hashes", "####### Title", "<p>####### Title</p>"}, |
| 229 | {"no space after hash", "#topic", "<p>#topic</p>"}, |
| 230 | {"bold in heading", "# **bold** heading", "<h1><strong>bold</strong> heading</h1>"}, |
| 231 | {"italic in heading", "## *italic* heading", "<h2><em>italic</em> heading</h2>"}, |
| 232 | {"bold", "**bold**", "<p><strong>bold</strong></p>"}, |
| 233 | {"double underscore bold", "__bold__", "<p><strong>bold</strong></p>"}, |
| 234 | {"italic star", "*italic*", "<p><em>italic</em></p>"}, |
| 235 | {"italic underscore", "_italic_", "<p><em>italic</em></p>"}, |
| 236 | {"escaped asterisk", "\\*x\\*", "<p>*x*</p>"}, |
| 237 | {"escaped underscore", "\\_x\\_", "<p>_x_</p>"}, |
| 238 | {"simple italic star", "*x*", "<p><em>x</em></p>"}, |
| 239 | {"simple italic underscore", "_x_", "<p><em>x</em></p>"}, |
| 240 | {"nested bold italic", "***bold and italic***", "<p><strong><em>bold and italic</em></strong></p>"}, |
| 241 | {"italic with bold inside", "*text **bold** text*", "<p><em>text <strong>bold</strong> text</em></p>"}, |
| 242 | {"bold with italic inside", "**bold *and italic* bold**", "<p><strong>bold <em>and italic</em> bold</strong></p>"}, |
| 243 | {"code span", "`code`", "<p>`code`</p>"}, |
| 244 | {"link", "[link](http://example.com)", "<p>[link](http://example.com)</p>"}, |
| 245 | {"image", "", "<p></p>"}, |
| 246 | {"unordered list", "- item 1\n- item 2", "<p>- item 1</p><p>- item 2</p>"}, |
| 247 | {"horizontal rule", "---", "<p>---</p>"}, |
| 248 | {"empty line becomes br", "line1\n\nline2", "<p>line1</p><br><p>line2</p>"}, |
| 249 | {"multiple paragraphs", "para1\n\npara2", "<p>para1</p><br><p>para2</p>"}, |
| 250 | {"unicode heading", "# 你好", "<h1>你好</h1>"}, |
| 251 | {"unicode emphasis", "*测试*", "<p><em>测试</em></p>"}, |
| 252 | {"mixed content", "# Title\n\nHello **world** and _universe_!", "<h1>Title</h1><br><p>Hello <strong>world</strong> and <em>universe</em>!</p>"}, |
| 253 | {"only emphasis delimiters", "***", "<p>***</p>"}, |
| 254 | {"empty string", "", "<br>"}, |
| 255 | {"whitespace only", " ", "<br>"}, |
| 256 | {"paragraph with special chars", "<script>", "<p><script></p>"}, |
| 257 | } |
| 258 | |
| 259 | for _, tt := range tests { |
| 260 | t.Run(tt.name, func(t *testing.T) { |
| 261 | t.Parallel() |
| 262 | |
| 263 | got, err := ConvertFormat(tt.input, "markdown", "html") |
| 264 | require.NoError(t, err) |
| 265 | assert.Equal(t, tt.expected, got) |
| 266 | }) |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | func TestConvertFormatRoundTrip(t *testing.T) { |
| 271 | t.Parallel() |
| 272 | |
| 273 | tests := []struct { |
| 274 | name string |
| 275 | input string |
| 276 | want string |
| 277 | }{ |
| 278 | {"escaped asterisk", "\\*x\\*", "*x*"}, |
| 279 | {"simple italic star", "*x*", "_x_"}, |
| 280 | {"escaped underscore", "\\_x\\_", "_x_"}, |
| 281 | {"simple italic underscore", "_x_", "_x_"}, |
| 282 | {"bold roundtrip", "**bold**", "**bold**"}, |
| 283 | {"nested emphasis", "***bold and italic***", "**_bold and italic_**"}, |
| 284 | {"escaped in heading", "# \\*Title\\*", "# *Title*"}, |
| 285 | } |
| 286 | |
| 287 | for _, tt := range tests { |
| 288 | t.Run(tt.name, func(t *testing.T) { |
| 289 | t.Parallel() |
| 290 | |
| 291 | html, err := ConvertFormat(tt.input, "markdown", "html") |
| 292 | require.NoError(t, err) |
| 293 | |
| 294 | back, err := ConvertFormat(html, "html", "markdown") |
| 295 | require.NoError(t, err) |
| 296 | |
| 297 | assert.Equal(t, tt.want, back) |
| 298 | }) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | func TestConvertFormatUnsupported(t *testing.T) { |
| 303 | t.Parallel() |
| 304 | |
| 305 | tests := []struct { |
| 306 | name string |
| 307 | from string |
| 308 | targetFormat string |
| 309 | expectError bool |
| 310 | }{ |
| 311 | {"xml to text", "xml", "text", true}, |
| 312 | {"unknown format", "unknown", "text", true}, |
| 313 | {"text to xml", "text", "xml", true}, |
| 314 | {"markdown to xml", "markdown", "xml", true}, |
| 315 | } |
| 316 | |
| 317 | for _, tt := range tests { |
| 318 | t.Run(tt.name, func(t *testing.T) { |
| 319 | t.Parallel() |
| 320 | |
| 321 | _, err := ConvertFormat("body", tt.from, tt.targetFormat) |
| 322 | if tt.expectError { |
| 323 | assert.Error(t, err) |
| 324 | } else { |
| 325 | assert.NoError(t, err) |
| 326 | } |
| 327 | }) |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | func TestConvertFormatEmptyInputs(t *testing.T) { |
| 332 | t.Parallel() |
| 333 | |
| 334 | tests := []struct { |
| 335 | name string |
| 336 | input string |
| 337 | from string |
| 338 | targetFormat string |
| 339 | expected string |
| 340 | }{ |
| 341 | {"empty markdown to text", "", "markdown", "text", ""}, |
| 342 | {"empty html to text", "", "html", "text", ""}, |
| 343 | {"empty text to markdown", "", "text", "markdown", ""}, |
| 344 | {"empty text to html", "", "text", "html", ""}, |
| 345 | {"empty markdown to html", "", "markdown", "html", "<br>"}, |
| 346 | {"empty html to markdown", "", "html", "markdown", ""}, |
| 347 | } |
| 348 | |
| 349 | for _, tt := range tests { |
| 350 | t.Run(tt.name, func(t *testing.T) { |
| 351 | t.Parallel() |
| 352 | |
| 353 | got, err := ConvertFormat(tt.input, tt.from, tt.targetFormat) |
| 354 | require.NoError(t, err) |
| 355 | assert.Equal(t, tt.expected, got) |
| 356 | }) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | func TestConvertFormatUnicodePreservation(t *testing.T) { |
| 361 | t.Parallel() |
| 362 | |
| 363 | tests := []struct { |
| 364 | name string |
| 365 | input string |
| 366 | from string |
| 367 | targetFormat string |
| 368 | expected string |
| 369 | }{ |
| 370 | {"chinese markdown to text", "# 你好\n\n*测试*", "markdown", "text", "你好\n\n测试"}, |
| 371 | {"emoji markdown to html", "Hello 🌍", "markdown", "html", "<p>Hello 🌍</p>"}, |
| 372 | {"cyrillic html to text", "<p>Привет</p>", "html", "text", "Привет"}, |
| 373 | {"arabic text to html", "مرحبا", "text", "html", "مرحبا"}, |
| 374 | {"mixed unicode markdown to html", "# 你好 **world**", "markdown", "html", "<h1>你好 <strong>world</strong></h1>"}, |
| 375 | } |
| 376 | |
| 377 | for _, tt := range tests { |
| 378 | t.Run(tt.name, func(t *testing.T) { |
| 379 | t.Parallel() |
| 380 | |
| 381 | got, err := ConvertFormat(tt.input, tt.from, tt.targetFormat) |
| 382 | require.NoError(t, err) |
| 383 | assert.Equal(t, tt.expected, got) |
| 384 | }) |
| 385 | } |
| 386 | } |