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

\n

b

", "

a

\n

b

", 2}, {"

no heading & text

", "

no heading & text

", 0}, {"

x

", "

x

", 0}, {"", "", 0}, {"x

y

", "x

y

", 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 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\n

Hello

\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) } // A raw h1 outside code is a heading in the rendered page, so it is // converted in the markdown source too (as v0.3.1 did), and counted // when there is no rendered HTML. if c, h, n := ConvertContentH1("plain

x

text", ""); c != "plain

x

text" || h != "" || n != 1 { t.Fatalf("markdown without HTML = %q %q %d", c, h, n) } } // TestConvertContentH1HTMLLead pins markdown that starts with an HTML // block (33-REVIEW-GAPFIX WR-01): the first byte does not make it HTML, // so the h1 samples in its code keep their tags and only a raw h1 outside // code is converted. func TestConvertContentH1HTMLLead(t *testing.T) { probe := "

Intro

\n\n```html\n

Hello

\n```" rendered, err := FormatHTML(probe) if err != nil { t.Fatal(err) } for _, html := range []string{rendered, ""} { c, h, n := ConvertContentH1(probe, html) if c != probe || h != html || n != 0 { t.Fatalf("probe with content_html %q = %q %q n=%d", html, c, h, n) } } mixed := "

Intro

\n\n

Real

\n\n```html\n

Hello

\n```" want := "

Intro

\n\n

Real

\n\n```html\n

Hello

\n```" if c, h, n := ConvertContentH1(mixed, ""); c != want || h != "" || n != 1 { t.Fatalf("raw h1 outside code = %q %q n=%d", c, h, n) } span := "

x

\n\nUse `

` here" if c, _, n := ConvertContentH1(span, ""); c != span || n != 0 { t.Fatalf("code span = %q n=%d", c, 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\n

Hi

\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) } probe := "

Intro

\n\n```html\n

Hello

\n```" rendered, err := FormatHTML(probe) if err != nil { t.Fatal(err) } lead := map[string]any{"content": probe, "content_html": rendered} if n := ConvertTranslationH1(lead); n != 0 || lead["content"] != probe { t.Fatalf("markdown with an HTML lead was rewritten: %v n=%d", lead, n) } }