83 lines
4.9 KiB
Go

package markdown_test
import (
"testing"
"git.akyoto.dev/go/assert"
"git.akyoto.dev/go/markdown"
)
func TestEmpty(t *testing.T) {
assert.Equal(t, markdown.Render(""), "")
}
func TestParagraph(t *testing.T) {
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>")
assert.Equal(t, markdown.Render("Line 1\n\nLine 2"), "<p>Line 1</p><p>Line 2</p>")
}
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>")
}
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>")
}
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>")
}
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>")
}
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>")
}
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>")
}
func TestCombined(t *testing.T) {
assert.Equal(t, markdown.Render("# Header\n\nLine 1."), "<h1>Header</h1><p>Line 1.</p>")
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>")
assert.Equal(t, markdown.Render("# [Header Link](https://example.com/)"), "<h1><a href=\"https://example.com/\">Header Link</a></h1>")
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>")
}
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>")
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>")
}