test: unit tests for heading conversion, image alt, content facts and related settings
- 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
This commit is contained in:
@@ -20,3 +20,28 @@ func TestSetImageAlt(t *testing.T) {
|
||||
t.Fatalf("missing = %q, %d", out, n)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetImageAltEdges pins the edges: every matching img is rewritten
|
||||
// and counted (the first one's old alt answered), repeated alt attributes
|
||||
// collapse to one, a src given escaped does not match the unescaped one,
|
||||
// an empty src and a body without img change nothing, and text that
|
||||
// looks like an img inside a comment or script is not touched.
|
||||
func TestSetImageAltEdges(t *testing.T) {
|
||||
doc := `<img src="a.jpg" alt="one"><p>t</p><img alt="x" src="a.jpg" alt="y"><!-- <img src="a.jpg"> --><script>"<img src=a.jpg>"</script>`
|
||||
out, n, old := SetImageAlt(doc, "a.jpg", "new")
|
||||
if n != 2 || old == nil || *old != "one" {
|
||||
t.Fatalf("n=%d old=%v", n, old)
|
||||
}
|
||||
want := `<img src="a.jpg" alt="new"><p>t</p><img alt="new" src="a.jpg"><!-- <img src="a.jpg"> --><script>"<img src=a.jpg>"</script>`
|
||||
if out != want {
|
||||
t.Fatalf("out = %q\nwant %q", out, want)
|
||||
}
|
||||
if out, n, _ := SetImageAlt(`<img src="a.jpg?x=1&y=2">`, "a.jpg?x=1&y=2", "z"); n != 0 || out != `<img src="a.jpg?x=1&y=2">` {
|
||||
t.Fatalf("escaped src matched: %q %d", out, n)
|
||||
}
|
||||
for _, tc := range []struct{ html, src string }{{`<img src="a.jpg">`, ""}, {`<p>no images</p>`, "a.jpg"}, {"", "a.jpg"}} {
|
||||
if out, n, old := SetImageAlt(tc.html, tc.src, "x"); out != tc.html || n != 0 || old != nil {
|
||||
t.Errorf("SetImageAlt(%q, %q) = %q %d %v", tc.html, tc.src, out, n, old)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user