package classes
import "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)
}
}
}
// TestConvertContentH1 pins the body pair: content_html's count when its
// HTML had H1s, content's otherwise (a markdown body's H1 is only in its
// rendered HTML), both converted.
func TestConvertContentH1(t *testing.T) {
c, h, n := ConvertContentH1("# Title\n\ntext", "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")
}
}
// 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")
}
}