2021-04-21 19:13:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExtractFrontmatter(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Raw string
|
|
|
|
Frontmatter string
|
|
|
|
Content string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Raw: "",
|
|
|
|
Frontmatter: "",
|
|
|
|
Content: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Raw: "---\na: b\nc: d\n---",
|
|
|
|
Frontmatter: "\na: b\nc: d",
|
|
|
|
Content: "",
|
|
|
|
},
|
2021-04-24 17:24:31 +00:00
|
|
|
{
|
|
|
|
Raw: "---\na: b\nc: d\n---\nHello, world!",
|
|
|
|
Frontmatter: "\na: b\nc: d",
|
|
|
|
Content: "\nHello, world!",
|
|
|
|
},
|
2021-04-21 19:13:45 +00:00
|
|
|
{
|
|
|
|
Raw: "# Hello, world!",
|
|
|
|
Frontmatter: "",
|
|
|
|
Content: "# Hello, world!",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Raw: "---\ne: f\ng: h",
|
|
|
|
Frontmatter: "\ne: f\ng: h",
|
|
|
|
Content: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Raw: "----\na: b\nc: d",
|
|
|
|
Frontmatter: "",
|
|
|
|
Content: "----\na: b\nc: d",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Raw: "---\n---",
|
|
|
|
Frontmatter: "",
|
|
|
|
Content: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Raw: "\n---\n---\nHello, world!",
|
|
|
|
Frontmatter: "",
|
|
|
|
Content: "\n---\n---\nHello, world!",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Raw: "---",
|
|
|
|
Frontmatter: "",
|
|
|
|
Content: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Raw: "----",
|
|
|
|
Frontmatter: "",
|
|
|
|
Content: "----",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
frontmatter, content := extractFrontmatter([]byte(test.Raw))
|
|
|
|
if string(frontmatter) != test.Frontmatter {
|
2022-06-14 21:42:23 +00:00
|
|
|
t.Fatalf("expected frontmatter %q, got %q, raw %q", test.Frontmatter, string(frontmatter), test.Raw)
|
2021-04-21 19:13:45 +00:00
|
|
|
}
|
|
|
|
if string(content) != test.Content {
|
2022-06-14 21:42:23 +00:00
|
|
|
t.Fatalf("expected content %q, got %q, raw %q", test.Content, string(content), test.Raw)
|
2021-04-21 19:13:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|