motorino/internal/mdx/emdash_test.go

57 lines
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")
}
}
// 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)
}
}