package sanitize import ( "html/template" "testing" "github.com/stretchr/testify/assert" ) func TestMarkdownToHTML(t *testing.T) { assert := assert.New(t) tests := []struct { Test string Result template.HTML }{ {"", ""}, {"> lll", "
\n\n"}, {"> lll > lol", "lll
\n
\n\n"}, // Limit number of blockquotes {"> lll", "lll > lol
\n
\n\n"}, {"\n", ""}, {"lol", "lll
\n
lol
\n"}, // keep HTML tags {"[b]lol[/b]", "[b]lol[/b]
\n"}, // keep BBCode tags {"**[b]lol[/b]**", "[b]lol[/b]
\n"}, // Render Markdown } for _, test := range tests { assert.Equal(test.Result, MarkdownToHTML(test.Test), "Should be equal") } } func TestParseBBCodes(t *testing.T) { assert := assert.New(t) tests := []struct { Test string Result string }{ {"", ""}, {">", ">"}, // keep escaped html {"lol", "lol"}, // keep html tags {"[b]lol[/b]", "lol"}, // Convert bbcodes {"[u][b]lol[/u]", "lol"}, // Close unclosed tags } for _, test := range tests { assert.Equal(test.Result, ParseBBCodes(test.Test), "Should be equal") } assert.Contains(ParseBBCodes("[url=http://kk.cc/]lol[/url]"), "rel=\"nofollow\"") // rel="nofollow" for urls } func TestRepairHTMLTags(t *testing.T) { assert := assert.New(t) tests := []struct { Test string Result string }{ {"", ""}, {">", ">"}, // keep escaped html {"lol", "lol"}, // keep html tags {"lol", "lol"}, // close unclosed tags encapsulated {"lol", "lol"}, // close unclosed tags non encapsulated {"lol", "lol"}, // close unclosed tags non encaptsulated + remove useless end tags {"