140 lines
4.1 KiB
Go
140 lines
4.1 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package header
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestParse checks the accepted shapes of a header and the body that follows it.
|
|
func TestParse(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
src string
|
|
wantKeys map[string]string
|
|
wantBody string
|
|
}{
|
|
{
|
|
name: "owner sample",
|
|
src: "Title: GNU/Linux Day 2026\nAuthor: Matteo Bini\nDate: 2026-09-18T17:00:45+0200\n" +
|
|
"Categories: Linux Day, FLUG\n-\nThe text.\n",
|
|
wantKeys: map[string]string{
|
|
"Title": "GNU/Linux Day 2026", "Author": "Matteo Bini",
|
|
"Date": "2026-09-18T17:00:45+0200", "Categories": "Linux Day, FLUG",
|
|
},
|
|
wantBody: "The text.\n",
|
|
},
|
|
{
|
|
name: "value with a colon and a space",
|
|
src: "Title: GNU/Linux Day 2026: programme\n-\nBody\n",
|
|
wantKeys: map[string]string{"Title": "GNU/Linux Day 2026: programme"},
|
|
wantBody: "Body\n",
|
|
},
|
|
{
|
|
name: "byte order mark",
|
|
src: "\ufeffTitle: With BOM\n-\nBody\n",
|
|
wantKeys: map[string]string{"Title": "With BOM"},
|
|
wantBody: "Body\n",
|
|
},
|
|
{
|
|
name: "CRLF line endings",
|
|
src: "Title: Windows\r\nAuthor: Someone\r\n-\r\nLine one\r\nLine two\r\n",
|
|
wantKeys: map[string]string{"Title": "Windows", "Author": "Someone"},
|
|
wantBody: "Line one\nLine two\n",
|
|
},
|
|
{
|
|
name: "blank lines after the terminator are dropped",
|
|
src: "Title: Gap\n-\n\n\nBody\n",
|
|
wantKeys: map[string]string{"Title": "Gap"},
|
|
wantBody: "Body\n",
|
|
},
|
|
{
|
|
name: "only the first dash line ends the header",
|
|
src: "Title: Dashes\n-\nBefore\n-\nAfter\n",
|
|
wantKeys: map[string]string{"Title": "Dashes"},
|
|
wantBody: "Before\n-\nAfter\n",
|
|
},
|
|
{
|
|
name: "empty value",
|
|
src: "Title: Empty\nCategories:\n-\nBody\n",
|
|
wantKeys: map[string]string{"Title": "Empty", "Categories": ""},
|
|
wantBody: "Body\n",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
h, body, err := Parse("file.md", []byte(tt.src))
|
|
if err != nil {
|
|
t.Fatalf("Parse() error = %v", err)
|
|
}
|
|
for k, want := range tt.wantKeys {
|
|
if !h.Has(k) {
|
|
t.Errorf("Has(%q) = false", k)
|
|
}
|
|
if got := h.Get(k); got != want {
|
|
t.Errorf("Get(%q) = %q, want %q", k, got, want)
|
|
}
|
|
}
|
|
if body != tt.wantBody {
|
|
t.Errorf("body = %q, want %q", body, tt.wantBody)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestParseErrors checks that every malformed header names the file and the line.
|
|
func TestParseErrors(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
src string
|
|
want string
|
|
}{
|
|
{"missing terminator", "Title: No end\nAuthor: x\n", "file.md:3"},
|
|
{"line without colon", "Title: Ok\nnonsense\n-\nBody\n", "file.md:2"},
|
|
{"empty key", "Title: Ok\n: value\n-\nBody\n", "file.md:2"},
|
|
{"duplicate key", "Title: One\nTitle: Two\n-\nBody\n", "file.md:2"},
|
|
{"empty file", "", "file.md"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, _, err := Parse("file.md", []byte(tt.src))
|
|
if err == nil {
|
|
t.Fatal("Parse() error = nil")
|
|
}
|
|
if !strings.Contains(err.Error(), tt.want) {
|
|
t.Errorf("error %q does not contain %q", err, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestRequire checks that a missing required key is reported by name.
|
|
func TestRequire(t *testing.T) {
|
|
h, _, err := Parse("file.md", []byte("Title: T\n-\n"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := h.Require("file.md", "Title"); err != nil {
|
|
t.Errorf("Require(Title) = %v", err)
|
|
}
|
|
err = h.Require("file.md", "Title", "Date")
|
|
if err == nil || !strings.Contains(err.Error(), "Date") || !strings.Contains(err.Error(), "file.md") {
|
|
t.Errorf("Require(Title, Date) = %v, want an error naming file.md and Date", err)
|
|
}
|
|
}
|
|
|
|
// TestOnly checks that a key outside the allowed list is rejected.
|
|
func TestOnly(t *testing.T) {
|
|
h, _, err := Parse("file.md", []byte("Title: T\nTitolo: T\n-\n"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = h.Only("file.md", "Title", "Date")
|
|
if err == nil || !strings.Contains(err.Error(), "Titolo") {
|
|
t.Errorf("Only() = %v, want an error naming Titolo", err)
|
|
}
|
|
if err := h.Only("file.md", "Title", "Titolo"); err != nil {
|
|
t.Errorf("Only(all allowed) = %v", err)
|
|
}
|
|
}
|