Files

70 lines
2.0 KiB
Go

// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: MIT
package markdown
import (
"bytes"
"testing"
)
func TestDeterministicOutput(t *testing.T) {
source := []byte("# Title\n\nText with < and &.\n\n- one\n - nested\n\n> quoted\n\n" +
"```go\nx := 1\n```\n\n[ref]: /url \"title\"\n")
first := RenderHTML(source)
for i := range 10 {
if next := RenderHTML(source); !bytes.Equal(first, next) {
t.Fatalf("render %d differs:\nfirst: %q\nnext: %q", i, first, next)
}
}
}
func TestReferenceNormalisation(t *testing.T) {
doc := Parse([]byte("[Foo Bar]: /first\n[FOO\t bar]: /second\n"))
if len(doc.refs) != 1 {
t.Fatalf("got %d references, want 1", len(doc.refs))
}
ref, ok := doc.refs["foo bar"]
if !ok {
t.Fatalf("no reference under the normalised label \"foo bar\"")
}
if ref.destination != "/first" {
t.Errorf("destination = %q, want \"/first\": the first definition wins", ref.destination)
}
}
func TestReferenceTitleKept(t *testing.T) {
doc := Parse([]byte("[foo]: /url 'the title'\n"))
ref, ok := doc.refs["foo"]
if !ok {
t.Fatal("no reference recorded")
}
if !ref.hasTitle || ref.title != "the title" {
t.Errorf("title = %q with hasTitle %v, want \"the title\" with hasTitle", ref.title, ref.hasTitle)
}
}
func TestJunkAfterTitleIsNotADefinition(t *testing.T) {
doc := Parse([]byte("[foo]: /url \"title\" ok\n"))
if len(doc.refs) != 0 {
t.Errorf("got %d references, want 0", len(doc.refs))
}
if len(doc.children) != 1 || doc.children[0].kind != kindParagraph {
t.Fatalf("the line should stay a paragraph")
}
}
func TestBacktickInfoStringRejectsFence(t *testing.T) {
doc := Parse([]byte("``` aaa ```\n"))
if len(doc.children) != 1 || doc.children[0].kind != kindParagraph {
t.Fatalf("the line should stay a paragraph, got %d children", len(doc.children))
}
}
func TestParseEmptyDocument(t *testing.T) {
doc := Parse(nil)
if doc.kind != kindDocument || len(doc.children) != 0 {
t.Errorf("Parse(nil) should give an empty document")
}
}