basic front matter test

This commit is contained in:
facundoolano 2024-02-10 12:18:19 -03:00
parent fe2684a2aa
commit c762cbb909
2 changed files with 54 additions and 2 deletions

View file

@ -1,3 +1,4 @@
// TODO consider making this another package
package commands
import (
@ -5,6 +6,7 @@ import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
@ -56,7 +58,7 @@ func render(sourcePath string) (string, string, error) {
return html, targetPath, nil
}
func extractFrontMatter(file *os.File) ([]byte, map[string]interface{}, error) {
func extractFrontMatter(file io.Reader) ([]byte, map[string]interface{}, error) {
const FM_SEPARATOR = "---"
var outContent, yamlContent []byte
@ -91,7 +93,7 @@ func extractFrontMatter(file *os.File) ([]byte, map[string]interface{}, error) {
if len(yamlContent) != 0 {
err := yaml.Unmarshal([]byte(yamlContent), &frontMatter)
if err != nil {
return nil, nil, fmt.Errorf("invalid yaml: ", err)
return nil, nil, fmt.Errorf("invalid yaml: %s", err)
}
}

View file

@ -0,0 +1,50 @@
package commands
import (
"strings"
"testing"
)
func TestExtractFrontMatter(t *testing.T) {
input := `---
title: my new post
subtitle: a blog post
tags: ["software", "web"]
---
<p>Hello World!</p>`
outContent, yaml, err := extractFrontMatter(strings.NewReader(input))
assertEqual(t, err, nil)
assertEqual(t, string(outContent), "<p>Hello World!</p>")
assertEqual(t, yaml["title"], "my new post")
assertEqual(t, yaml["subtitle"], "a blog post")
assertEqual(t, yaml["tags"].([]interface{})[0], "software")
assertEqual(t, yaml["tags"].([]interface{})[1], "web")
}
func TestInvalidFrontMatterDelimiter(t *testing.T) {
// TODO
}
func TestInvalidFrontMatterYaml(t *testing.T) {
// TODO
}
func TestFrontMatterNotAtTop(t *testing.T) {
// TODO
}
func TestRenderHtml(t *testing.T) {
// TODO
}
func TestRenderOrg(t *testing.T) {
// TODO
}
// TODO move to assert package
func assertEqual(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Fatalf("%s != %s", a, b)
}
}