goldmark/extra_test.go
jkboxomine e6747ee628 Update test case for auto heading ID generation
- remove WithIDs option
- add test cases with international characters and other conditions
2019-12-06 00:29:05 +09:00

99 lines
2.5 KiB
Go

package goldmark_test
import (
"bytes"
"testing"
. "github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/testutil"
)
func TestExtras(t *testing.T) {
markdown := New(WithRendererOptions(
html.WithXHTML(),
html.WithUnsafe(),
))
testutil.DoTestCaseFile(markdown, "_test/extra.txt", t)
}
func TestEndsWithNonSpaceCharacters(t *testing.T) {
markdown := New(WithRendererOptions(
html.WithXHTML(),
html.WithUnsafe(),
))
source := []byte("```\na\n```")
var b bytes.Buffer
err := markdown.Convert(source, &b)
if err != nil {
t.Error(err.Error())
}
if b.String() != "<pre><code>a\n</code></pre>\n" {
t.Errorf("%s \n---------\n %s", source, b.String())
}
}
func TestWindowsNewLine(t *testing.T) {
markdown := New(WithRendererOptions(
html.WithXHTML(),
))
source := []byte("a \r\nb\n")
var b bytes.Buffer
err := markdown.Convert(source, &b)
if err != nil {
t.Error(err.Error())
}
if b.String() != "<p>a<br />\nb</p>\n" {
t.Errorf("%s\n---------\n%s", source, b.String())
}
source = []byte("a\\\r\nb\r\n")
var b2 bytes.Buffer
err = markdown.Convert(source, &b2)
if err != nil {
t.Error(err.Error())
}
if b2.String() != "<p>a<br />\nb</p>\n" {
t.Errorf("\n%s\n---------\n%s", source, b2.String())
}
}
type myIDs struct {
}
func (s *myIDs) Generate(value []byte, kind ast.NodeKind) []byte {
return []byte("my-id")
}
func (s *myIDs) Put(value []byte) {
}
func TestAutogeneratedIDs(t *testing.T) {
ctx := parser.NewContext()
markdown := New(WithParserOptions(parser.WithAutoHeadingID()))
source := []byte("## Não há quem goste de dor")
var b bytes.Buffer
err := markdown.Convert(source, &b, parser.WithContext(ctx))
if err != nil {
t.Error(err.Error())
}
if b.String() != `<h2 id="não-há-quem-goste-de-dor">Não há quem goste de dor</h2>
` {
t.Errorf("%s\n---------\n%s", source, b.String())
}
source = []byte("## 봄 꿀밤 단 꿀밤 v1.0 (2019년-1월-1일)\n### 가을 꿀밤 안단 꿀밤 v2.0 (2020년_12월_31일)")
var b2 bytes.Buffer
err = markdown.Convert(source, &b2, parser.WithContext(ctx))
if err != nil {
t.Error(err.Error())
}
if b2.String() != `<h2 id="봄-꿀밤-단-꿀밤-v1-0-2019년-1월-1일">봄 꿀밤 단 꿀밤 v1.0 (2019년-1월-1일)</h2>
<h3 id="가을-꿀밤-안단-꿀밤-v2-0-2020년-12월-31일">가을 꿀밤 안단 꿀밤 v2.0 (2020년_12월_31일)</h3>
` {
t.Errorf("%s\n---------\n%s", source, b2.String())
}
}