Files
uptimesebi/internal/pdf/pdf_test.go
T
Sebastian UnterschützandClaude Opus 5.5 f892f7c03a feat(pdf): write simple text documents as PDF
Standard fonts with WinAnsi encoding cover German text without a
dependency; output is checked with pdftotext where available.

Refs #66

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 10:10:15 +01:00

57 lines
1.5 KiB
Go

package pdf
import (
"bytes"
"os/exec"
"strings"
"testing"
)
func TestWrap(t *testing.T) {
got := wrap("aaa bbb ccc", 7)
if strings.Join(got, "|") != "aaa bbb|ccc" {
t.Errorf("wrap() = %q", got)
}
if got := wrap(strings.Repeat("x", 10), 4); len(got) != 3 || got[0] != "xxxx" {
t.Errorf("wrap() of an unbreakable word = %q", got)
}
}
func TestEscape(t *testing.T) {
if got := escape(`Grüße (a\b) 漢`); got != "Gr\xfc\xdfe \\(a\\\\b\\) ?" {
t.Errorf("escape() = %q", got)
}
}
func TestDocumentIsReadable(t *testing.T) {
d := New("Export")
d.Title("Datenexport für Jörg")
for i := range 200 {
d.Heading("Abschnitt")
d.Field(1, "Schlüssel", strings.Repeat("Wert ", i%40))
}
var buf bytes.Buffer
if err := d.Write(&buf); err != nil {
t.Fatal(err)
}
if len(d.pages) < 2 {
t.Fatalf("200 sections fit on %d page", len(d.pages))
}
if !bytes.HasPrefix(buf.Bytes(), []byte("%PDF-1.4")) || !bytes.HasSuffix(buf.Bytes(), []byte("%%EOF\n")) {
t.Fatal("not a PDF file")
}
// Validate structure and text with poppler when available.
if _, err := exec.LookPath("pdftotext"); err != nil {
t.Skip("pdftotext not installed")
}
cmd := exec.Command("pdftotext", "-", "-")
cmd.Stdin = &buf
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("pdftotext: %v\n%s", err, out)
}
if !strings.Contains(string(out), "Datenexport für Jörg") || strings.Contains(string(out), "Syntax Error") {
t.Errorf("pdftotext output lacks the title or reports errors:\n%.300s", out)
}
}