2021-06-12 03:51:07 +00:00
package parsing
import (
2021-06-23 03:52:04 +00:00
"strings"
2021-06-12 03:51:07 +00:00
"testing"
"github.com/stretchr/testify/assert"
)
2021-06-23 03:52:04 +00:00
func TestMarkdown ( t * testing . T ) {
t . Run ( "fenced code blocks" , func ( t * testing . T ) {
t . Run ( "multiple lines" , func ( t * testing . T ) {
2021-08-27 03:59:12 +00:00
html := ParseMarkdown ( "```\nmultiple lines\n\tof code\n```" , ForumRealMarkdown )
2021-06-23 03:52:04 +00:00
t . Log ( html )
assert . Equal ( t , 1 , strings . Count ( html , "<pre" ) )
assert . Contains ( t , html , ` class="hmn-code" ` )
assert . Contains ( t , html , "multiple lines\n\tof code" )
} )
t . Run ( "multiple lines with language" , func ( t * testing . T ) {
2021-08-27 03:59:12 +00:00
html := ParseMarkdown ( "```go\nfunc main() {\n\tfmt.Println(\"Hello, world!\")\n}\n```" , ForumRealMarkdown )
2021-06-23 03:52:04 +00:00
t . Log ( html )
assert . Equal ( t , 1 , strings . Count ( html , "<pre" ) )
assert . Contains ( t , html , ` class="hmn-code" ` )
assert . Contains ( t , html , "Println" )
assert . Contains ( t , html , "Hello, world!" )
} )
2021-06-12 03:51:07 +00:00
} )
}
2021-06-13 17:22:55 +00:00
2021-06-23 03:52:04 +00:00
func TestBBCode ( t * testing . T ) {
t . Run ( "[code]" , func ( t * testing . T ) {
t . Run ( "one line" , func ( t * testing . T ) {
2021-08-27 03:59:12 +00:00
html := ParseMarkdown ( "[code]Just some code, you know?[/code]" , ForumRealMarkdown )
2021-06-23 03:52:04 +00:00
t . Log ( html )
assert . Equal ( t , 1 , strings . Count ( html , "<pre" ) )
assert . Contains ( t , html , ` class="hmn-code" ` )
assert . Contains ( t , html , "Just some code, you know?" )
} )
t . Run ( "multiline" , func ( t * testing . T ) {
bbcode := ` [ code ]
Multiline code
with an indent
[ / code ] `
2021-08-27 03:59:12 +00:00
html := ParseMarkdown ( bbcode , ForumRealMarkdown )
2021-06-23 03:52:04 +00:00
t . Log ( html )
assert . Equal ( t , 1 , strings . Count ( html , "<pre" ) )
assert . Contains ( t , html , ` class="hmn-code" ` )
assert . Contains ( t , html , "Multiline code\n\twith an indent" )
assert . NotContains ( t , html , "<br" )
} )
t . Run ( "multiline with language" , func ( t * testing . T ) {
bbcode := ` [ code language = go ]
func main ( ) {
fmt . Println ( "Hello, world!" )
}
[ / code ] `
2021-08-27 03:59:12 +00:00
html := ParseMarkdown ( bbcode , ForumRealMarkdown )
2021-06-23 03:52:04 +00:00
t . Log ( html )
assert . Equal ( t , 1 , strings . Count ( html , "<pre" ) )
assert . Contains ( t , html , "Println" )
assert . Contains ( t , html , "Hello, world!" )
} )
} )
2021-06-13 17:22:55 +00:00
}
2021-06-20 22:21:58 +00:00
2021-07-18 02:42:52 +00:00
func TestSharlock ( t * testing . T ) {
t . Skipf ( "This doesn't pass right now because parts of Sharlock's original source read as indented code blocks, or depend on different line break behavior." )
t . Run ( "sanity check" , func ( t * testing . T ) {
2021-08-27 03:59:12 +00:00
result := ParseMarkdown ( sharlock , ForumRealMarkdown )
2021-07-18 02:42:52 +00:00
for _ , line := range strings . Split ( result , "\n" ) {
assert . NotContains ( t , line , "[b]" )
assert . NotContains ( t , line , "[/b]" )
assert . NotContains ( t , line , "[ul]" )
assert . NotContains ( t , line , "[/ul]" )
assert . NotContains ( t , line , "[li]" )
assert . NotContains ( t , line , "[/li]" )
assert . NotContains ( t , line , "[img]" )
assert . NotContains ( t , line , "[/img]" )
assert . NotContains ( t , line , "[code" )
assert . NotContains ( t , line , "[/code]" )
}
} )
}
2021-07-17 23:20:47 +00:00
func BenchmarkSharlock ( b * testing . B ) {
for i := 0 ; i < b . N ; i ++ {
2021-08-27 03:59:12 +00:00
ParseMarkdown ( sharlock , ForumRealMarkdown )
2021-07-17 23:20:47 +00:00
}
2021-06-20 22:21:58 +00:00
}