package classes
import (
"strings"
"testing"
)
// TestConvertH1 pins ConvertH1's byte rule: only h1 tag names change.
func TestConvertH1(t *testing.T) {
cases := []struct {
in, want string
n int
}{
{"
A
x
", "A
x
", 1},
{`T
`, `T
`, 1},
{"Up
", "Up
", 1},
{"a
\nb
", "a
\nb
", 2},
{"no heading & text
", "no heading & text
", 0},
{"x
", "x
", 0},
{"", "", 0},
{"xy
", "xy
", 1},
{"# Markdown raw
a < b", "# Markdown raw
a < b", 1},
{"unterminated
x", "
unterminated
x", 1},
{"text <", "text <", 0},
{"", "", 0},
}
for _, c := range cases {
got, n := ConvertH1(c.in)
if got != c.want || n != c.n {
t.Errorf("ConvertH1(%q) = %q, %d; want %q, %d", c.in, got, n, c.want, c.n)
}
}
// L1 WR-03: input without a match comes back identical, and an h1
// followed by an unfinished tag keeps the tail.
for _, in := range []string{"p a a x
a x
a Title
text
")
if c != "# Title\n\ntext" || h != "Title
text
" || n != 1 {
t.Fatalf("markdown body = %q %q %d", c, h, n)
}
c, h, n = ConvertContentH1("a
b
", "")
if c != "a
b
" || h != "" || n != 2 {
t.Fatalf("HTML body without rendered HTML = %q %q %d", c, h, n)
}
if _, _, n := ConvertContentH1("x
", "x
"); n != 0 {
t.Fatal("no H1, nothing converted")
}
if CountH1(`a
b
c
`) != 2 || CountH1("") != 0 {
t.Fatal("CountH1 is wrong")
}
// L1 CR-02: markdown with a fenced html sample and an inline code h1 is
// kept byte for byte; its rendered HTML has the real heading converted
// and the code still escaped.
md := "# Title\n\nExample:\n\n```html\nHello
\n```\n\nInline `` tag."
rendered, err := FormatHTML(md)
if err != nil {
t.Fatal(err)
}
c, h, n = ConvertContentH1(md, rendered)
if c != md {
t.Fatalf("the markdown source was rewritten:\n%s", c)
}
if n != 1 || !strings.Contains(h, "<h1>") {
t.Fatalf("rendered HTML = %q n=%d", h, n)
}
// Markdown without rendered HTML: nothing changes, nothing counted.
if c, h, n := ConvertContentH1("plain x
text", ""); c != "plain x
text" || h != "" || n != 0 {
t.Fatalf("markdown without HTML = %q %q %d", c, h, n)
}
}
// TestConvertTranslationH1 pins a locale's translated attributes,
// changed in place: content and content_html converted, other attributes
// and values that are not strings left alone (a category's pillar text
// takes the same path as a post body).
func TestConvertTranslationH1(t *testing.T) {
attrs := map[string]any{"title": "not content
", "content": "Pillar
x
", "content_html": 5}
if n := ConvertTranslationH1(attrs); n != 1 || attrs["content"] != "Pillar
x
" || attrs["title"] != "not content
" || attrs["content_html"] != 5 {
t.Fatalf("attrs = %v n=%d", attrs, n)
}
both := map[string]any{"content": "# T", "content_html": "T
"}
if n := ConvertTranslationH1(both); n != 1 || both["content_html"] != "T
" || both["content"] != "# T" {
t.Fatalf("both = %v n=%d", both, n)
}
if ConvertTranslationH1(nil) != 0 || ConvertTranslationH1(map[string]any{"title": "x"}) != 0 {
t.Fatal("nothing to convert")
}
md := "Sample:\n\n```html\nHi
\n```\n"
tr := map[string]any{"content": md, "content_html": "<h1>Hi</h1>
"}
if n := ConvertTranslationH1(tr); n != 0 || tr["content"] != md {
t.Fatalf("a markdown translation was rewritten: %v n=%d", tr, n)
}
}