motorino/internal/mdx/emdash_test.go
tgiachi 4ab8f8d122
All checks were successful
check / check (pull_request) Successful in 30s
fix: find code blocks with the Markdown parser and make the escape work everywhere
The hand written fence scanner took a line that starts with a code span for a
fence and missed fences in quotes and lists. The code blocks are now the ones
goldmark finds. A backslash before a placeholder is turned into a marker that
survives the Markdown and the list expansion, so it works in a code span and
next to a block name, and two backslashes are a literal one. The em dash rule
reports the line of the body, refuses padded entities and checks the header keys
by name, the feed leaves protocol-relative links alone, base_url needs a host
and a language file cannot set it.
2026-11-15 10:38:56 +01:00

87 lines
3.2 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package mdx
import (
"strings"
"testing"
)
// TestNoEmDash checks every rewrite of the manifesto sources.
func TestNoEmDash(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{"numbered heading", "## 01 — Antifascist. No ambiguity.", "## 01. Antifascist. No ambiguity."},
{"word heading", "## Preamble — We don't ask permission to think", "## Preamble: We don't ask permission to think"},
{"title heading", "# kmalloc.org — Manifesto", "# kmalloc.org: Manifesto"},
{"attribution", "> — Funkadelic, Free Your Mind", "> Funkadelic, Free Your Mind"},
{"parenthetical en", "modify, and — where technically feasible — install", "modify, and, where technically feasible, install"},
{"parenthetical it", "e — dove tecnicamente possibile — installare", "e, dove tecnicamente possibile, installare"},
{"and continues", "it — and continues", "it, and continues"},
{"e continua", "esso — e continua", "esso, e continua"},
{"no dash", "plain text", "plain text"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NoEmDash(tt.in)
if err != nil {
t.Fatalf("NoEmDash() error = %v", err)
}
if got != tt.want {
t.Errorf("NoEmDash() = %q, want %q", got, tt.want)
}
})
}
}
// TestNoEmDashLeftover checks that a dash with no rewrite rule is an error.
func TestNoEmDashLeftover(t *testing.T) {
_, err := NoEmDash("some words — and then more")
if err == nil {
t.Fatal("NoEmDash() error = nil, want an error for a leftover em dash")
}
}
// TestRejectEmDashEntities checks that the entities of the em dash are refused too.
func TestRejectEmDashEntities(t *testing.T) {
for _, in := range []string{"a — b", "a — b", "a — b", "a — b", "a — b", "a — b", "a — b"} {
if err := RejectEmDash("f.md", in); err == nil {
t.Errorf("RejectEmDash(%q) = nil, want an error", in)
}
}
if err := RejectEmDash("f.md", "a – b & c"); err != nil {
t.Errorf("RejectEmDash(ndash) = %v", err)
}
}
// TestNoEmDashErrorSaysBody checks that a leftover is reported as a line of the
// body, which is how the caller counts it.
func TestNoEmDashErrorSaysBody(t *testing.T) {
_, err := NoEmDash("fine\nsome words \u2014 and then more\n")
if err == nil || !strings.Contains(err.Error(), "line 2 of the body") {
t.Errorf("NoEmDash() error = %v, want it to say line 2 of the body", err)
}
}
// TestHasEmDash checks the test that the header keys use.
func TestHasEmDash(t *testing.T) {
for in, want := range map[string]bool{"plain": false, "a \u2014 b": true, "a — b": true, "a – b": false, "": false} {
if got := HasEmDash(in); got != want {
t.Errorf("HasEmDash(%q) = %v, want %v", in, got, want)
}
}
}
// TestRejectEmDash checks that the error names the file and the line.
func TestRejectEmDash(t *testing.T) {
if err := RejectEmDash("content/en/x.md", "clean\ntext\n"); err != nil {
t.Errorf("RejectEmDash(clean) = %v", err)
}
err := RejectEmDash("content/en/x.md", "clean\nbad — line\n")
if err == nil || !strings.Contains(err.Error(), "content/en/x.md") || !strings.Contains(err.Error(), "2") {
t.Errorf("RejectEmDash() = %v, want an error naming the file and line 2", err)
}
}