- ConvertContentH1 and ConvertTranslationH1 for post bodies and category pillar text - SetImageAlt with several matches, repeated alt attributes and markup that only looks like an img - the content facts outline, images, decorative images and embeds - the related-posts mode and count
69 lines
2.8 KiB
Go
69 lines
2.8 KiB
Go
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
|
|
}{
|
|
{"<h1>A</h1><p>x</p>", "<h2>A</h2><p>x</p>", 1},
|
|
{`<h1 class="x" id='y'>T</h1>`, `<h2 class="x" id='y'>T</h2>`, 1},
|
|
{"<H1>Up</H1>", "<h2>Up</h2>", 1},
|
|
{"<h1>a</h1>\n<h1>b</h1>", "<h2>a</h2>\n<h2>b</h2>", 2},
|
|
{"<p>no heading & text</p>", "<p>no heading & text</p>", 0},
|
|
{"<!-- <h1>c</h1> --><p>x</p>", "<!-- <h1>c</h1> --><p>x</p>", 0},
|
|
{"<script>var s = '<h1>';</script>", "<script>var s = '<h1>';</script>", 0},
|
|
{"<h10>x</h10><h1>y</h1>", "<h10>x</h10><h2>y</h2>", 1},
|
|
{"# Markdown <h1>raw</h1> a < b", "# Markdown <h2>raw</h2> a < b", 1},
|
|
{"<p>unterminated <h1>x", "<p>unterminated <h2>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", "<h1>Title</h1><p>text</p>")
|
|
if c != "# Title\n\ntext" || h != "<h2>Title</h2><p>text</p>" || n != 1 {
|
|
t.Fatalf("markdown body = %q %q %d", c, h, n)
|
|
}
|
|
c, h, n = ConvertContentH1("<h1>a</h1><h1>b</h1>", "")
|
|
if c != "<h2>a</h2><h2>b</h2>" || h != "" || n != 2 {
|
|
t.Fatalf("HTML body without rendered HTML = %q %q %d", c, h, n)
|
|
}
|
|
if _, _, n := ConvertContentH1("<p>x</p>", "<p>x</p>"); n != 0 {
|
|
t.Fatal("no H1, nothing converted")
|
|
}
|
|
if CountH1(`<h1>a</h1><div><h1 class="x">b</h1></div><h2>c</h2>`) != 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": "<h1>not content</h1>", "content": "<h1>Pillar</h1><p>x</p>", "content_html": 5}
|
|
if n := ConvertTranslationH1(attrs); n != 1 || attrs["content"] != "<h2>Pillar</h2><p>x</p>" || attrs["title"] != "<h1>not content</h1>" || attrs["content_html"] != 5 {
|
|
t.Fatalf("attrs = %v n=%d", attrs, n)
|
|
}
|
|
both := map[string]any{"content": "# T", "content_html": "<h1>T</h1>"}
|
|
if n := ConvertTranslationH1(both); n != 1 || both["content_html"] != "<h2>T</h2>" || 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")
|
|
}
|
|
}
|