fixed frontmatter processing, added test
This commit is contained in:
parent
4629200510
commit
0ecad9e96a
5 changed files with 102 additions and 4 deletions
|
@ -2,6 +2,7 @@
|
|||
package builder
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
@ -89,3 +90,94 @@ This is the body of the document.`
|
|||
t.Fatalf("Expected error for invalid frontmatter, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFrontmatter(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
content string
|
||||
wantErr bool
|
||||
wantData []byte
|
||||
wantLen int
|
||||
}{
|
||||
{
|
||||
name: "Valid frontmatter",
|
||||
content: `---
|
||||
title: "Test"
|
||||
author: "User"
|
||||
---
|
||||
Content here`,
|
||||
wantErr: false,
|
||||
wantData: []byte("title: \"Test\"\nauthor: \"User\"\n"),
|
||||
wantLen: 2,
|
||||
},
|
||||
{
|
||||
name: "Missing closing delimiter",
|
||||
content: `---
|
||||
title: "Incomplete Frontmatter"`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Frontmatter later in file",
|
||||
content: `This is some content
|
||||
---
|
||||
title: "Not Frontmatter"
|
||||
---`,
|
||||
wantErr: false,
|
||||
wantData: nil, // Should return nil because `---` is not the first line
|
||||
wantLen: 0,
|
||||
},
|
||||
{
|
||||
name: "Empty frontmatter",
|
||||
content: `---
|
||||
---`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "No frontmatter",
|
||||
content: `This is just a normal file.`,
|
||||
wantErr: false,
|
||||
wantData: nil, // Should return nil as there's no frontmatter
|
||||
wantLen: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Create a temporary file
|
||||
tmpFile, err := os.CreateTemp("", "testfile-*.md")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp file: %v", err)
|
||||
}
|
||||
defer os.Remove(tmpFile.Name())
|
||||
|
||||
// Write test content
|
||||
_, err = tmpFile.WriteString(tc.content)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to write to temp file: %v", err)
|
||||
}
|
||||
tmpFile.Close()
|
||||
|
||||
// Call function under test
|
||||
data, length, err := readFrontmatter(tmpFile.Name())
|
||||
|
||||
// Check for expected error
|
||||
if tc.wantErr {
|
||||
if err == nil {
|
||||
t.Errorf("expected error but got none")
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
// Check content
|
||||
if !bytes.Equal(data, tc.wantData) {
|
||||
t.Errorf("expected %q, got %q", tc.wantData, data)
|
||||
}
|
||||
// Check length
|
||||
if length != tc.wantLen {
|
||||
t.Errorf("expected length %d, got %d", tc.wantLen, length)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue