90 lines
5.3 KiB
Go
Raw Normal View History

2024-03-31 19:01:47 +00:00
package markdown_test
import (
"testing"
"git.akyoto.dev/go/assert"
"git.akyoto.dev/go/markdown"
)
2024-03-31 21:08:01 +00:00
func TestEmpty(t *testing.T) {
assert.Equal(t, markdown.Render(""), "")
}
2024-04-01 09:54:14 +00:00
func TestParagraph(t *testing.T) {
2024-03-31 21:08:01 +00:00
assert.Equal(t, markdown.Render("Text"), "<p>Text</p>")
assert.Equal(t, markdown.Render("Text\n"), "<p>Text</p>")
assert.Equal(t, markdown.Render("Text\n\n"), "<p>Text</p>")
assert.Equal(t, markdown.Render("Text\n\n\n"), "<p>Text</p>")
assert.Equal(t, markdown.Render("Line 1\nLine 2"), "<p>Line 1 Line 2</p>")
2024-04-01 10:43:36 +00:00
assert.Equal(t, markdown.Render("Line 1\n\nLine 2"), "<p>Line 1</p><p>Line 2</p>")
2024-03-31 21:08:01 +00:00
}
func TestHeader(t *testing.T) {
assert.Equal(t, markdown.Render("# Header"), "<h1>Header</h1>")
assert.Equal(t, markdown.Render("## Header"), "<h2>Header</h2>")
assert.Equal(t, markdown.Render("### Header"), "<h3>Header</h3>")
assert.Equal(t, markdown.Render("#### Header"), "<h4>Header</h4>")
assert.Equal(t, markdown.Render("##### Header"), "<h5>Header</h5>")
assert.Equal(t, markdown.Render("###### Header"), "<h6>Header</h6>")
}
2024-04-01 09:54:14 +00:00
func TestLink(t *testing.T) {
assert.Equal(t, markdown.Render("[text](https://example.com/)"), "<p><a href=\"https://example.com/\">text</a></p>")
assert.Equal(t, markdown.Render("[text](https://example.com/"), "<p>[text](https://example.com/</p>")
assert.Equal(t, markdown.Render("[text]https://example.com/)"), "<p>[text]https://example.com/)</p>")
assert.Equal(t, markdown.Render("[text(https://example.com/)"), "<p>[text(https://example.com/)</p>")
assert.Equal(t, markdown.Render("text](https://example.com/)"), "<p>text](https://example.com/)</p>")
assert.Equal(t, markdown.Render("Prefix [text](https://example.com/) suffix."), "<p>Prefix <a href=\"https://example.com/\">text</a> suffix.</p>")
2024-04-01 16:10:52 +00:00
}
2024-04-01 18:04:20 +00:00
func TestList(t *testing.T) {
assert.Equal(t, markdown.Render("- Entry"), "<ul><li>Entry</li></ul>")
assert.Equal(t, markdown.Render("- Entry 1\n- Entry 2"), "<ul><li>Entry 1</li><li>Entry 2</li></ul>")
assert.Equal(t, markdown.Render("- Entry 1\n- Entry 2\n- Entry 3"), "<ul><li>Entry 1</li><li>Entry 2</li><li>Entry 3</li></ul>")
}
2024-04-01 18:59:32 +00:00
func TestTables(t *testing.T) {
assert.Equal(t, markdown.Render("| Head |\n| --- |\n| Body |"), "<table><thead><tr><th>Head</th></tr></thead><tbody><tr><td>Body</td></tr></tbody></table>")
assert.Equal(t, markdown.Render("| 1 | 2 |\n| --- | --- |\n| 1 | 2 |"), "<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table>")
}
2024-04-01 19:27:10 +00:00
func TestCode(t *testing.T) {
assert.Equal(t, markdown.Render("```\nText\n```"), "<pre><code>Text</code></pre>")
assert.Equal(t, markdown.Render("```go\ntype A struct {\n\t\n}\n```"), "<pre><code class=\"language-go\">type A struct {\n\t\n}</code></pre>")
2024-04-02 17:13:28 +00:00
assert.Equal(t, markdown.Render("`monospace`"), "<p><code>monospace</code></p>")
assert.Equal(t, markdown.Render("Inline `monospace` text."), "<p>Inline <code>monospace</code> text.</p>")
2024-04-01 19:27:10 +00:00
}
2024-04-01 16:10:52 +00:00
func TestQuote(t *testing.T) {
assert.Equal(t, markdown.Render("> Line"), "<blockquote><p>Line</p></blockquote>")
assert.Equal(t, markdown.Render("> Line 1\n> Line 2"), "<blockquote><p>Line 1 Line 2</p></blockquote>")
assert.Equal(t, markdown.Render("> Line 1\n\nLine 2"), "<blockquote><p>Line 1</p></blockquote><p>Line 2</p>")
assert.Equal(t, markdown.Render("> Line 1\n>>Line 2"), "<blockquote><p>Line 1</p><blockquote><p>Line 2</p></blockquote></blockquote>")
assert.Equal(t, markdown.Render("Line 1\n> Line 2\n> Line 3\nLine 4"), "<p>Line 1</p><blockquote><p>Line 2 Line 3</p></blockquote><p>Line 4</p>")
2024-04-01 09:54:14 +00:00
}
2024-04-02 17:25:42 +00:00
func TestSeparator(t *testing.T) {
assert.Equal(t, markdown.Render("---"), "<hr>")
assert.Equal(t, markdown.Render("Line 1\n\n---\n\nLine 2"), "<p>Line 1</p><hr><p>Line 2</p>")
}
2024-03-31 21:08:01 +00:00
func TestCombined(t *testing.T) {
2024-04-01 10:43:36 +00:00
assert.Equal(t, markdown.Render("# Header\n\nLine 1."), "<h1>Header</h1><p>Line 1.</p>")
2024-03-31 21:08:01 +00:00
assert.Equal(t, markdown.Render("# Header\nLine 1.\nLine 2.\nLine 3."), "<h1>Header</h1><p>Line 1. Line 2. Line 3.</p>")
assert.Equal(t, markdown.Render("# Header 1\nLine 1.\n# Header 2\nLine 2."), "<h1>Header 1</h1><p>Line 1.</p><h1>Header 2</h1><p>Line 2.</p>")
2024-04-01 09:54:14 +00:00
assert.Equal(t, markdown.Render("# [Header Link](https://example.com/)"), "<h1><a href=\"https://example.com/\">Header Link</a></h1>")
2024-04-01 18:04:20 +00:00
assert.Equal(t, markdown.Render("# Title\n\n- Entry 1\n- Entry 2\n\nText."), "<h1>Title</h1><ul><li>Entry 1</li><li>Entry 2</li></ul><p>Text.</p>")
assert.Equal(t, markdown.Render("- Entry\n# Header"), "<ul><li>Entry</li></ul><h1>Header</h1>")
assert.Equal(t, markdown.Render("> - Entry\n> # Header"), "<blockquote><ul><li>Entry</li></ul><h1>Header</h1></blockquote>")
2024-04-01 09:54:14 +00:00
}
func TestSecurity(t *testing.T) {
assert.Equal(t, markdown.Render("[text](javascript:alert(\"xss\"))"), "<p><a href=\"\">text</a></p>")
assert.Equal(t, markdown.Render("[text](javAscRipt:alert(\"xss\"))"), "<p><a href=\"\">text</a></p>")
2024-04-01 17:34:02 +00:00
assert.Equal(t, markdown.Render("[text]( javascript:alert(\"xss\"))"), "<p><a href=\"\">text</a></p>")
assert.Equal(t, markdown.Render("[text]('javAscRipt:alert(\"xss\")')"), "<p><a href=\"&#39;javAscRipt:alert(&#34;xss&#34;)&#39;\">text</a></p>")
assert.Equal(t, markdown.Render("[text](\"><script>alert(123)</script>)"), "<p><a href=\"&#34;&gt;&lt;script&gt;alert(123)&lt;/script&gt;\">text</a></p>")
assert.Equal(t, markdown.Render("[<script>alert(123)</script>]()"), "<p><a href=\"\">&lt;script&gt;alert(123)&lt;/script&gt;</a></p>")
2024-03-31 19:01:47 +00:00
}